Chapter – 3. OBJECT ORIENTED PROGRAMMING

Eg:class Rectangle
{
private: float len,bre,area;
public: void readData( )
{
cout<<”\nEnter the length and breadth..”;
cin>>len>>bre;
} void calculate( )
{
area=len*bre;
}
void display( )
{
cout<<”\nThe area of the rectangle = “<<area;
}
};

Eg: Here in the above class the data members ie len,bre,area and the member functions ie readData( ), calculate( ), display( ) are bind together in a class named as Rectangle. Ie The member functions can access any data member in the class.
Benefits with encapsulation:

  1. Modularity.
  2. Information hiding .

2004

1.a. What is polymorphism? Give an example in C ++ to show its implementation in C++.

Ans: Polymorphism is the attribute that allows one interface to be used with different situation.C++ implements polymorphism through virtual functions, through overloaded functions and overloaded operators. A virtual function is used to specify the interface in abstract class, but its implementation details are made available by the concrete class(es).
An overloaded function refers to a function having (one name and) more than one distinct meanings. Similarly, when two or more distinct meanings are defined for an operator, it is said to be an ‘overloaded operator’.It is the compiler’s job to select the specific action as it applies to each situation. Eg: The program in the next answer.

2003:

2.a. What do you understand by function overloading? Give an example illustrating its use in a c++ program.

Ans: A function name having several definitions that are differentiable by the number or types of their arguments, is known as an overloaded function and this process is known as function overloading.
Function overloading not only implements polymorphism but also reduces number of comparisons in a program and thereby makes the program run faster.