Some C++ programming Questions and Solutions

Q1: WAP to demonstrate how ambiguity is avoided in single inheritance using scope resolution operator.

Ans:

#include<iostream.h>

Class A

{

public:

void display()

{

cout<<”An”;

}

};

class B:public A

{

void display()

{

cout<<”Bn”;

}

};

int main()

{

B b;                      //derived class object

b.display();         // invokes display() in B

b.A::display();    // invokes display() in A

b.B::display();    // invokes display() in B

return 0;

}

Output:

B

A

B

 

Q2: WAP to demonstrate how the private data members cannot be accessed by the public member         functions of the derived class even though the derived class has been inherited publicly.

Ans:

#include<iostream.h>

Class B

{

int a;                  // private; not inheritable

public:

int b;               // public; ready for inheritance

void get_ab();

int get_a(void);

void show_a(void);

};

class D: public B         // public derivation

{

int c;

public:

void mul(void);

void display(void);

};

void B:: get_ab(void)

{

a=5; b=10;

}

int B:: get_a()

{

return a;

}

void B:: show_a()

{

cout<<”a=”<<a<<”n”;

}

void D:: mul()

{

c=b*get_a();

}

void D:: display()

{

cout<<”a=”<<get_a()<<”n”;

cout<<”b=”<<b<<”n”;

cout<<”c=”<<c<<”n”;

}

int main()

{

D d;

d.get_ab();

d.mul();

d.show_a();

d.display();

d.b=20;

d.mul();

d.display();

return 0;

}

 

Q3: What do you mean by inheritance in the OOP paradigm? How is direct and indirect base class  declared in CPP?

Ans: The mechanism of deriving a new class from an old one is called inheritance. The old    class is called as base class and the new class is called as derived class. Direct and indirect base class can be declared in the following way.

Class M                      //direct base class

{

protected:

int m;

public:

void get_m(int);

};

class N       // indirect base class derived from the above said base class

{

protected:

int n;

public:

void get_n(int);

};

class B: public M, public N

{

public:

void display(void);

};

void M:: get_m(int x)

{

m=x;

}

void N:: get_n(int y)

{

n=y;

}

void B:: display(void)

{

cout<<”m=”<<m<<”n”;

cout<<”n=”<<n<<”n”;

cout<<”m*n”<<m*n<<”n”;

}

int main()

{

B p;

p.get_m(10);

p.get_n(20);

p.display();

return 0;

}

Output:

m=10

n=20

m*n=200

 

Q4: Explain the relationship among the following terms

a)      Super class

b)      Sub class

c)      Base class

d)     Derived class

Ans:  The derived class is also called sub class and the base class is also called super class.

Super class(grand parent class)àbase class (parent class)àderived class or sub class (child class)

e.g.

 

class A{……};     // super class

class B: public A {……};    // base class derived from the super class

class C: public B {……};    // derived class or sub class

 

 

Q5: Develop an OOP in CPP to read the following information from the keyboard in which the base class consists of

a)       employee name

b)       employee code

c)       designation

d)      years of experience

e)       age

Ans:

 

#include<iostream.h>

#include<conio.h>

class employee

{

private:

char name[50];

char designation[50];

int age,code;

int yrs;

public:

void getdata()

{

cout<<"enter name of employee"<<endl;

cin>>name;

cout<<"enter designation"<<endl;

cin>>designation;

cout<<"enter age"<<endl;

cin>>age;

cout<<"enter code"<<endl;

cin>>code;

cout<<"enter years of experience"<<endl;

cin>>yrs;

}

void display()

{

cout<<"name: "<<name<<endl;

cout<<"designation: "<<designation<<endl;

cout<<"age: "<<age<<endl;

cout<<"code: "<<code<<endl;

cout<<"years of experience: "<<yrs<<endl;

}

};

class B:public employee

{

};

void main()

{

clrscr();

B b;

b.getdata();

b.display();

getch();

}

 

 

Output:

Enter name of employee

Rajiv

Enter designation

Manager

Enter age

22

Enter code

123

Enter years of experience

12

Name: Rajiv

Designation: Manager

Age: 22

Code: 123

Years of experience: 12

 

Q6: Create a following class network

PERSON (NAME, CODE)àACCONT(PAY)

PERSON (NAME, CODE)àADMIN(EXPERIENCE)

ACCOUNT (PAY), ADMIN (EXPERIENCE)àPERSON(NAME,CODE,EXPERIENCE,PAY)

Q7: Debug the following and state reason.

a)

#include<iostream.h>

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;

getch();

}

 

Ans:

class X

{

private:

int x1;

b)

#include<iostream.h>

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;

}

};

void 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;

getch();

}

 

Leave a reply