PART A
Q1. In what order are the class constructors called when a derived class object is
created?
If the base class has constructors that are to be called then according
to the Syntax shown below:
Derived_class _name():Base_class_name()
We can call the base class constructors as well.Therefore first the base
class constructor is executed and then the derived class constructor is
called.
#include<iostream.h>
#include<conio.h>
class A
int x;
public:
A(int i)
x=i;
cout<<"\nA is initialized";
void show_x()
{
cout<<"\nx="<<x;
};
class B
float y;
public:
B( float j)
y=j;
cout<<"\nB is initialized";
void show_y()
cout<<"\ny="<<y;
};
int m,n;
public:
m=c;
n=d;
void show_z()
cout<<"\nm="<<m;
cout<<"\nn="<<n;
};
void main()
clrscr();
C c(4,5,6,7);
c.show_x();
c.show_y();
c.show_z();
getch();
b) Class D is derived from class B. The class D does not contain any data members
If a base class does not have any constructor then derived class need
not have a constructor function. If a base class contains any
constructor function with any argument then it is mandatory for the
derived class to have a constructor and pass the arguments to the
base class constructor when both classes have constructor then base
class will execute firstly and then derived class will be executed.
Q2. We know that a private member of a base class is not inheritable. Is it anyway
possible for the objects of a derived class to access the private members of the base
class? If yes,how? Remember the base class cannot be modified.
ANSWER:
Thus ,if we want to access the private members of the base class
which cannot be inherited ,we need to call the member function of the
base call through the member function the derived class as shown in
the example below:
Example: 1
#include<iostream.h>
#include<conio.h>
Class A
int a;
public:
void show()
a=10;
};
Class B:public A
{
public:
void aces( )
A::showdata( );
};
void main()
clrscr( );
B obj;
obj.aces( );
getch( );
Q3. Assume that a publishing company prints books and digital books(electronic
form- CD). Create a class named publication with data members title, price, and
author’s name. From publication class, derive two classes named book and ebook. Te
book class adds a page count data member named pcount while ebook adds data
member playing time name ptime.Each of these classes must have member function
getdata( ) to read class specific data from keyboard and displaydata( ) to output the
class specific data to the computer screen.
#include<iostream.h>
#include<conio.h>
class publication
int price;
char n[10];
char t[20];
public:
void getdata()
cin>>n>>t>>p;
Void display()
};
private:
Int pcount;
public:
void getdata()
cin>>pcount;
void displaydata()
display();
cout<<”the pcount is”<<pcount;
};
private:
int ptime;
public:
void getdata();
cin>>ptime;
void displaydata()
};
void main()
book b;
ebook e;
b.getdata();
e.getdata();
b.displaydata();
e.displaydata();
getch();
PART B
Ex.
class A
int x;
public:
void show()
};
class B:public A
int y;
public:
void show();
};
since the function show() is same in both the classes therefore static
binding does not apply. Therefore member function could be selected
while the program is running.
(b)
When we use the same function name in both the base class and
derived class, the function in base class is declared as virtual using the
keyword virtual. When the function is made virtual then C++
determines which function is to use at runtime, based on the type of
object pointed to by the base pointer. Thus by making the base pointer
to point different objects we use different versions of virtual functions.
#include<iostream.h>
#include<conio.h>
class Base
public:
};
public:
void getdata()
cout<<"\nDrive class";
}
void showdata()
cout<<"\nDisplay derive";
};
void main()
clrscr();
Drive d;
Drive *p;
p=&d;
p->getdata();
p->showdata();
getch();
Q5. When do we make a virtual function “pure”? What are the implications of
making a
ANS 5
We make the virtual function pure because compiler requires the each
derived class to either define the function or redeclare it as a pure
virtual function. A class containing the pure virtual function cannot use
to declare the object of its own.
Example :
#include<iostream.h>
#include<conio.h>
class Base
public:
};
public:
void getdata()
cout<<"\nDrive class";
void showdata()
cout<<"\nDisplay derive";
};
void main()
clrscr();
Drive d;
Drive *p;
p=&d;
p->getdata();
p->showdata();
getch();
}
Q6. Develop an object oriented program in C++ to read the following information
from the keyword in which the base class consists of employee name, code and
designation, and the derived class containing the data members, viz. years of
experience and age.
a) Employee name
b) Employee code
c) Designation
d) Years of experience
e) Age
ual base class for the item employee name and code.
Design a virt Design a virtual base class for the item employee
name and code.
ANS 6
#include<iostream.h>
#include<conio.h>
Class A
int emp_code;
char emp_name[10];
char desig;
public:
void getdata()
cin>>n
cin>>c
cin>>d;
void display()
};
int a;
int y;
public:
void getdata()
A::getdata();
cin>>a
cin>>y;
void display1()
A::display();
};
void main()
B obj;
Obj.getdata1();
Obj.display1();
getch();