Sponsor Area
Inheritance (Extending Classes)
Answer the questions (i) to (iv) based on the following:
class First
{
int X1;
protected:
float X2;
public:
First();
void Enter1(); void Display1();
};
class Second : private First
{
int Y1;
protected:
float Y2;
public:
Second();
void Enter2();
void Display();
};
class Third : public Second
{
int Z1;
public:
Third();
void Enter3();
void Display();
};
void main()
{
Third T; //Statement 1
__________; //Statement 2
}
(i) Which type of Inheritance out of the following is illustrated in the above example?
Single Level Inheritance, Multilevel Inheritance, Multiple Inheritance?
(ii) Write the names of all the member functions, which are directly accessible by the object T of class Third as declared in main() function.
(iii)Write Statement 2 to call function Display() of class Second from the object T of class Third.
(iv) What will be the order of execution of the constructors, when the object T of class Third is declared inside main()?
(i) Multilevel Inheritance is illustrated by the given example.
(ii) Enter2(), Display() of class Second
Enter3(), Display() of class Third
(iii) T.Second::Display();
(iv) First, Second, Third
Write the definition of a function Alter(int A[], int N) in C++, which should change all the multiples of 5 in the array to 5 and rest of the elements as 0. For example, if an array of 10 integers is as follows:
| A[0] | A[1] | A[2] | A[3] | A[4] | A[5] | A[6] | A[7] | A[8] | A[9] |
| 55 | 45 | 20 | 16 | 39 | 90 | 83 | 40 | 48 | 25 |
After executing the function, the array content should be changed as follows:
| A[0] | A[1] | A[2] | A[3] | A[4] | A[5] | A[6] | A[7] | A[8] | A[9] |
| 5 | 0 | 5 | 0 | 0 | 5 | 0 | 5 | 0 | 5 |
void Alter(int A[ ],int N)
{
for (int i=0;i<N;i++){
if(A[i]%5==0)
A[i]=5;
else
A[i]=0;
}
}
What is the difference between the members in private visibility mode and the members in protected visibility mode inside a class? Also, give a suitable C++ code to illustrate both.
| Private | Public |
| Private members of a class are accessible only from within other members of the same class or from their friends. | Protected members are accessible from members of their same class and from their friends, but also from members of their derived classes. |
|
|
Answer the question (i) and (ii) after going through the following class :
class Travel
{
int PlaceCode; char Place[20]; float Charges; public:
Travel( ) //Function 1
{
PlaceCode=1;strcpy(Place,”DELHI”);Charges=1000;
}
void TravelPlan(float C ) // Function 2
{
cout<<PlaceCode<<“:”<<Place<<”:”<<Charges<<endl;
}
~Travel( ) // Function 3
{
cout<<”Travel Plan Cancelled”<<endl;
}
Travel(int PC,char p[],float C) // Function 4
{
PlaceCode=PC;strcpy(Place,P);Charges=c;
}(i) In Object Oriented Programming, what are Function 1 and Function 4 combined together referred as?
(ii) In Object Oriented Programming, which concept is illustrated by Function 3? When is this function called/invoked?
(i) Polymorphism or Constructor Overloading
(ii) Function 3: Destructor
A destructor called/invoked when an object of that class is destroyed. When a variable goes out of scope, or a dynamically allocated variable is explicitly deleted using the delete keyword, the class destructor is called to help clean up the class before it is removed from memory.
Sponsor Area
Mock Test Series
Mock Test Series



