8.1

Identify the error in the following program.

#include <iostream>;
using namespace std;
class Student {
      char* name;
      int rollNumber;
public:
      Student() {
           name = "AlanKay";;
           rollNumber = 1025;
      }
       
       void setNumber(int no) {
            rollNumber = no;
       }
       int getRollNumber() {
           return rollNumber;
      }
};
 
class AnualTest: Student {
          int mark1, mark2;
public:
         AnualTest(int m1, int m2)
                  :mark1(m1), mark2(m2) {
       }
       int getRollNumber() {
           return Student::getRollNumber();
       }
};
void main()
{
     AnualTest test1(92 85);
     cout<< test1.getRollNumber();
}
Answer

Constructor and Private (data & function) can not be inherited.

8.2

Identify the error in the following program.

#include<iostream> ;
using namespace std;
class A 
{
public:
      A()
      {
       
       cout<< "A";
      }
};
 
class B: public A 
{
public:
      B()
       {
             cout<< "B";
       }
};
class C: public B 
{
public:
      C()
       {
             cout << "C";
       }
};
class D
{
public:
      D()
       {
             cout << "D";
       }
};
class E: public C, public D 
{
public:
      E()
       {
             cout<< "D";
       }
};
class F: B, virtual E
{
public:
      F()
       {
             cout<< "F";
       }
};
void main()
{
     F f;
}
Answer

The inheritance can be represented as follows:

Exarcise-1

Here B is virtual, but not E.

8.3

Identify the error in the following program.

#include <iostream>;
using namespace std;
class A
{
     int i;   
};
 
class AB: virtual A
{
          int j;
};
class AC: A, ABAC
{
          int k;
};
class ABAC: AB, AC
{
          int l;
};
void main()
{
     ABAC abac;
     cout << "sizeof ABAC:" << sizeof(abac);
}
Answer

The inheritance can be represented as follows:

Exarcise-2

Class AC: A, Here there is no identification of ABAC. If we write class ABAC; after #include it will not show any error massage.

8.4

Find errors in the following program. State reasons.

// Program test
#include <iostream>
using namespace std;
class X 
{
      private:
          int x1;
      Protected:
         int x2;
      public:
        int x3;    
};
 
class Y: public X
{
         public:
           void f()
           {
                 int y1,y2,y3;
                 y1 = x1;
                 y2 = x2;
                 y3 = x3;
           }
};
class Z: X
{
       public:
           void f()
           {
                 int z1,z2,z3;
                 z1 = x1;
                 z2 = x2;
                 z3 = x3;
           }
};
main()
{
     int m,n,p;
     Y y;
     m = y.x1;
     n = y.x2;
     p = y.x3;
     Z z;
     m = z.x1;
     n = z.x2;
     p = z.x3;
}
Answer

Here x1 is private, so x1 cannot be inherited.
y1 = x1; is not valid
z1 = x1; is not valid
m = y, x1; is not valid
m = z, x1; is not valid

8.5

Debug the following program.

// Test program
#include <iostream>
using namespace std;
class B1
{
      int b1;
      public:
        void display();
        {
               cout << b1 <<"\n";
        }    
};
 
class B2
{
      int b2;
      public:
        void display();
        {
               cout << b2 <<"\n";
        }    
};
class D: public B1, public B2
{
       //nothing here
};
main()
{
    D d;
    d.display()
    d.B1::display();
    d.B2::display();
}
Answer

d.display ( ); show ‘ambiguity error’.

Here display() function should be declared as virtual in B1 class.


Next Previous