Introduction to OOPs
Introduction
Welcome to the exciting world of Object Oriented Programming! In this module you will be introduced to OOPs concepts and their importance in programming. In the previous module you have done the programs in the same way like which you have done in C programming where there is no Object Oriented Concepts included. CPP is one programming language which supports both Procedural and Object Oriented programming. This module majorly focuses on two most fundamental aspects of OOP that is Object and the Class.Learning Objective
By the end of this module, you will be able to:- Know the need of OOP
- Identifying Real world Objects
- Creating Software Objects using class.
- Implement state and behavior of software objects.
Why do we need OOP?
You may have heard about, or been involved in, horror stories of project development in structured programming approach. As programs grow larger the project becomes too complex. When more programs are added, complexity increases and disaster ensues. Analyzing the reasons for above problem reveals weaknesses in the procedural paradigm itself. No matter how well structured programming approach is implemented, large problems become excessively complex. The solution to avoid this problem is to practice Object Oriented Approach.The Object Oriented Approach
The fundamental idea behind object-oriented languages is to combine into a single program entity both data and the functions that operate on that data. Such an entity is called an object. An object’s functions, called member function in C++ because they belong to particular class of objects. In Object Oriented Programming you can’t access the data directly. The data is hidden, so it is safe from accidental alteration. In simple words we can say data and its functions are encapsulated as a single unit. Remember Encapsulation and data hiding are key terms in the description of object oriented languages.Now let us tell more about objects both in the real world and in terms of programming.
What is an Object
Objects are key to understanding object-oriented technology. Look around right now and you'll find many examples of real-world objects: your dog, your desk, your television set, your bicycle.State and Behavior of Objects
Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, height, weight…) and behavior (barking, sit, Lay Down, wagging tail…). Bicycles also have state (current gear, current pedal cadence, and current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior of real-world objects is a great way to begin thinking in terms of objects oriented programming.Take a minute right now to observe the real-world objects that are in your immediate area. For each object that you see, ask yourself two questions: "What possible states can this object be in?" and "What possible behavior can this object perform?" Make sure to write down your observations. As you do, you'll notice that real-world objects vary in complexity; your desktop lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off) but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming.
Software Objects
Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation fundamental principle of object-oriented programming.Advantages of Software Objects
Bundling code into individual software objects provides a number of advantages, including:- Modularity The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.
- Information-hiding By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.
- Code re-use If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.
- Pluggability and debugging ease if a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.
No comments:
Post a Comment