The mechanism of deriving a new class from an old one is called inheritance. The old class is referred to as the base class and the new one is called derived class.
8.2 What are the different forms of inheritance? Give an example for each.
Answer:
1. Single inheritance: Only one derived class inherited from only one base class is called single inheritance. Example:
classA{......};classB:publicA{......};
2. Multiple inheritance: a class can inherit the attributes of two or more classes. Example: class D: visibility B1, B2 and B3.(visibility may be public or private)
{(body of D)}
3. Multilevel inheritance: a class is derived from a base class, and another class is derived from this derived class and so on, then it’s called multilevel inheritance. Example:
8.3 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:
Yes. It’s possible for the objects of derived class to access the private member of the base class by a member function of base class which is public. Example:
#include<iostream>usingnamespace std;classB{private:int a;public:intget_a();voidset_a();};classD:publicB{private:int b;public:voiddisplay_a();};voidD::display_a(){
cout<<" a = "<<get_a()<<'\n';}voidB::set_a(){
a =383;}intB::get_a(){return a;}intmain(){
D d;
d.set_a();
d.display_a();return0;}
8.4 When do we use the protected visibility specifier to a class member?
Answer:
When we want access a data member by the member function within its class and by the member functions immediately derived from it, then we use visibility modifier protected.
8.5 When do we make a class virtual?
Answer:
To avoid the douplication of inherited members due to multiple paths between base and derived classes we make base class virtual.
8.6 In what order are the class constructors called when a derived class object is created?
Answer:
According to the order of derived class header lines.
8.7 Class D is derived from class B. The class D does not contain any data members of its own. Does the class D required constructors? If yes, why?
Answer:
D doesn’t required any constructor or because a default constructor is always set to class by default.
8.8 What is containership? How does it differ from inheritance?
Answer:
Containership is another word for composition. That’s the “has-a” relationship where A “has-a” member that is an object of class B.
Difference: Inheritance the derived class inherits the member data and function from the base class and can manipulatebase public/protected member data as tis own data. By default a program which constructs a derived class can directly access the public members of the base class as well. The derived class can be safely down cast to the base class, because the derived “is-a” base class.
Container: a class contain another object as member data. The class which contains the object cannot access any protected or private members of the contained class (unless the container it was made a friend in the definition of the contained class). The relationship between the container and the contained object is “has-a” instead of “is-a”.
8.9 Describe how an object of a class that contains objects of other classes created?
Answer:
By inheriting an object can be create that contains the objects of other class. Example:
classA{private:int a;public:voiddoSomething();};classB:classA{private:int b;public:voiddoNothing();};/* Now if object of B is created, then it contains:
1. void doSomething();
2. int b;
3. void doNothing(); */
8.10 State whether the following statements are TRUE or FALSE:
a. Inheritance helps in making a general class into a more specifuc class.
b. Inheritance aids data hiding.
c. One of the advantages of inheritance is that it provides a conceptual framework.
d. Inheritance facilitates the creations of class libraries.
e. Defining a derived class requires some changes in the base class.
f. A base class is never used to create object.
g. It’s legal to have an object of one class as a member of another class.
h. We can prevent the inheritance of all members of the base class by making base class virtual in the definition of the derived class.
d.display(); -----> error: request for member ‘display’ is ambiguous.
Programming Exercises:
8.1 Assume that a bank maintains two kinds of customer accounts , one called savings and the other as current accounts. The savings account provides compound interest and withdrawal facilities but no chequebook facility. The current account provides a chequebook facility but no interest. Current account holders should also maintain a minimum balance and if the balance falls below this level a service charge is imposed.
create a class account that stores customer name, account number and type of account. From this derive the classes cur_acct and sav_acct to make them more specific to their requirements. Include necessary member functions in order to achieve the following tasks:
a. Accept the deposit from a customer and update the balance.
b. Display the balance.
c. Compute and deposit interest.
d. Permit withdraw and update the balance.
e. Check for the minimum balance, impose a penalty, necessary and update the balance.
Do not use any constructors. Use member functions to initialize class members.
#include<iostream>#include<cmath>#include<cstring>usingnamespace std;#defineminimum500#defineservice_charge100#definerate0.15classaccount{private://protectedchar name[100];int acc_number;char acc_type[100];public:voidcreate(char*t);};void account::create(char*t){
cout <<" Enter customer name : ";gets(name);strcpy(acc_type, t);
cout <<" Enter account number : ";
cin >> acc_number;}// current accountclasscur_account:publicaccount{private:float balance;public:voiddeposit(float d);voidwithdraw(float w);voiddisplay();};void cur_account::deposit(float d){
balance = d;}void cur_account::withdraw(float w){if(balance < w)
cout <<" Sorry, your balance is less than your withdrawal amount \n";else{
balance -= w;if(balance < minimum){
cout <<"\n your current balance is : "<< balance
<<" Which is less than Minimum"<< minimum
<<" your account is discharged by : "<< service_charge;int test;
cin >> test;if(test ==0)
balance += w;}}}void cur_account::display(){
cout <<"\n Now Balance = "<< balance <<endl;}// Saving accountclasssav_account:publicaccount{private:float balance;int d, m, y;public:voiddeposit(float d);voidwithdraw(float w);voiddisplay();voidinterest();voidsetDate(int a,int b,int c){d=a;m=b;y=c;}};void sav_account::deposit(float d){int x, y, z;
cout <<" Enter today's date (day,month,year) : ";
cin >> x >> y >> z;setDate(x,y,z);
balance = d;}void sav_account::withdraw(float w){if(balance < w)
cout <<" Sorry, your balance is less than your withdrawal amount \n";else{
balance -= w;if(balance < minimum){
cout <<"\n your current balance is : "<< balance
<<" Which is less than Minimum"<< minimum
<<" your account is discharged by : "<< service_charge;int test;
cin >> test;if(test ==0)
balance += w;}}}void sav_account::display(){
cout <<" \n Now Balance = "<< balance;}// 利息按照每月30, 每年365 天计算void sav_account::interest(){int d1, m1, y1;
cout <<" Enter today's date (day,month,year) : ";
cin >> d1>>m1>>y1;float dur_year;int dur_day, dur_mon;
dur_day = d1 - d;
dur_mon = m1 - m;
dur_year =float((dur_day + dur_mon*30)/365.0+(y1 - y));
cout <<"dur_day = "<< dur_day <<",dur_mon = "<< dur_mon <<",dur_year = "<< dur_year<<endl;float intrst;
intrst = balance * rate * dur_year;// 利息 = 本金 * 利息 * 存期
cout <<"Interest is : "<< intrst << endl;// update balance
balance += intrst;}intmain(){
sav_account sav;
sav.create("Saving");float d;
cout <<" Enter your deposit amount : ";
cin >>d;
sav.deposit(d);
sav.display();int t;
cout <<"\n press '1' to see your interest : \n"<<" press '0' to skip : ";
cin >> t;if(t ==1)
sav.interest();
cout <<"\n Enter your withdrawal amount : ";float w;
cin >> w;
sav.withdraw(w);
sav.display();return0;}
8.2 An educational institution wishes to maintain a database of its employees. The database is divided into a number of classes whose hierarchical relationships are shown in the following figure. The figure also shows the minimum information required for each class. Specify all classes and define functions to create the database and retrieve individual information as and when required.