Object Oriented Programming with C++
Inheritance
Evaluation
PART II
Answer to the all qustions (2 Marks):
1. What is inheritance?
Answer: The mechanism of deriving new class from an existing class is
called inheritance.
2. What is a base class?
Answer: A class that is used as the basis for inheritance is called a
superclass or base class.
3. Why derived class is called power packed class?
Answer: The derived class is a power packed class, as it can add
additional attributes and methods of other classes and thus enhance its
functionality.
4. In what multilevel and multiple inheritance differ though both contains many base class?
Answer: The difference between multiple and multilevel inheritances is
that multiple inheritance is when a class inherits from many base classes while
multilevel inheritance is when a class inherits from a derived class, making
that derived class a base class for a new class. Multilevel inheritance is
widely used than multiple inheritance.
5. What is the difference between public and private visibility mode?
Answer:
(i) Private
visibility mode : When a base class is inherited with
private visibility mode the public and protected members of the base class
become ‘private’ members of the derived class.
(ii) Public
visibility mode : When a base class is inherited
with public visibility mode, the protected members of the base class will be
inherited as protected members of the derived class and the public members of
the base class will be inherited as public members of the derived class.
PART III
Answer to the all questions (3 Marks):
1. What are the points to be noted while deriving a new class?
Answer: While defining a derived class, the derived class should
identify the class from which it is derived. The following points should be
observed for defining the derived class.
(i) The keyword class has to be used
(ii) The name of the derived class is to be given after the
keyword class.
(iii) A single colon
(iv) The type of derivation (the visibility mode), namely
private, public or protected. If no visibility mode is specified, then by
default the visibility mode is considered as private.
(v) The names of all base classes(parent classes) separated by
comma.
2. What is difference between the members present in the private visibility mode and the members present in the public visibility mode
Answer: (i) In publicly derived class, the public members of the base
class remain public and protected members of base class remain protected in
derived class.
(ii) In privately derived class, the public and the protected
members of the base class become protected in derived class.
3. What is the difference between polymorphism and inheritance though are usedfor reusability of code?
Answer: (i) Polymorphism lets you reuse methods by applying them in
similar kind of objects.
(ii) Inheritance lets you reuse and extents class definitions,
creating new class from an existing class.
(iii) Inheritance permits subclasses to reuse the fields and
methods of super classes.
4. What do you mean by overriding?
Answer: When a derived class member function has the same name as that
of its base class member function, the derived class member function
shadows/hides the base class’s inherited function. This situation is called
function overriding.
5. Write some facts about the execution of constructors and destructors in inheritance
Answer: (i) Base class constructors are executed first, before the
derived class constructors execution
(ii) Derived class can not inherit the base class constructor
but it can call the base class constructor by using.
(iv) Base_class name::base_class_constructor() in derived class
definition.
(v) If there are multiple base classes, then its start executing
from the left most base class.
(vi) In multilevel inheritance, the constructors will be
executed in the order of inheritance.
PART IV
Answer to the all questions (5 Marks):
1. Explain the different types of inheritance
Answer: There are different types of inheritance viz., Single
Inheritance, Multiple inheritance, Multilevel inheritance, hybrid inheritance
and hierarchical inheritance.
(i) Single Inheritance : When a derived class inherits only from one base class, it is
known as as single inheritance
(ii) Multiple
Inheritance : When a derived class inherits from
multiple base classes it is known as multiple inheritance
(iii) Hierarchical
inheritance: When more than one derived classes
are created from a single base class, it is known as Hierarchical inheritance.
(iv) Multilevel
Inheritance : The transitive nature of inheritance
is itself reflected by this form of inheritance. When a class is derived from a
class which is a derived class - then it is referred to as multilevel
inheritance.
(v) Hybrid
inheritance : When there is a combination of more than
one type of inheritance, it is known as hybrid inheritance. Hence, it may be a
combination of Multilevel and Multiple inheritance or Hierarchical and
Multilevel inheritance or Hierarchical, Multilevel and Multiple inheritance.
The following diagram represents the different types of
inheritance.
2. Explain the different visibility mode through pictorial representation
Answer: (i)
Visibility Modes : An important feature of
Inheritance is to know which member of the base class will be acquired by the
derived class. This is done by using visibility modes. The accessibility of
base class by the derived class is controlled by visibility modes. The three
visibility modes are private, protected and public. The default visibility mode
is private. Though visibility modes and access specifiers look similar, the
main difference between them is Access specifiers control the accessibility of
the members with in the class where as visibility modes control the access of
inherited members with in the class.
(ii) Private
Visibility Mode : When a base class is inherited with
private visibility mode the public and protected members of the base class
become ‘private’ members of the derived class.
(iii) Protected
Visibility Mode : When a base class is inherited with
protected visibility mode the protected and public members of the base class
become 'protected members’ of the derived class.
(iv) Public
Visibility Mode : When a base class is inherited with
public visibility mode, the protected members of the base class will be
inherited as protected members of the derived class and the public members of
the base class will be inherited as public members of the derived class.
3.
#include<iostream>
#include<string.h>
#include<stdio.h>
using name spacestd;
class publisher
{
char pname[15];
char hoffice[15];
char address[25];
double turnover;
protected:
char phone[3][1O];
void register();
public:
publisher();
~publisher();
void enter data();
void disp data();
};
class branch
{
charbcity[15];
char baddress[25];
protected:
intno_of_emp;
public:
charbphone[2][10];
branch();
~branch();
void have data();
void give data();
};
class author: public branch, publisher
{
intaut_code;
charaname[2O];
float income;
public:
author();
~author();
voidgetdata();
voidputdata();
};
Answer the following questions based on the above given program:
3.1. Which type of Inheritance is shown in the program?
Answer: Multiple
Inheritance.
3.2. Specify the visibility mode of base classes.
Answer: Public.
3.3 Give the sequence of Constructor/Destructor Invocation when object of class author is created.
Answer: Constructor
Order :
(i) Constructor of base class publisher.
(ii) Constructor of base class branch.
(iii) Constructor of derived class author.
Destructor Order :
(i) Destructor of derived class author.
(ii) Destructor of base class branch.
(iii) Destructor of base class publisher.
3.4. Name the base class(/es) and derived class (/es).
Answer: Base
Classes :
(i) Publisher
(ii) Branch
Derived Class : Author.
3.5 Give number of bytes to be occupied by the object of the following class:
(a) publisher (b) branch (c) author
Answer:
(a) 94 bytes
(b) 64 bytes
(c) 28 bytes.
3.6. Write the names of data members accessible from the object of class author.
Answer: Data
members accessed:
(i) Phone
(ii) no_of_emp
(iii) out-code
(iv) aname
(v) income.
3.7. Write the names of all member functions accessible from the object of class author.
Answer: Member
functions accessed:
(i) register ( );
(ii) enterdata ( );
(iii) have data ( );
(iv) give data ( );
(v) get data ( );
(vi) put data ( ) ;
3.8 Write the names of all members accessible from member functionsof class author.
Answer: getdata ( ) and putdata ( ) member can access the following
members, phone, no_of_emp, out-code, aname, income register ( ), enterdata (),
dispdata (), havedata ( ) and givedata( ).
4. Consider the following c++ code and answer the questions
class Personal
{
int Class,Rno;
char Section;
protected:
char Name[20];
public:
personal();
void pentry();
voidPdisplay();
};
class Marks:private Personal
{
float M{5};
protected:
char Grade[5];
public:
Marks();
void M entry();
void M display();
};
class Result:public Marks
{
float Total,Agg;
public:
char FinalGrade, Commence[20];
Result();
void R calculate();
void R display();
}:
4.1. Which type of Inheritance is shown in the program?
Answer: Multiple
Inheritance.
4.2. Specify the visibility mode of base classes.
Answer: Private,
Public.
4.3. Give the sequence of Constructor/Destructor Invocation when object of class Result is created.
Answer: Constructors
:
(i) Personal ( )
(ii) Marks ( )
(iii) Result ( )
Destructor :
(i) Result ( )
(ii) Marks ( )
(iii) Personal ( )
4.4. Name the base class(/es) and derived class (/es).
Answer: Base
Classes : Personal
Derived Classes : Marks, Result.
4.5. Give number of bytes to be occupied by the object of the following class:
(a) Personal (b) Marks (c) Result
Answer: (a) 25 bytes
(b) 25 bytes
(c) 29 bytes.
4.6. Write the names of data members accessible from the object of class Result.
Answer: Total,
Agg, Grade, Final Grade, Commence.
4.7. Write the names of all member functions accessible from the object of class Result.
Answer: RCalculate
( ), Rdisplay ( ), Mentry ( ), Mdisplay () Pentry (), Pdisplay ( ).
4.8 Write the names of all members accessible from member functionsof class Result.
Answer: Total,
Agg, Grade, final grade, commence Rcalculate (), Rdisplay (), Mentry (),
Mdisplay (), Pentry (), Pdisplay ().
5. Write the output of the following program
#include<iostream>
using namespace std;
class A
{
protected:
int x;
public:
void show()
{
cout<<"x = "<<x<<endl;
}
A()
{
cout<<endl<<" I am class A "<<endl;
}
~A()
{
cout<<endl<<" Bye ";
}
};
class B : public A
{
{
protected:
int y;
public:
B(int x, int y)
{
this->x = x; //this -> is used to denote the objects datamember
this->y = y; //this -> is used to denote the objects datamember
}
B()
{
cout<<endl<<" I am class B "<<endl;
}
~B()
{
cout<<endl<<" Bye ";
}
void show()
{
cout<<"x = "<<x<<endl;
cout<<"y = "<<y<<endl;
}
};
int main()
{
objA;
objB(30, 20);
objB.show();
return 0;
}
Answer:
I am class A
I am class B
x =30
y = 20
Bye
Bye
6. Debug the following program
Output
-------------
15
14
13
Program :
-------------
%include(iostream.h)
#include<conio.h>
Class A
{
public;
int a1,a2:a3;
Void getdata[]
{
a1=15;
a2=13;a3=13;
}
}
Class B:: public A()
{
PUBLIC
voidfunc()
{
int b1:b2:b3;
A::getdata[];
b1=a1;
b2=a2;
a3=a3;
cout<<b1<<’\t’<<b2<<’t\’<<b3;
}
void main()
{
clrscr()
der;
der1:func();
getch();
}
Answer:
(i) #include<iostream.h>
(ii) class A
(iii) void getdata()
(iv) a2 = 14;
(v) };
(vi) class B: public A
(vii) public :
(viii) void func()
(ix) int bl,b2, b3;
(x) A:: getdata ( );
cant <<b1>> ' 1t' <<b2>>' 1t'<<b3;
};
(xi) clrscr;
(xii) B derl;
(xii) derl. func ();
CASE STUDY
All the banks operating in India are controlled by RBI. RBI has set certain guidelines (e.g. minimum interest rate, minimum balance allowed, maximum withdrawal limit etc) for all banks to follow. For example, suppose RBI has set minimum interest rate applicable to a saving bank account to be 5% annually; however, banks are free to use 5% interest rate or to set any rates above it.
Write a program to implement bank functionality in the above scenario. Note: Create few classes namely Customer, Account, RBI (Base Class) and few derived classes (SBI, ICICI, PNB,IOBetc). Assume and implement required member variables and functions in each class.
Hint:
class Customer
{
//Personal Details ...
//Few functions ...
}
class Account
{
// Account Detail ...
//Few functions ...
}
}
class RBI
{
Customer c; //hasA relationship
Account a; //hasA relationship
..
Public double GetInterestRate() { }
Public double GetWithdrawalLimit() { }
}
class SBI: public RBI
{
//Use RBI functionality or define own functionality.
}
class ICICI: public RBI
{
//Use RBI functionality or define own functionality.
}
class IOB: public RBI
{
//Use RBI functionality or define own functionality.
}
CASE STUDY 2
Write a class for a class Stock
1. − Each Stock has a data member which holds the net price, and a constructor which sets this price.
- Each Stock has a method get_Price(), which calculates and returns the gross price (the gross price includes VAT at 21%)
2. Write 2 classes which inherit from the general Stock class, of type Notebook and Book
-The gross price for Notebook includestax at 21%,
-Books are free of tax, so the gross price is unchanged from the net price, and you will need to re-define the getGrossPrice method in this class
3. Write a program which does the following:
a. Declare an array of 10 objects to Stock
b. Declare aobject to a Book and an object to Notebook
c. Ask the user to enter details of the book, and of the Notebook item,.
d. Check your method getGrossPrice works correctly with each type and then displaythe result
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.