Inheritance (Extending Classes)

  • Question 1
    CBSEENCO12011569

    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()?



    Solution

    (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

    Question 2
    CBSEENCO12011670

    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

    Solution
    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;
    	}
    }
    Question 3
    CBSEENCO12011716

    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.

    Solution

     

    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.
    #include <iostream> 
    class Example
    {
    public:
    	int a;
    	int add(); private:
    	int b;
    };
    	int Example::add()
    {
    	return a+b ;
    }
    void main( )
    {
    	Example ex;
    	ex.a = 10; // OK: because a is public
    	ex.b = 20; // Error: because b is private
    	int sum=ex.add(); // local variable
    	cout << 'Sum of a + b : ' <<
    }
    Output: Error due to access of
    private member
    
    #include <iostream.h> 
    class ExBase
    {
    	protected: int i, j;
    };
    
    class ExDerived : public ExBase { public:
    	void show()
    	{
    		i=35; j=45;
    		//both i & j are accessible here cout<<'Value of i '<<i; cout<<'Value of j '<<j;
    	}
    };
    void main()
    {
    	ExDerived exd; exd.show();//both I and j are not accessible exd.i=50;
    	exd.j=60;
    }
    Question 4
    CBSEENCO12011717

    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?

    Solution

    (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

    Sponsor Area

    NCERT Book Store

    NCERT Sample Papers

    Entrance Exams Preparation