This document discusses hierarchical inheritance in object-oriented programming. Hierarchical inheritance occurs when multiple subclasses inherit from a single base class. The example shows class NO_AB and NO_AC both inheriting from base class NO_A, making NO_A the base class of both subclasses. Private members of the base class cannot be accessed by the derived subclasses, while protected and public members are inherited.
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
1. INHERITANCE IS ONE OF THE CORNERSTONE
OF OOP BECAUSE IT ALLOWS THE CREATION
OF CLASS THAT DEFINE TRAITS COMMON TO A
SET OF RELATED ITEMS. THIS CLASS THEN MAY
BE INHERITED BY OTHER MORE SPECIFIC
CLASSES, EACH ADDING ONLY THOSE THINGS
THAT ARE UNIQUE TO THE INHERITING CLASS.
THE CLASS THAT IS INHERETED IS REFERED TO
AS A BASE CLASS. THE CLASS THAT DOES
INHERITING IS CALLED THE DERIVED CLASS. IN
THIS WAY MULTIPLE INHERITANCE IS ACHIVED.
CLICK HERE
2. Inheritance is the capability of one class to inherit properties from another class.
A class from which another class is inheriting its properties is called base class and
the class inheriting properties is known as a sub-class or derived class. Ones a base
class is written and debugged, it can be used in various situations without having to
redefine it or rewrite it. Actually a derived class inherits all the members of a base
class, the derived class has access privilege only to the non-private members of the
base class.
NEED FOR INHERITANCE.
1. The Capability to express the inheritance relationship which ensures the
closeness with the real-world models.
2. Reusability. Once a base class is written it can be used in various situations. It
gives some advantages like faster development time, easier maintenance, and
easy to extend. Inheritance allows the addition of additional features to an
existing class without modifying it.
3. Transitive nature of Inheritance. Inheritance is transitive. If a class B inherits
properties of another class A, then all sub-class of B will automatically inherit the
properties of A. This property is called transitive nature of inheritance.
CLICK HERE
CLICK HERE
CLICK HERE
CLICK HERE
CLICK HERE
3. class derived-class-name : visibility-mode base-class-name
{
Members of derived class.
};
The base class name
followed with colon (:).
Access specification of inheritable
members of base class. It controls
access to inheritable members from
outside of derived class. Visibility
Mode must be either private,
protected or public.
class A
{
};
class B : public A
{
};
A derived class
is defined by
specifying its
relationship with
the Base Class
using Colon (:).
4. class A
{
Properties that are inherited to Child
Class.
};
class B : A
{
};
Properties Inherit from Base
Calss A
Addition of additional features
of Class B.
The base class follow colon (:). It means
‘Inherit From’. i.e. Class B is Inherit From
A. class B : A
BASE CLASS A.
A Class that define traits
common to child classes is
called BASE CLASS. A class
from which another class is
inheriting its properties is called
Base Class.
DERIVED CLASS B
A derived class inherits all
members of a base class,
however the derived class has
access privilege only to non-
private members of base
class.
Traits
common to
Child Classes.
Child Class will contain inherited
properties of Base class in addition
to its own additional features. The
Object of Child class will show all
features of both Base class and
Child class.
5. SCOPE OF CLASS MEMBERS. Access to class members from other part of the
program is called SCOPE. Scope of class members is specified by using ACCESS
SPECIFIERS. It can be either public, private, or protected. private
Private members can only be used by member functions and friends of the class
in which it is declared. It can’t access from derived classes. Members of a class
are private by default.
Public members are public, it can be used from anywhere in the program.
Protected members are same as private. In addition, It can be also used by the
derived class. Derived class members and friends of derived class can access
protected members. In other words, ‘protected’ members are protected from
unauthorized part in the program. i.e the Scope of protected members is
restricted within the class and derived class.
6. Class BIO_GROUP inherits from ‘class student’.
Properties of ‘class student’ will be copied into
derived class BIO_GROUP. But private members of
Base class can’t be access from Derived Class.
# include <iostream.h>
class student
{
int roll_no, phy_mark, che_mark, math_mark;
char name[20];
public:
};
main()
{
a.input(); b.input();
a.out(); b.out();
}
class BIO_GROUP : student
{
int bio_mark, tot;
public:
};
void out()
{
cout << "Name : " << name << " Roll No " << roll_no;
cout << "Marks of Physics " << phy_mark << " Maths “
<< math_mark << "Chemistry “ << che_mark<<;
}
void input()
{
cout << “nnPls Enter Name & Roll No";
cin >> name >> roll_no;
cout << "Enter marks of Physics, Chemistry and Maths";
cin >> phy_mark >> che_mark >> math_mark;
}
void input()
{
student :: input();
cout << "nnEnter Biology Mark ";
cin >> bio_mark;
}
void out()
{
student :: out();
cout << " Biology Mark " <<
bio_mark;
}
student a; //Click Me
BIO_GROUP b; //Click Me
DATA &
FUNCTIONS
common to All
GROUP.
Click Me
7. # include <iostream.h>
class student {
int roll_no, phy_mark, che_mark, math_mark;
char name[20];
public:
void out() {
cout << "nnName : " << name << " Roll No " << roll_no;
cout << "Marks of Physics " << phy_mark << "Chemistry "
<< che_mark<< " Maths " << math_mark; }
void input() {
cout << “nnPls Enter Name & Roll No";
cin >> name >> roll_no;
cout << "Enter marks of Physics, Chemistry and Maths";
cin >> phy_mark >> che_mark >> math_mark; }
};
main()
{
student a;
BIO_GROUP b;
COMP_GROUP c;
a.input(); b.input(); c.input();
a.out(); b.out(); c.out();
}
class BIO_GROUP : student {
int bio_mark, tot;
public:
void input() {
student :: input();
cout << "Enter Bio Mark "; cin >> bio_mark; }
void out() {
student :: out();
cout << " Biology Mark " << bio_mark; }
};
class COMP_GROUP : student
{
int comp_mark, tot;
public:
void out()
{
student :: out();
cout<<"Computer Mark " <<
comp_mark;
}
void input()
{
student :: input();
cout << "Computer Mark ";
cin >> comp_mark;
}
};
Click Me
a
Click Me
b
Click Me
c
Flow of Execution.
Creating object of student
invoke student :: input()
input name & marks Back
to main() Invoke student ::
out() Print name & other
details. Back to main() and
STOP EXECUTION.
Flow of Execution.
Creating object of BIO_GROUP
invoke BIO_GROUP :: input () invoke
student :: input() input name & marks
Back to BIO_GROUP :: input()
Read Biology Mark Back to main()
Invoke BIO_GROUP :: out() invoke
student :: out() Print name & other
details. Back to BIO_GROUP :: out()
& Print bio_mark Back to main() and
STOP EXECUTION.
Creating object of COMP_GROUP invoke
COMP_GROUP :: input () invoke student :: input()
input name & marks Back to COMP_GROUP::input()
Read Computer Mark Back to main() Invoke
COMP_GROUP::out() invoke student::out() Print
name & other details. Back to COMP_GROUP::out()
& Print comp_mark Back to main() and STOP.
8. # include <iostream.h>
class student
{
private:
int roll_no, phy_mark, che_mark, math_mark;
char name[20];
public:
void out()
{ cout << "nnName : " << name << " Roll No “<< roll_no << "Marks of Physics "<< phy_mark
<< "Chemistry “ << che_mark << "Maths" << math_mark; }
void input()
{ cout << “nnPls Enter Name & Roll No"; cin >> name >> roll_no;
cout << "Enter marks of Physics, Chemistry and Maths"; cin >> phy_mark >> che_mark >>math_mark; }
};
class BIO_GROUP : student
{
int bio_mark, tot;
public:
void input() { student :: input(); cout << "Enter Bio Mark "; cin >> bio_mark; }
void out()
{
student :: out(); cout << " Biology Mark " << bio_mark;
tot = bio_mark + phy_mark + che_mark + math_mark;
}
};
main() { BIO_GROUP obj; obj.input(); obj.out(); }
Access to Private members are
restricted only within the class. It can’t
access from derived classes.
phy_mark, che_mark, math_mark etc
are not accessible from derived class
BIO_GROUP.
ERROR !
Private Members are
not accessible from
derived class.
9. # include <iostream.h>
class student
{
private:
int roll_no; char name[20];
protected:
Int phy_mark, che_mark, math_mark;
public:
void out()
{ cout << "nnName : " << name << " Roll No “<< roll_no << "Marks of Physics "<< phy_mark
<< "Chemistry “ << che_mark << "Maths" << math_mark; }
void input()
{ cout << “nnPls Enter Name & Roll No"; cin >> name >> roll_no;
cout << "Enter marks of Physics, Chemistry and Maths"; cin >> phy_mark >> che_mark >>math_mark; }
};
class BIO_GROUP : student
{
int bio_mark, tot;
public:
void input() { student :: input(); cout << "Enter Bio Mark "; cin >> bio_mark; }
void out()
{
student :: out(); cout << " Biology Mark " << bio_mark;
tot = bio_mark + phy_mark + che_mark + math_mark;
}
};
main() { BIO_GROUP obj; obj.input(); obj.out(); }
Make it to be ‘protected’ inorder to
access phy_mark, che_mark,
math_mark from derived class
BIO_GROUP.
Correct !
Protected Members are
accessible from derived
class.
No need to access name & roll_no from derived
class. So it remains ‘private’.
10. THERE ARE 5
DIFFERENT
FORMS OF
INHERITANCE
.
1. SINGLE INHERITANCE
2. MULTIPLE INHERITANCE
3. HIEARACHICAL INHETANCE
4. MULTI LEVEL INHERITANCE
5. HYBRID INHERITANCE
The relationship between a base
and derived class is referred to as
a derivation or inheritance
hierarchy. A derivation from
multiple base classes is referred
to as a derivation or inheritance
graph.
11. 1. SINGLE INHERITANCE.
When a sub-class is inherit from only one base class, it is known as single
inheritance.
class A
{
};
class B : A
{
};
If no Visibility Mode
Specified, then default mode
is ‘private’
Base Class ‘A’
Child/Derived/Sub Class of
Base Class ‘A’
The access of private members of class is restricted within that class. It can’t access by
others. private members of Base class are not accessible from derived class. Protected &
public members are accessible from derived class. Access to protected members is restricted
within class and other class derived from this classes.
DIAGRAM
A
B
Members will be inherited.
12. # include <iostream.h>
void main()
{
NO_AB obj;
obj.add();
cout << “nnSize of the object is “ << sizeof(obj);
}
class NO_A
{
protected :
Int a;
};
class NO_AB : NO_A
{
int b;
void add()
{
cout << “Enter values of a and b”;
cin >> a >> b;
cout << “nnSum is “ << a+b ;
}
};
Private members can not be accessed
from derived class. But protected &
public members are accessible.
protected & public
members are
inheritable to
derived class.
Derived class can
access all non-
private members
of its Base Class.
‘int a’ is
inherited from
class NO_A
int a;
OUTPUT
Enter values of a and b 10 20
Sum is 30.
Size of the object is 4
NO_A :: a (2bytes) +
NO_AB :: b(2bytes)
= 4 bytes
Click Me
NO_A :: a + NO_B :: b
13. # include <iostream.h>
class AP
{
};
main()
{
AP a;
GP b;
a.input(); a.generate();
b.input(); b.generate();
}
protected:
int a, n, c; // These are Common to
public: // Both AP & GP classes. So
void input() // these will be inherit into GP.
{
cout << "Enter Starting No & No of Terms”;
cin >> a >> n;
cout << “Enter Common Diff/Ratio ";
cin >> c;
}
void generate()
{
cout << “AP Series is “;
for(int i = 0; i<n; i++)
{ cout << a << ", "; a+=c; }
}
class GP : public AP
{
public:
void generate()
{
cout << “GP Series is “;
for(int i = 0; i<n; i++)
{ cout << a << ", "; a*=c; }
}
};
Override base
class version of
‘generate()’ with
this definition.
Because
calculation is
diifferent.
Click Me
Define a class for generating AP series. It contains starting element ‘a’, Common
Difference / Ratio ‘c’ and No of Terms ‘n’. It also contains function for input data. We
can be derive a new class GP to generate GP series by inherit data a, c, n and input().
And over-ride base class version of generate() with a new one.
14. 2. MULTIPLE INHERITANCE.
When a sub-class is inherit from multiple base class, it is known as Multiple
inheritance.
class C : A , B
{
…… // Definition of Derived Class ‘C’
};
DIAGRAM
A B
class A
{
…… // Definition of Base Class ‘A’
}
class B
{
…… // Definition of Base Class ‘B’
}
C
To inherit more than one base class, use a
comma-separated list.. Also can specify
visibility mode for each class.
Classes A & B are Base class of ‘C’. All features of both class A & B will be inherited
into Derived Class ‘C’. However class ‘C’ can access only Non-Private members of
class ‘A’ & ‘B’.
15. # include <iostream.h>
class NO_A
{
protected :
Int a;
};
class NO_ABC : NO_A, NO_B
{
int c;
void add()
{
cout << “Enter values of A and B & C”;
cin >> a >> b >> c;
cout << “nnSum is “ << a+b+c ;
}
};
class NO_B
{
protected :
Int b;
};
void main()
{
NO_ABC obj;
obj.add();
cout<<“Size of the object is “<<sizeof (obj);
}
OUTPUT
Enter values of A, B & C 10 20 30
Sum is 60
Size of the object is 6
a
b
Click Me
NO_A :: a (2bytes) +
NO_B :: b (2bytes) +
NO_ABC :: c (2bytes) = 6
To inherit more than one base class, use a list
separated by comma.. Also can specify visibility
mode for each class. Default visibility mode is
private.
class NO_ABC : private A, private B
{
}
NO_A :: a + NO_B :: b +
NO_ABC :: c
16. 3. HIERACHICAL INHERITANCE.
When many sub-class inherit from a single base class, it is known as
Hierarchical inheritance.
class C : A
{
…… // Definition of 2nd Sub-class of ‘A’
};
DIAGRAM
A
B
class A
{
…… // Definition of Base Class ‘A’
};
class B : A
{
…… //Definition of 1st sub-class of ‘A’
};
C
Class B & C inherits from Base class ‘A’. Class ‘A’ is the Base Class of Both the
classes ‘B’ and ‘C”. Private members of the Base Class can not be accessed from
derived class ‘B’ & “C’.
17. # include <iostream.h>
class NO_A
{
protected :
Int a;
};
class NO_AC : NO_A
{
int c;
void add()
{
cout << “Enter values of A and C”;
cin >> a >> c;
cout << “nnSum is “ << a+c ;
}
};
class NO_AB : NO_A
{
Int b;
void add()
{
cout << “Enter values of A and B”;
cin >> a >> b;
cout << “nnSum is “ << a+b ;
}
};
void main()
{
NO_AB ab;
NO_AC ac;
ab.add(); ac.add();
}
OUTPUT
Enter values of A and B 10 20
Sum is 30
Enter values of A and B 30 40
Sum is 70
Click Me
18. 4. MULTILEVEL INHERITANCE.
When a sub-class inherits from a class, that itself ingerits from another class is
known as Hierarchical inheritance.
class C : B
{
…… // Derived Class ‘B’
};
DIAGRAM
A
B
class A
{
…… // Definition of Base Class ‘A’
};
class B : A
{ // Derived Class of ‘A’
…… // It act as Base Class of ’C’.
};
C
Derive the class B from Base class ‘A’. Derive another class ‘C’ from ‘B’. ‘B’ act as a
sub-class of ‘A’ as well as Base Class of ‘C’ at the same time. ‘C’ contains all non-
private features of Both the class '
19. 5. HYBRID INHERITANCE.
Hybrid inheritance combines two or more forms of inheritance. When a sub-
class inherits from multiple base class and all of its base classes inherit from a
single base class, this form of inheritance is known as Hybrid inheritance.
class D : B, C
{
…… // Derived Class ‘B’
};
DIAGRAM
A
class A
{
…… // Definition of Base Class ‘A’
};
class B : A
{ // Derived Class of ‘A’
…… // It act as Base Class of ’D’.
};
C
D
class C : A
{ // Derived Class of ‘A’
…… // It act as Base Class of ’D’.
};
Click Me
When many sub-classes ( B
and C ) inherit from a single
base class (A), it is called
Hierarchical Inheritance.
When a sub-class (D)
inherits from multiple base
class (‘B’ and ‘C’), it is called
Multiple Inheritance.
It is
combination of
Hierarchical
and Multiple
Inheritance. So
it is an example
of Hybrid
Inheritance.
A B C
D
E F
B
20. Visibility Modes. The access status of the base class inside the derived class is
determined by Visibility Mode. The visibility mode must be either public, private or
protected. If no visibility mode is present, the visibility mode is private by default. In all
cases, the base’s private elements remain private to the base class and are not
accessible by members of the derived class.
Visibility Mode is
Inheritable public
member becomes
(In Base Class)
Inheritable protected
member becomes
(In derived Class)
Private
Member of
base class are
not directly
accessible from
derived class.
public: public Protected
protected: protected Protected
private: private Private
The Visibility Modes basically control the access specifier to be for inheritable
members of base class in the derived class. The visibility mode in the definition of
the derived class specifies whether the features of the base class are privately
derived or publicly derived or protected derived.
class derived-class-name : base-class-name
{
Members of derived class.
};
visibility-mode
21. The public Visibility Mode. / The Public Derivation.
The public derivations means that the derived class can access the public and
protected members of the base class but not the private members of the base class.
With ‘public’ visibility mode the public members of the base class become the
public members of the derived class and the protected members of the base
class become the protected members of the derived class.
class A
{
private:
…..
protected:
……
public:
….
};
class B : public A
{
private:
…..
protected:
……
public:
………………
};
the base’s private elements remain
private to the base class and are
not accessible by members of the
derived class.
22. The private Visibility Mode. / The Private Derivation.
The private derivations means that the derived class can access the public and
protected members of the base class privately. With ‘private’ visibility mode the
public and protected members of the base class become private members of
the derived class. That means the inherited members (members of base class) can
be accessed only through member functions of the derived class. Also as the
inherited members become private in the derived class, they can’t be inherited further
if the derived class happens to be the base class of any other class.
class A
{
private:
…..
protected:
……
public:
….
};
class B : private A
{
private:
…..
protected:
……
public:
….
};
the base’s private elements remain
private to the base class and are
not accessible by members of the
derived class.
23. The protected Visibility Mode. / The Protected Derivation.
The protected derivations means that the derived class can access the public and
protected members of the base class protectedly. With ‘protected’ visibility mode
the public and protected members of the base class become the protected
members of the derived class. These members can be inherited further if any
classes are inheriting from the derived class.
class A
{
private:
…..
protected:
……
public:
….
};
class B : protected A
{
private:
…..
protected:
……
public:
….
};
the base’s private elements remain
private to the base class and are
not accessible by members of the
derived class.
24. Inheritance and the Base class. While defining a base class, following things
should be kept in mind.
Members intended to be inherited and the same time intended to be available to
every function, even to non-members, should be declared as public in the base
class.
Members intended to be inherited but not intended to be public should be
declared as protected in the base class.
Members not intended to be inherited should be declared as private.
Accessibility of Base Class Members.
Access Specifier Accessible from
own class.
Accessible from
derived class
(inheritable).
Access from
objects outside
class.
public Yes Yes Yes
protected Yes Yes No
Private Yes No No
The private and protected members of a class can only be accessed by the
member & friend functions of the class. However, protected members can be
inherited, whereas private members cannot be.
25. The Significance of Visibility Modes.
You should derive a class publicly when the derived class to have all the attributes of
the base class, plus some extra attributes that are defined in the derived class.
Deriving publicly is a way of saying ‘is a type of’.
Private inheritance should be used when you want the features of the base class to
be available to the derived class but not to the classes that are derived from this
derived class.
Protected inheritance should be used when the features are required to be hidden
from outside class and at the same time required to be inherit to the sub-classes of
this derived class..
26. Inheritance and Constructors and Destructors. When an object of derived
class is created, the program first calls the base class constructor, then the
constructor for the derived class. Because the constructor for the derived class may
build upon data members of the base class; hence the base class object to be
constructed first. Similarly, when an object expires, the program first calls the derived
class destructor and then the base destructor.
# include <iostream.h>
class A
{
public:
A()
{
cout << “Now Constructing Object of A”;
}
~A()
{
cout << “Now Destructing Object of A”;
}
};
class B : A
{
public:
B()
{
cout << “Now Constructing Object of B”;
}
~B()
{
cout << “Now Destructing Object of B”;
}
};
main()
{
B obj;
};
OUTPUT
Now Constructing Object of A
Now Constructing Object of B
Now Destructing Object of B
Now Destructing Object of A
Click Me
Constructor Destructor
Base Class Constructor. Sub-Class Constructor.
Base Class Destructor.
Base Class Destructor.
27. # include <iostream.h>
# include <string.h> // To use strcpy()
class student
{
int roll_no, phy_mark, che_mark, math_mark;
char name[20];
public:
};
main()
{
}
class BIO_GROUP : student
{
int bio_mark, tot;
public:
};
void out()
{
cout << "Name : " << name << " Roll No " << roll_no;
cout << "Marks of Physics " << phy_mark << " Maths “
<< math_mark << "Chemistry “ << che_mark<<;
}
void input()
{
cout << “nnPls Enter Name & Roll No";
cin >> name >> roll_no;
cout << "Enter marks of Physics, Chemistry and Maths";
cin >> phy_mark >> che_mark >> math_mark;
}
void input()
{
student :: input();
cout << "nnEnter Biology Mark ";
cin >> bio_mark;
}
void out()
{
student :: out();
cout<<"Biology Mark"<<bio_mark;
}
student ( ) // BASE CLASS CONSTRUCTOR.
{
roll_no = phy_mark = che_mark = math_mark = 0;
strcpy(name, ”” );
}
BIO_GROUP() //CONSTRUCTOR OF
{ // DERIVED CLASS
bio_mark = 0;
}
BIO_GROUP a; // CLICK ME
a.input(); a.out(); // CLICK ME
When an object of derived class is created,
the program first calls the base class
constructor, then the constructor for the
derived class. Because the constructor for
the derived class may build upon data
members of the base class; hence the base
class object to be constructed first.
Similarly, when an object expires, the
program first calls the derived class
destructor and then the base destructor.
28. Inheritance and Constructors and Destructors. Whenever the derived class
needs to invoke base class’s constructor or destructor, it can invoke them through
explicit call. Some times the base class constructors require arguments to construct
their objects. It is the responsibility of derived class constructor to pass those
arguments required for the base class constructor. To pass the base class
arguments, the base class constructor being invoked explicitly through mem-
initialization list.
# include <iostream.h>
class NO_A
{
protected:
int a;
public :
};
NO_A(int arg)
{
a = arg;
}
class NO_AB : NO_A
{
int b;
public :
void sum()
{
cout << “nnThe Sum is “ << a + b;
}
};
main()
{
NO_AB obj(6,8); obj.sum();
}
NO_AB(int arg1 , int arg2) : NO_A (arg1)
{
b = arg2;
}
Invoke base class constructor with
arg1
mem-initialization list.
6 8
Base Class and its
Constructor.
29. # include <iostream.h>
class rectangle // BASE CLASS
{
protected:
int length, breadth;
public:
int area()
{
return length*breadth;
}
};
class cube : rectangle // DERIVED CLASS
{
int height;
public:
void volume()
{ cout << "Volume is " << area()*height; }
};
int main()
{
cube a(5,3,6);
a.volume();
}
rectangle(int l = 0, int b = 0)
{
length = l; breadth = b;
}
cube(int l, int b , int h) : rectangle( l, b )
{
height = h;
}
When creating an Object of
class ‘cube’, constructor will
be invoked with 3 arguments
5, 3 and 6.
Constructor of derived class will
accepts 3 values l(5), b(3) and h(6).
And explicitly invoke base class
constructor with first 2 arguments ‘l’
and ‘b’. Finally third argument ‘h’ will
be assigned in ‘height’. i.e length=5,
breadth=3 and height=6.
Base class Constructor is invoked from
mem-initialization list of derived class.
And accepts 2 values ‘l’ and ‘b’. These
arguments will be used to initialize
‘length’ and ‘breadth’ of base class.
Invoke rectangle :: area(). It will
return ‘length*breadth’.
Returns (15) ‘length*breadth’. To
cube :: volume().
OUTPUT
Volume is 90.
5 3 6
5*3=15
30. Inheritance and Access Control. When a class inherits some members from
another class, there must be two way to control the access of inherited members. 1.
Using desired visibility mode. 2. Selectively re-declare Base class Members.
Access-Control in Publicly Derived Class. A class is publicly derived when the
keyword ‘public’ precedes the base class name in the class definition. In this method,
the objects of the base class are able to access public members of the base class as
if they were the public members of their own class. With public derivation, the
protected members of the base class become the protected members of the derived
class. And directly available to member functions and friend functions of the derived
class. Private members of the base class can be access indirectly using the public or
protected member functions of the base class since they have access privilege for
the private members of the base class.
class BIO_GROUP : public student, public Exta_curricular
{
…………….
};
Public Visibility Mode /
Public Derivation.
Base Classes of BIO_GROUP.
It is Multiple Inheritance.
32. Access-Control in Privately Derived Class. A class is privately derived when the
keyword ‘private’ precedes the base class name in the class definition. In this
method, the public and protected members of the base class become private
members of the derived class. The private and protected members of the base class
are directly accessible by the member functions and friends of derived class as they
were the private members of the derived class. Hence the objects of derived class
can not access them directly.
class BIO_GROUP : private student, private Exta_curricular
{
…………….
};
Private Visibility Mode /
Private Derivation.
The derived class inherits all the members of the base class; however the
derived class has the direct access privilege only to the non-private members
of the base class.
Base Classes of BIO_GROUP.
It is Multiple Inheritance.
33. Some Facts about Inheritance. Private members of the base class are not
directly accessible in the derived class. But that does not affects its visibility in the
derived class. The private members are still visible in the derived class, even though
the compiler will not allow direct access to it.
The private members of the base class are visible in the
derived class but they are not directly accessible.
You can’t deny access to certain members of the base class
when inheriting publicly.
You can selectively allow access to some of the base class
members when deriving privately.
A class, that serves only as a base class from which
other classes are derived, but no objects of this base
class type exists, is known as ABSTRACT CLASS.
34. The private members of the base class are visible in the derived class but they are
not directly accessible.
# include <iostream.h>
# include <conio.h>
int Value = 10;
class A
{
int Value;
public:
A() { Value =25; }
void out()
{
cout << "nnThe Value is " << Value;
cout << "nnThe :: Value(Global Value) is "
<< ::Value;
}
};
class B : A
{
public:
void B_out()
{
cout << "nnThe Value is " << Value;
}
};
main()
{
B a;
clrscr();
a.B_out();
getch();
}
Global Variable.
This will hide
Global Variable
‘Value’.
ERROR !.
A :: Value is
visible here, but
not accessible.
A::Value will hide
global variable
‘Value’.
Show
Hiding
Global ‘Value=10
‘A :: Value=25’, B
accessible from de
35. Access can only be changed to public or protected
Derived class can modify the access rights of a base class member, but
only to public or protected.
A base class member can't be made private.
When a base class is inherited as private or protected, all public and protected
members of base class become private/protected members of derived class.
You can restore its original access specification by re-declare base class
members in the public/protected section of derived class.
We can not grant or reduce access to members of base class.
A C++ derived class can modify the access rights of a base class member,
but only by restoring it to the rights in the base class.
It can't add or reduce access rights.
36. When a base class is inherited as private, all public and protected members of base
class become private members of derived class.
# include <iostream.h>
class BASE
{
public:
void BASE_out()
{
cout << "nn I am BASE_out.";
}
};
main()
{
DERIVED a;
a.DERIVED_out();
a.BASE_out();
}
class DERIVED : private BASE
{
private:
public:
void DERIVED_out()
{
cout << "nn I am DERIVED_out.";
}
};
When a base class is inherited as
private, all members of base class
become private members of derived
class. The public function
BASE_out() of BASE class becomes
private member of derived class.
Hence the objects of derived class
can’t access it from main().
It will becomes private member of
DERIVED class.
It is ‘public’ in
BASE class.
ERROR !.
Private
members of the
class can’t
access from
main().
Click Me
37. # include <iostream.h>
class BASE
{
public:
void BASE_out()
{
cout << "nnI am BASE_out.";
}
};
class DERIVED : private BASE
{
public:
BASE :: BASE_out;
void DERIVED_out()
{
cout << "nnI am DERIVED_out.";
}
};
main()
{
DERIVED a;
a.BASE_out();
a.DERIVED_out();
}
You can restore original access specification of base class members by re-declare
base class members in the public/protected section of derived class.
Base_out() is public in
Base Class.
All members of BASE class becomes
private by ‘private derivation.”. Hence
access to BASE_out() will be changed
to private.
Restore public accessibility of
BASE_out() by re-declaring it in the
public section of derived class.
OUTPUT
I am BASE_out.
I am DERIVED_out.
Click Me
38. 1. By changing visibility mode of the private member as public. By making it
public, it will become accessible to all other functions of the program,
thereby eliminating the benefits of data hiding.
Making a Private Member Inheritable. In inheritance, the derived class
has get access privilege to public and protected members of the base class.
The derived class can’t access private members of the base. Access to private
members of the base class done by 2 ways.
1. By changing visibility mode of the private member as protected. In this way,
the data hiding feature will retained and the same time it becomes
accessible by the derived class.
39. Inherit. But Not
Accessible
Private
Protected
Public
class BASE
Private
Protected
Public
Inherit. But Not
Accessible
Private
Protected
Public
Inherit. But Not
Accessible
Private
Protected
Public
class DER1 : public BASE class DER2 : private BASE
class DER3 : public DER1, private DER2
Following figure will illustrates the relationship of inheritance, visibility
modes and the type of inheritance.
Click Me
Click Me
40. MULTIPLE INHERITANCE means deriving a class from more than one base class.
With multiple inheritance, the new class inherits the members of all base class.
Hence the derived class get the features of other class in addition of its features.
class A
private:
int a;
void A_Fun1();
protected:
int b;
void A_Fun2();
public:
int c;
void A_Fun3();
class B
private:
int x;
void B_Fun1();
protected:
int y;
void B_Fun2();
public:
int z;
void B_Fun3();
class C : public A , private B
Not Accessible inheriting members
From Class A From Class B
int a;
void A_Fun1();
int x;
void B_Fun1();
private:
int y; int z;
void B_Fun2(); void B_Fun3();
protected:
int b;
void A_Fun2();
public:
int c;
void A_Fun3();
In public derivation, the public
members of base class becomes
public and protected members of
the base class become protected
members of the derived class.
In private derivation,
all members of the
base class become
private members of
the derived class.
Click Me
41. # include <iostream.h>
class BIO_GROUP : student_base, compulsory _subjects
{
int bio_mark, tot;
public:
void input()
{
cout << "nnEnter Name & Roll No of the Student";
cin >> name >> roll_no;
cout << "nnEnter Phy, Chem, Maths & Biology Marks ";
cin >> phy_mark >> chem_mark >> math_mark >> bio_mark;
}
void out()
{
cout << "nnName : " << name << " Roll No " << roll_no;
tot = phy_mark + chem_mark + math_mark + bio_mark;
cout << "nnnTotal Marks " << tot;
}
};
class compulsory_subjects
{
protected:
int phy_mark, chem_mark, math_mark;
};
class student_base
{
protected:
char name[20];
int roll_no;
};
main()
{
BIO_GROUP a;
a.input();
a.out();
}
Enter Name & Roll No of the Student
ANIL 100
Enter Phy, Chem, Maths & Biology
Marks 48 76 58 80
Name : ANIL 100
Total Marks : 262
Click Me
Click Me
42. Constructors in Multiple Inheritance. When an object of a derived class is
created, if the base class contains a constructor, it will be called first, followed by the
derived class’s constructor. When a derived object is destroyed, its destructor is
called first, followed by the base class’s destructor. Constructors are called in order
of derivation, destructors in reversed order.
# include <iostream.h>
class A
{
public:
A()
{
cout << "nnNow Constructing Base Class.";
}
};
class B : A
{
public:
B()
{
cout << "nConstructing Derived class B.";
}
};
main()
{
B obj;
}
Base Class
Constructor.
Constructor
of Derived
Class. Creating
Object of
Derived Class.
B obj;
43. Destructors in Multiple Inheritance. When a derived object is destroyed, its
destructor is called first, followed by the base class’s destructor. Constructors are
called in order of derivation, destructors in reversed order.
# include <iostream.h>
class A
{
public:
A()
{
cout << "nConstructing Base Class.";
}
~A()
{
cout << "nDestructing Base Class.";
}
};
main()
{
B obj;
}
class B : A
{
public:
B()
{
cout << "nConstructing Derived class B.";
}
~B()
{
cout << "nDestructing Derived class B.";
}
};
Creating
Object of
Derived Class.
B obj;
Base Class
Constructor.
Constructor
of Derived
Class.
Base Class Destructor.
Destructor of
Derived Class.
OUTPUT
Constructing Base Class.
Constructing Derived class B.
Destructing Base Class.
Destructing Derived class B.
44. Constructors in Multiple Inheritance. If base class constructor contains a parameterized
constructor, then it is mandatory for the derived class to have a constructor and pass the
arguments to the base class constructor.
class NO_ABC : NO_A, NO_B
{
int c;
void add()
{
cout << “nnSum is “ << a + b + c ;
}
};
# include <iostream.h>
class NO_A
{
protected :
int a;
public:
NO_A(int arg = 0)
{
a = arg;
}
};
class NO_B
{
protected :
Int b;
public:
NO_A(int arg = 0)
{
b = arg;
}
};
void main()
{
NO_ABC obj(10,20,30);
obj.add();
}
OUTPUT
Sum is 60
NO_ABC(int X = 0, int Y = 0, int Z = 0) : NO_A(X), NO_B(Y)
{
c = Z;
}
:
The constructor of derived class
accept 3 arguments and pass
appropriate arguments to base class’
constructor through explicit
invocation of them.
NO_A(X), NO_B(Y).
10 20 30
45. # include <iostream.h>
# include <string.h>
class student_base
{
protected:
char name[20]; int roll_no;
student_base(char nam[], int rol)
{ strcpy(name, nam); roll_no = rol; }
};
class compulsary_subjects
{
protected:
int phy_mark, chem_mark, math_mark;
public:
compulsary_subjects(int p, int c, int m)
{ phy_mark = p; chem_mark = c;
math_mark = m; }
};
Constructors in Multiple Inheritance. If base class constructor contains a parameterized
constructor, then it is mandatory for the derived class to have a constructor and pass the
arguments to the base class constructor. Constructors and destructors can’t be inherited.
Though a derived class can explicitly call the constructor and destructor of the base class.
class BIO_GROUP : student_base, compulsary_subjects
{
int bio_mark, tot;
public:
void out()
{
cout << "nName : " << name << " Roll No " << roll_no;
tot = phy_mark + chem_mark + math_mark + bio_mark;
cout << "nTotal Marks " << tot;
}
};
main()
{
BIO_GROUP a("anil", 100, 10,20,30,40);
a.out(); getch();
}
BIO_GROUP(char nam[], int rol, int p, int c, int m, int b)
: student_base(nam, rol), compulsary_subjects(p,c,m)
{ bio_mark = b; }
Nam=anil, rol=100, p=10, c=20, m=30, b=40anil
Click Me
100 10 20 30 40
"anil", 100, 10,20,30,40
46. If two or more classes are derived from a common base class
‘A’, then each derived class will contain separate copies of ‘A’.
When another class derived from the above two derived
classes, the resultant class will contain multiple copies of base
class ‘A’. It will create ambiguity (uncertainty).
HYBRIDE INHERITANCE
Classes ‘B’ and ‘C’ are derived from
a common Base class ‘A’. Hence
both classes ‘B’ and ‘C’ have their
own copies of base class.
A
B C
D
Classe ‘D’ contains multiple copies
of ‘A’. One through class ‘B’ and
another through ‘C’.
Copy 1: A B D
Copy 2: A C D
Copy
1
Copy
2
47. main()
{
D obj;
obj.a=10; // ERROR
obj.b=20; obj.c=30; obj.d=40;
}
# include <iostream.h>
class A
{
public:
int a;
};
class B : public A
{
public:
int b;
};
class C : public A
{
public:
int c;
};
class D : public B, public C
{
public:
int d;
};
obj.a=10;
a a
B::a,
b
C::a,
c
ERROR !
‘a’ can be found in
both base class,
and it was not
qualified to
indicate which
one you meant.
Class ‘A’ has been passed
to more than one derived
class ‘B’ and ‘C’, and those
derived class act as base
classes of another class.
The class, derived
from ‘B’ & ‘C’ have
get multiple copies
of base class ’A’. It
will create mbiguity.
Both class ‘B’ & ‘C’ has
separate copies of base
class ‘A’.
48. # include <iostream.h>
main()
{
D obj;
obj.B::a = 10; obj.C::a = 25;
cout << "nnOBJ.B::a = " << obj.B::a;
cout << "nnOBJ.C::a = " << obj.C::a;
}
class A
{
public:
int a;
};
class B : public A
{
public:
int b;
};
class C : public A
{
public:
int c;
};
class D : public B, public C
{
public:
int d;
};
a
obj.B::a obj.C::a
OUTPUT
OBJ.B::a = 10
OBJ.C::a = 20
Specifying name
of Data Member
with the
appropriate base
class name.
Obj.C::aObjects of class
‘D’ have get 2
copies of
A :: a from both
base class ‘B’
and ‘C’.
a
B::a=10 C::a=25
Resolving
ambiguity by
giving Qualified
Name.
49. DREADED DIAMOND ?. The Dreaded Diamond refers to a class structure in which
a particular class appears more than once in a class’s inheritance hierarchy. Some
times it will create ambiguity as base class is inherited more than one time. To deal
this situation, C++ provides ‘virtual base classes’.
class A
{
public:
int a;
};
class B : public A
{
public:
int b;
};
class C : public A
{
public:
int c;
};
class D : public B, public C
{
public:
int d;
};
INHERITING MEMBERS
B :: b
B :: A :: a
C :: A :: a
C :: c
B :: A :: a
C :: A :: a
INHERITING
MEMBERS
A :: a
INHERITING
MEMBERS
A :: a
Objects of
class ‘D’ have
multiple copies
of members of
class ‘A’.
a a
50. With ‘DREADED DIAMOND’ inheritance the resultant class will have multiple copies
of grandparent Base class from both parent classes. This duplication wastes space
and requires to specify which copy of the base class members you want. The
duplication can be avoided by making grandparent class as virtual. Hence the
derived class has a single copy of grandparent class, and it will be shared by all the
parent base classes. By placing the keyword ‘virtual’ before the name of parent
class, the grandparent class becomes virtual base class of parents.
class A
{
public:
int a;
};
class B :
{
public:
int b;
};
class C :
{
public:
int c;
};
class D : public B, public C
{
public:
int d;
};
Objects of
class ‘D’ have
a single copy
of class ‘A’.
Class ‘A’ becomes virtual Base
class of both ‘B’ & ‘C’.
A single copy of base class is
shared by all its sub-classes.
virtual public Avirtual public A
By placing ‘virtual’
class ‘A’ becomes
virtual Base class of
both ‘B’ & ‘C’.
51. # include <iostream.h>
main()
{
D obj;
obj.B:: a = 10; obj.C:: a = 25;
cout<<"Address of B::a is"<<unsigned(&obj.B::a);
cout<<“Address of C::a is"<<unsigned(&obj.C::a);
cout << "nThe Value of B :: a is " << obj.B::a;
cout << "nThe Value of C :: a is " << obj.C::a;
}
class A
{
public:
int a;
};
class B : public A
{
public:
int b;
};
class C : public A
{
public:
int c;
};
class D : public B, public C
{
public:
int d;
};
# include <iostream.h>
main()
{
D obj;
obj.B:: a = 10; obj.C:: a = 25;
cout<<"Address of B::a is"<<unsigned(&obj.B::a);
cout<<“Address of C::a is"<<unsigned(&obj.C::a);
cout << "nThe Value of B :: a is " << obj.B::a;
cout << "nThe Value of C :: a is " << obj.C::a;
}
class A
{ public: int a; };
class C : virtual public A
{
public:
int c;
};
class D : public B, public C
{ public: int d; };
Absence of
‘virtual’, it
becomes
dreaded
derivation.
class C : virtual public A
{
public:
int c;
};
Presence of ‘virtual’,
it becomes
virtual derivation.
Show Output
OUTPUT of virtual Derivation.
Address of obj.B::a is 65524
Address of obj.C::a is 65524
The Value of obj.B :: a is 25
The Value of obj.C :: a is 25
The virtual keyword ensures
that only one copy of the
A::a is included.
Obj.B :: a = 10;
There was only one
copy of A :: a, it was
shared between B :: a
and C :: a. Hence
Obj.B :: a = 10;
becomes
Obj.C :: a = 25;
Show Output
It is ‘DREADED DIAMOND’
There was multiple
copies of A :: a .
Obj.B :: a = 10
and
Obj.C :: a = 25.
10 25 10 25
OUTPUT
Address of B::a is 65524
Address of C::a is 65520
The Value of B :: a is 10
The Value of C :: a is 25
52. MULTILEVEL INHERITANCE. A class is derive from a class which is derive from
another class is known as multilevel inheritance. In multilevel inheritance, the
members of parent class are inherited to the child class and the member of child
class are inherited to the grand child class. The chain of classes forming
multilevel inheritance is known as inheritance hierarchy or inheritance path.
class A
{
};
class B : protected A
{
};
Class C : protected B
{
};
A
B
C
inheritance hierarchy
When a new class has been derived from
an existing class, the properties of base
class will be transferred to derived class.
When another class has been derived
from this derived class, the properties of
both 1st and 2nd base class are transferred
to the new generation class. This process
of inheriting features of one base class
into following generation of classes is
called transitive nature of inheritance. If
a class ‘A’ inherits properties of any base
class ‘X’, then all sub-classes of ‘A’ will
automatically inherits properties of class
‘X’. This property is called transitive
nature of inheritance.
transitive nature
53. class A : public Y
{
protected:
int a;
public:
void setval_A(int arg) { a = arg; }
void sum()
{
cout << "nSum of inherited Nos is " << x+y+a;
}
};
void main()
{
}
# include <iostream.h>
class X
{
protected:
int x;
public:
void setval_X(int arg) { x = arg; }
};
class Y : public X
{
protected:
int y;
public:
void setval_Y(int arg) { y = arg; }
};
class B : public Y
{
protected:
int b;
public:
void setval_B(int arg) { b = arg; }
void sum()
{
cout << "nSum of inherited Nos is " << x+y+b;
}
};
By transitive nature of
inheritance
X :: x and Y :: y
are inherited to newly
created class.
By transitive nature of
inheritance X::x is
inherited to newly created
class.
By transitive nature of
inheritance
X :: x and Y :: y
are inherited to newly
created class.
A ob1; obj.setval_X(10); ob1.setval_Y(20); ob1.setval_A(30); ob1.sum(); // Show Output
B ob2; ob2.setval_X(40); ob2.setval_Y(50); ob2.setval_B(60); obj2.sum(); // Show Output
10 20 30
40 50 60
OUTPUT
Sum of inherited Nos is 60
10 20
30
OUTPUT
Sum of inherited Nos is 150
40 50
60
54. NESTING OF CLASSES. A class has get properties of another class in different
ways.
1. A class has get certain properties of another class by
inheritance. So far we discussed about it.
2. A class has get certain properties of another class by
defining a inner class in it. It was discussed in the chapter
‘CLASSES AND OBJECTS’.
3. A class has get certain properties of another class
By defining an object of another class in it.
It was already discussed in the chapter
‘CONSTRUCTORS AND DISTRUCTORS’.
55. # include <iostream.h>
class A
{
public :
void Aout()
{
B obj;
obj.Bout();
cout << “nI am A out.”;
}
};
class B
{
public :
void Bout()
{
cout << “nI am B Out.”;
}
};
A class has get certain properties of another class by defining a inner class in it. It
was discussed in the chapter ‘CLASSES AND OBJECTS’.
Class B is declared within the
scope of its enclosed class. The
scope of nested class is restricted
within the scope of its enclosing
class. To refer to a nested class
from a scope other than its
immediate enclosing scope, you
must use a fully qualified name.
main()
{
A x;
x.Aout();
}
Create an object ‘obj’ of inner
class ‘B’, and invoke
obj.Bout();
OUTPUT
I am B Out.
I am A Out.
Show Output
56. A class has get certain properties of another class By defining an object of
another class in it. When a class contains objects of another class as its
member then it is called ‘Containership’ or ‘Containment’ or ‘Aggregation’.
# include <iostream.h>
class A
{
public :
void Aout()
{
cout << “nI am A out.”;
}
};
class B
{
A obj;
public :
void Bout()
{
obj.Aout();
cout << “nI am B Out.”;
}
};
main()
{
B x;
x.Bout();
}
Class ‘B’ has get properties of
class ‘A’ by defining an object of
class ‘A’ in it. The relationship
obtained in this manner is called
‘HAS-A Relationship’. Class ‘B’
has ‘HAS-A’ relationship with
Class ‘A’.
OUTPUT
I am A Out.
I am B Out.
Show Output
57. # include <iostream.h>
class A
{
public:
A()
{
cout << "nnNow Constructing A";
}
};
void main()
{
B x;
}
class B
{
A obj;
public:
B()
{
cout << "nnNow Constructing B";
}
};
When a class contains objects of
another class as its member then
the constructor of member object
invoke first then invoke its
constructor at last.
OUTPUT
Now Constructing A
Now Constructing B
Class ‘B’ has ‘HAS-A’
relationship with class ‘A’.
58. # include <iostream.h>
class time
{
int h, min, s;
public:
time ( int arg1, int arg2, int arg3 )
{
h = arg1; min = arg2; s = arg3;
}
void out()
{
cout<< “ “<<h << ":"<<min<<":“ <<s;
}
};
class date_time
{
time t;
int d, mon, y;
public:
date_time ( int dd, int mm, int yy, int hh,
int min, int ss) : t ( hh, min, ss )
{
d = dd; mon = mm; y = yy;
}
void out()
{
cout << "Date & time is " << d << "/" << mon
<< "/" << y;
t.out();
}
};
Implicit call to the constructor
time ( hh, min, ss )
main()
{
date_time a(15, 9, 2010, 3, 47, 50 );
a.out();
}
OUTPUT
Date & time is 15 / 9 / 2010 3:47:50
Show Values
Object of another
Classes.
HAS_A relationship.
Show Order
3 47 5015 9 2010
Arguments for
constructing time H:M:S.
Arguments for
constructing date. d/m/y
59. # include <iostream.h>
class time
{
int h, min, s;
public:
time ( int arg1, int arg2, int arg3 )
{
h = arg1; min = arg2; s = arg3;
}
void out()
{
cout<< “ “<<h << ":"<<min<<":“ <<s;
}
};
class date_time : public time
{
int d, mon, y;
public:
date_time (int dd, int mm, int yy, int hh,
int min, int ss) : time ( hh, min, ss )
{
d = dd; mon = mm; y = yy;
}
void out()
{
cout << "Date & time is " << d << "/" << mon
<< "/" << y;
t.out();
}
};
Explicit call to the constructor
time ( hh, min, ss )
main()
{
date_time a(15, 9, 2010, 3, 47, 50 );
a.out();
}
OUTPUT
Date & time is 15 / 9 / 2010 3:47:50
Show Values
Deriving a class from
the class ‘time’.
IS-A relationship.
Show Order
3 47 5015 9 2010
Arguments for
constructing time H:M:S.
Arguments for
constructing date. d/m/y
60. When a class contains object of another class then constructor for the member
object is invoked implicitly through object name. But a class being derive from
another class, the constructor for the base class is invoked explicitly by giving
name of the constructor.
# include <iostream.h>
class A
{
public:
int a;
A ( int arg = 0 )
{
a = arg;
}
};
class B
{
public:
A a_obj;
int b;
B ( int arg1 = 0, int arg2 = 0 ) : a_obj(arg1)
{
b = arg2;
}
};
class C : public A
{
public:
int c;
C ( int arg1 = 0, int arg2 = 0 ) : A (arg1)
{
c = arg2;
}
};
main()
{
B b_obj(10,20); C c_obj(30,40);
cout << "nMembers of class B are " <<
b_obj.a_obj.a << ", " << b_obj.b;
cout << "nMembers of class C are " <<
c_obj.a << ", " << c_obj.c;
}
Class ‘B’ gets properties
of class ‘A’ through the
object a_obj.
Class ‘C’ inherits
properties class ‘A’.
So it can access base
class members as its
own members.
Explicitly invoke the
Construct A(arg1).
Implicitly invoke the
Construct A(arg1)
Through object name.
Click Me
10 20 30 40
61. The relationship between classes. When a class has get properties of another
class, then a relationship between classes will be established. These
relationship established between classes are in several forms.
IS-A Relationship. When a class inherits from another class, is known as ‘IS-
A’ relationship. The derived class may either override the methods of the
base, or add new methods.
HAS-A Relationship. When a class contains objects of another class as its
member, is known as ‘HAS-A’ relationship. The class having HAS-A
relationship has get the ownership of contained objects. Ownership defines
the responsibility for the creation and destruction of object.In this
relationship, we can not override any methods of member objects.
HOLDS-A Relationship. When a class indirectly contains objects of another
class via pointer or reference is said to be HOLDS-A relationship. It is similar
to HAS-A relationship but the ownership is missing.
62. # include <iostream.h>
class A
{
public:
A()
{
cout << “nNow Constructing A”;
}
void out()
{
cout << "nnI am out () of A";
}
}a_obj;
main()
{
cout << “nnEntered to MAIN()”;
B b_obj;
b_obj.out();
}
class B
{
A &ref_obj;
public:
B () : ref_obj ( a_obj ) { }
void out()
{
cout << "nnI am out () of B";
ref_obj.out();
}
};
HOLDS-A Relationship. When a class
indirectly contains objects of another
class via pointer or reference is said to
be HOLDS-A relationship. It is similar
to HAS-A relationship but the
ownership is missing.
Class ‘B’ has a ‘HOLDS-A’
Relationship with class ‘A’.
Initializing Reference
Member. Do not Invoke the
Constructor.
OUTPUT
Now Constructing A
Entered to MAIN()
I am out () of B
I am out () of A
Show Output
1
2
3
4
1
2
3
4
63.
64. # include <iostream.h>
# include <string.h>
# include <conio.h>
class STUDENT
{
protected:
char name[20]; int rollno;
STUDENT_BASE(char nam[]="", int rol=0)
{
strcpy(name, nam); rollno = rol;
}
};
class COMPULSARY_SUBJECTS : public virtual STUDENT
{
protected:
int phy, chem, maths;
public:
COMPULSARY_SUBJECTS(int p, int c, int m)
{
phy = p; chem = c; maths = m;
}
};
65. class EXTRA_CARICULAR : public virtual STUDENT
{
protected:
int sports, arts, social_activity;
public:
EXTRA_CARICULAR(int sp, int art, int so)
{
sports = sp; arts = art; social_activity = so;
}
};
class BIO_GROUP : COMPULSARY_SUBJECTS, EXTRA_CARICULAR
{
int bio;
public:
BIO_GROUP(char nam[20]="", int rol, int p, int c, int m, int sp, int art, int so, int bi) :
STUDENT_BASE(nam, rol) , COMPULSARY_SUBJECTS(p,c,m),
EXTRA_CARICULAR(sp,art,so)
{ bio = bi; }
void input();
void out();
};
66. void BIO_GROUP :: input()
{
cout << "nnPlease Enter name & Roll No"; cin >> name >> rollno;
cout << "nnPlease Enter Marks of Phy, Chem & Maths";
cin >> phy >> chem >> maths;
cout << "nnPlease Enter Grace Marks of Sports, Arts & Social Activity. ";
cin >> sports >> arts >> social_activity;
cout << "nnPlease Enter Biology Marks "; cin >> bio;
}
void BIO_GROUP :: out()
{
cout << "nnName " << name << " Roll No " << rollno << "nnPhy Marks " << phy
<< " Chem. Marks " << chem << " Maths Mark " << maths << "nGrace Marks : “
<< “ Sports " << sports << " Arts " << arts << " Social Activity. " << social_activity;
cout << "nnBiology Marks " << bio;
}
void main()
{
BIO_GROUP a("Anil Kumar", 100, 10, 20, 30, 40, 50, 60, 80);
cout << "nnInitial Data ";
a.out(); cout << "nn"; a.input(); a.out();
}
67. a
Click Me
c
Flow of Execution.
Creating object of student
invoke student :: input()
input name & marks Back
to main() Invoke student ::
out() Print name & other
details. Back to main() and
STOP EXECUTION.
Flow of Execution.
Creating object of BIO_GROUP
invoke BIO_GROUP :: input () invoke
student :: input() input name & marks
Back to BIO_GROUP :: input()
Read Biology Mark Back to main()
Invoke BIO_GROUP :: out() invoke
student :: out() Print name & other
details. Back to BIO_GROUP :: out()
& Print bio_mark Back to main() and
STOP EXECUTION.
Traits
common to
Child Classes.
The access to the
base class members
from outside of derived
class to be specified
by visibility modes.
The access specifier
must be either private,
protected or public.
c
6
anil
6