Saturday 29 August 2015

C++:1. THE BIG PICTURE


The Object Oriented Approach
          The fundamental idea behind the object-oriented languages is to combine into a single unit both data and the functions that operate on that data. Such a unit is called an object.
          An object's functions, called member functions in C++, typically provide the only way to access its data. If you want to read a data item in an object, you call a member function in an object. It will read the item and return the value to you. You cant access the data directly. The data is hidden, so it is safe for accidental alteration. Data and its functions are said to be encapsulated into a single entity. Data encapsulation and data hiding are the key terms in the description of object-oriented languages. A C++ program typically consists of a number of objects, which communicate with each other by calling one another's member functions.

OOP: An Approach to Organisation
        Keep in mind that object-oriented programming in not primarily concerned with the details of program operations. Instead it deals with the overall organisation of the program. Most individual program statements in C++ are similar to statements in procedural languages, and many are identical to statements in C. Indeed, an entire member function in C++ program may be very similar to a procedural function in C. It is only when you look at the larger context that you can determine whether a statement or a function is part of a procedural C program or an object-oriented C++ program.




CHARACTERISTICS OF OBJECT-ORIENTED LANGUAGES

OBJECT
            When you approach a programming problem in an object-oriented language, you no longer ask how the problem will be divided into functions, but how it will be divided into objects. Thinking in terms of objects, rather than functions, has a surprisingly helpful effect on how easily programs can be designed. This results from the close match between objects in the programming sense and objects in the real world.

  • Physical objects
  • Elements of the computer-user environment
  • Programming Constructs
  • Collection of data
  • User-defined data types
  • Components in computer games
CLASSES
            In OOP we say that objects are member of classes.
A class serves as a plan, or template. If specifies what data and what function will be included in objects of that class.
A class is thus a collection of similar objects. This fits our nontechnical understanding of the word class. Prince, Sting, and Madonna are members of the class of rock musicians. There is no one person called "rock musician", but specific people with specific names are members of this class if they possess certain characteristics.

INHERITANCE
           The idea of classes leads to the idea of inheritance. In our daily lives, we use the concept of classes being divided into sub-classes. We know that the class of animals is divided into mammals, amphibians, insects, birds, and so on. The class of vehicles is divided into cars, trucks, buses, and motorcycles.
In similar way, an OOP class can be divided into sub-classes. In C++ the original class is called the base class; other classes can be defined that share its characteristics, but add their own as well. These are called derived classes.



RE-USABILITY
          Once a class has been written, created, and debugged, it can be distributed to other programmers for use in their own programs. This is called re-usability.
In OOP, the concept of inheritance provides an important extension to the idea of reusability. A programmer can take an existing class and, without modifying it, add additional features and capabilities to it. This is done by deriving a new class from the existing one. The new class will inherit the capabilities of the old one, but is free to add new features of its own.

CREATING NEW DATA TYPES
          One of the benefits of objects is that they give the programmer a convenient way to construct new data types. Suppose you work with two-dimensional positions in your program. You would like to express operations on these positional values with normal arithmetic operations, such as

                                        position1 = position2 + origin;

POLYMORPHISM AND OVERLOADING
           Using operators or functions in different ways, depending on what they are operating on, is called polymorphism. When an existing operator, such as + or =, is given the capability to operate on a new data types, it is said to be overloaded. Overloading is a kind of polymorphism, it is also an important feature of OOP.

No comments:

Post a Comment