Thursday, August 26, 2010

c plus plus notes

Introduction to Object Oriented Programming

The term Object Oriented Programming is a relatively new concept in the world of programming languages. Earlier the only style of programming was known as Sequential or Linear or Procedural Programming. Every program has two parts: Code and Data. The code part consists of the statements that are executed to perform the job. The data part consists of the variables which hold the necessary values to perform the job. In Sequential programming style more emphasis is placed on the code part and less on the data part. The code statements are executed one by one, sequentially, meaning that you can execute statement number n if you have already executed statement number n-1, and the next statement to execute is statement number n+1.

In Object Oriented Programming the style is changed. Basically, more emphasis is placed on the data part and emphasis placed on the code part is secondary. Everything you have to consider must be viewed as an object. The definition of an object is like the following.

An object is a collection of a set of data and a set of code. The data part is called attributes (formed as variables) and the code part is called functions (formed as executable statements). The relationship between the attributes and the functions is such that if a function is executed, the values of some attribute can be accessed or changed. In other words, to access or change the value of some attribute, we must execute the proper function.

Let us explain with an example. Consider ‘chair’ to be an object. We can say that any chair in the world must have a set of attributes like height, width, breadth, colour, material, weight, price and location. To validate our case we illustrate three different chairs of different types.

Chair1 Chair2 Chair3
Height 3 ft 2.5 ft 3.5 ft
Width 2 ft 2.5 ft 3 ft
Breadth 3 ft 2.25 ft 3.5 ft
Colour Red Green Blue
Material Wood Plastic Iron
Weight 5 kg 2 kg 10 kg
Price 500/- 250/- 1000/-
Location Centre West North East

The attribute set described above defines the data part of the three chair objects. You can see that all the three chairs have the identical attribute set. It may happen that the value within the individual attribute could be different for the three objects. For example the three objects have different values for the material attribute, but all of them have the material attribute in their set of attributes.



But as they are objects, the chairs must also have a set of functions (meaning what we can do with these chairs). The set of functions could be like the following.

Buy ( );
Sell ( );
Repair ( );
Paint ( );
Move ( );

As you can understand, if we execute these functions, then one or some of the attribute values will be accessed or changed. For example, to Buy ( ) or Sell ( ) the chair, its price attribute will be changed. To Repair ( ) a chair, we may change its dimensions, weight or material. To Paint ( ) a chair its colour property will be changed. To Move ( ) a chair, we change its location.

The concept of Class

If some objects are found to have a similar set of attributes and a similar set of functions, then these objects can be grouped together into a class. A class is a collection of declaration of the attribute variables and the definition of the functions. Once the class has been defined, we can create as many objects of a class as we need just as we declare variables of a data type. In the light of a class, an object can also be defined as an instance or occurrence of a class.

We may declare the CHAIR class as having declared variables like height, width, breadth, colour, material, weight, price and location and defined the functions like Buy ( ), Sell ( ), Repair ( ), Paint ( ) and Move ( ). Now, in order to declare a new object Chair4, all we need to do is write the following statement

CHAIR Chair4; (Equivalent to int n;)

And the job is done.

Each object has its own space in memory where it can store its own attribute set, because each object is expected to have a unique attribute set of its own. However, there is no need to store separate copies of the functions for each individual objects, as the coding for the functions need not be changed for each object.

Writing the actual program in an object oriented system is fairly easy. All we have to do is to execute the appropriate function with the appropriate object. For example, if we want to sell Chair2, then we may write

Chair2.Sell ( );

And if we want to move Chair3 then we may write

Chair3.Move ( );

Below we briefly mention some of the important features of the object oriented programming. We shall discuss then in detail in the later chapters.



Encapsulation

Encapsulation is the technique of putting together the data part (attributes) and the code part (functions) that can operate on the data part into the same single container called a class.


Data Abstraction or Data hiding

This is the technique of making some part of the class accessible to the outside world while keeping some sensitive part inaccessible. Generally the attributes of a class are defined with a keyword called private, which means that these attributes cannot be accessed from outside the class by any means. The functions of the class are generally declared with a keyword called public, which means that they can be accessed from outside the class if called using an object of the same class.


Polymorphism

This means the quality of the same thing to exist in different forms. This is an integral feature of object oriented programming that is not found in sequential programming.


Inheritance

This is also another unique feature of object oriented programming. This means the creation of a new class out of an existing class. Inheritance supports the concept of code reuse and saves a lot of time and effort to create a new class.

2 comments:

  1. Programming in C++

    Now it is the time to write the first program in C++. The following is the code for a program for adding two numbers. We are assuming that you have some knowledge of the C language.

    #include
    #include

    class Add
    {
    private :
    int a, b, c; // PRIVATE ATTRIBUTES
    public :
    void input(); // PUBLIC FUNCTIONS
    void add();
    void output();
    }; // CLASS DECLARATION ENDS WITH A SEMICOLON

    void Add :: input()
    {
    cout<<"\n\tEnter first number : ";
    cin>>a;
    cout<<"\n\tEnter second number : ";
    cin>>b;
    }

    void Add :: add()
    {
    c = a + b;
    }

    void Add :: output()
    {
    cout<<"\n\tThe sum of "<
    #include

    class Add
    {
    int a; // PRIVATE ATTRIBUTE
    void greetings(); // PRIVATE FUNCTION

    public :
    int b; // PUBLIC ATTRIBUTE
    void input_output(); // PUBLIC FUNCTION
    };

    void Add :: greetings() // DEFINING THE PRIVATE FUNCTION
    {
    cout<<"\n\tHello";
    }

    void Add :: input_output() // DEFINING THE PUBLIC FUNCTION
    {
    greetings(); // ACCESSING PRIVATE FUNCTION FROM PUBLIC FUNCTION
    cout<<"\n\tEnter first number : ";
    cin>>a; // ACCESSING PRIVATE ATTRIBUTE FROM PUBLIC FUNCTION
    cout<<"\n\tFirst number : "<>obj.b;
    cout<<"\n\tSecond number : "<<obj.b;

    getch();
    }
    The program is self-explanatory. Run it to see the result.

    We hope that you have understood how the concept of encapsulation and data hiding have been implemented in the above two programs.

    ReplyDelete
  2. Constructor and Destructor

    To understand what a constructor does, let us first review the addition program again.


    #include
    #include

    class Add
    {
    private :
    int a, b, c; // PRIVATE ATTRIBUTES
    public :
    void input(); // PUBLIC FUNCTIONS
    void add();
    void output();
    };

    void Add :: input()
    {
    cout<<"\n\tEnter first number : ";
    cin>>a;
    cout<<"\n\tEnter second number : ";
    cin>>b;
    }

    void Add :: add()
    {
    c = a + b;
    }

    void Add :: output()
    {
    cout<<"\n\tThe sum of "<
    #include

    class ABC
    {
    char a; // PRIVATE ATTRIBUTES OF THREE DIFFERENT BASIC DATA TYPES
    int b;
    float c;

    public :
    ABC() // DEFAULT CONSTRCUCTOR, NO ARGUMENTS
    {
    a = 'E';
    b = 10;
    c = 3.1428;
    }

    ABC(char a1, int b1, float c1) // PARAMETERIZED CONSTRUCTOR,
    // TAKING ARGUMENTS
    {
    a = a1;
    b = b1;
    c = c1;
    }

    void output();
    };

    void ABC :: output()
    {
    cout<<"\n\n\tA : "<
    #include

    class ABC
    {
    char a; // PRIVATE ATTRIBUTES OF THREE DIFFERENT BASIC DATA TYPES
    int b;
    float c;

    public :
    ABC() // DEFAULT CONSTRCUCTOR, NO ARGUMENTS
    {
    a = 'E';
    b = 10;
    c = 3.1428;
    }

    ABC(char a1, int b1, float c1) // PARAMETERIZED CONSTRUCTOR,
    // TAKING ARGUMENTS
    {
    a = a1;
    b = b1;
    c = c1;
    }

    ABC(ABC &obj) // COPY CONSTRUCTOR, TAKES AN OBJECT REFERENCE
    // AS ITS ARGUMENT
    {
    a = obj.a;
    b = obj.b;
    c = obj.c;
    }

    void output();
    };

    void ABC :: output()
    {
    cout<<"\n\n\tA : "<
    #include

    class ABC
    {
    char a; // PRIVATE ATTRIBUTES OF THREE DIFFERENT BASIC DATA TYPES
    int b;
    float c;

    public :
    ABC() // DEFAULT CONSTRCUCTOR, NO ARGUMENTS
    {
    a = 'E';
    b = 10;
    c = 3.1428;
    }

    ABC(char a1, int b1, float c1) // PARAMETERIZED CONSTRUCTOR,
    // TAKING ARGUMENTS
    {
    a = a1;
    b = b1;
    c = c1;
    }

    ABC(ABC &obj) // COPY CONSTRUCTOR, TAKES AN OBJECT REFERENCE
    // AS ITS ARGUMENT
    {
    a = obj.a;
    b = obj.b;
    c = obj.c;
    }
    ~ABC() // DESTRUCTOR
    {
    a = '\0';
    b = NULL;
    c = NULL;
    cout<<"\n\tObject destroyed";
    getch();
    }

    void output();
    };

    void ABC :: output()
    {
    cout<<"\n\n\tA : "<<a;
    cout<<"\n\tB : "<<b;
    cout<<"\n\tC : "<<c;
    }

    void main(void)
    {
    ABC obj1; // CALLING DEFAULT CONSTRUCTOR
    ABC obj2('M', 100, 54.7814); // CALLING PARAMATERIZED CONSTRCUTOR
    ABC obj3(obj2); // CALLING COPY CONSTRUCTOR

    clrscr();

    obj1.output();
    obj2.output();
    obj3.output();

    getch();
    }

    First, see the body of the destructor. It nullifies the attributes of the objects, so that if anyone searches for the attribute values in the memory, only null values will be retrieved.

    If you run this program, you will be able to see the output “Object destroyed” three times after the values of the three objects are shown. Seeing the message thrice is a proof that the destructor has been called automatically, as many times as the number of objects have been created in this program. When the last statement of the main ( ) function has been executed, the three objects are no longer required. As a result, the objects automatically call the destructor function, nullify the attributes and then they are out of the scope of the program.

    ReplyDelete