0% found this document useful (0 votes)
6 views78 pages

Understanding Inheritance in C++

Inheritance is a fundamental concept in object-oriented programming that allows a derived class to inherit features from a base class, enhancing code reusability and reliability. The document discusses various types of inheritance, including private, protected, and public inheritance, as well as multiple inheritance and the diamond problem. It also covers constructor and destructor execution order, function overriding, and polymorphism, providing examples in C++.

Uploaded by

Sadiholic
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views78 pages

Understanding Inheritance in C++

Inheritance is a fundamental concept in object-oriented programming that allows a derived class to inherit features from a base class, enhancing code reusability and reliability. The document discusses various types of inheritance, including private, protected, and public inheritance, as well as multiple inheritance and the diamond problem. It also covers constructor and destructor execution order, function overriding, and polymorphism, providing examples in C++.

Uploaded by

Sadiholic
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Inherita

nce
Introduction
• Inheritance is one of the key feature of object-oriented programming
including C++ which allows user to create a new class(derived class)
from a existing class(base class)
• The derived (child) class inherits all feature from a base (parent)
class and it can have additional features of its own

2
Introduction Cont..

3
Advantages
• Saves memory space
• Saves time
• Increases reliability of the code
• Saves the developing and testing efforts

4
Syntax
class drivedClass_name : access_mode
baseClass_name
e.g.
Rectangle : Public Shape

5
Inheritance Visibility Mode
• Depending on Access modifier used while inheritance, the availability
of class members of parent class in the child class changes
• It can either be
○ Private
○ Protected
○ Public

6
Private Inheritance
• In private mode, the protected and public members of super class
become private members of derived class
• Syntax is
class Subclass : private Superclass OR
class Subclass : Superclass // Default

7
Protected Inheritance
• In protected mode, the public and protected
members of Super class becomes protected
members of Sub class
• Syntax is
class Subclass : protected Superclass

8
Inheritance Visibility Mode

9
Example
class Shape {
protected:
float length;
float width;
};
Class Rectangle : public Shape{
public:
float getArea ();
} ;

10
Example Cont..
●Implement a Polygon class that contains length and width data
members. Derive two classes Triangle and Rectangle class from Polygon
class and calculate area (Area of rectangle is length x width and area of
triangle is (length x width)/2)

11
Example Cont..
class CTriangle: public CPolygon {
class CPolygon { public:
protected: int area ()
int width, height; { return (width * height /
public: 2); }
void set_values (int a, int b) };
{ width=a; height=b;} void main () {
}; CRectangle rect;
class CRectangle: public CPolygon { CTriangle trgl;
public: rect.set_values (4,5);
int area () trgl.set_values (4,5);
{ return (width * height); } cout << [Link]() << endl;
}; cout << [Link]() << endl;
}
12
Order of Constructor and
Destructor Execution
• Base class constructors are always executed
first
• Destructors are executed in exactly the
reverse order of constructors

13
Order of Constructor and
Destructor Execution Cont..
class Base{
public:
Base ( )
{cout << "Inside Base constructor" << endl; }
~Base ( ) { cout << "Inside Base destructor" << endl;} Output
};
class Derived : public Base{ Inside Base constructor
public: Inside Derived
constructor
Derived ( ){cout<< "Inside Derived constructor\n";}
Inside Derived
~Derived ( ){cout<<"Inside Derived destructor\n";} destructor
}; Inside Base destructor
void main( ){
Derived x;}
14
15
16
Inheriting Constructors
• If the base class constructor takes no parameters then the
inheritance is implicit - you don’t need to do anything!
• If the base class constructor takes parameters then each derived
class needs to declare a constructor with the same parameters
○ You can pass the arguments given to the derived class constructor to the
constructor for the base class

17
Inheriting Constructors Cont..
class Person{ class Student: public Person{
protected: public:
void setAge(int a)
int age; { age = a; }
public:
Person() };
{age = -5;}
Person(int a) int main(){ Default constructor
Student p;
{age = a;} cout<<[Link]();
int getAge() Student p1(15);
{return age;} } Parameterized constructor can’t be inherited
}; Compilation error

18
Inheriting Constructors Cont..
class Student: public Person{
public:
Student(int age):Person(age){
}
void setAge(int a){ age = a; }
In the implementation of the constructor
for the derived class, the parameter
}; passed to the derived class constructor is
passed down to the base class constructor

19
Inheriting Constructors Cont..
derived class constructor base class constructor

Square(int side) : Rectangle(side,side)

derived constructor base constructor


parameter parameters

20
Inheriting Constructors Cont..
class base{ class derv1 : public base{
private: private:
int x; int y;
public: public:
base (int x1){ derv1 (int yy, int xx) : base (xx){
cout<<"Base's cout << " Derv1's constructor
constructor executed"; executed";
x = x1; y = yy;
cout<<x<<endl; cout<<y<<endl;
} }
}; };
void main ( ){ Base’s constructor executed 30
derv1 d (20, 30);} Derv1’s constructor executed 20

21
Overriding Functions
• It is language feature that allows a subclass or child class to provide
a specific implementation of a method that is already provided by
one of its super-classes or parent classes

22
Overriding Functions Cont..
class Stack class Stack2 : public Stack {
{ public:
protected: void push(int var){
int st[3]; if(top >= MAX-1){cout<<
int top; “\nError: stack is full”;
public: exit(1); }
Stack(){top = -1;} Stack::push(var);
void push(int var) }
{st[++top]=var;}
int pop() int pop(){
{return st[top--]; if(top<0){cout<< “\
} nError: stack is empty\n”; exit(1);
}
return Stack::pop();
} 23
};
Overriding Functions Cont..
int main(){
Stack2 s1;
[Link](11);
[Link](22);
[Link](33);
cout << endl << [Link]();
cout << endl << [Link]();
cout << endl << [Link]();
cout << endl << [Link]();
cout << endl;
}

24
Overriding Functions Cont..
class Point { class Circle : public Point {
protected: double radius;
int x,y; public:
public: Circle(int ,int ,double );
Point(int ,int); void display(void);
void display(void); Circle(int a,int b,double
}; c):Point(a,b) {radius = c; }
Point::Point(int a,int b) void Circle::display(void) {
{ Point::display();
x=a; cout<<"and radius = "<<radius;}
y=b;
} void main(void){
void Point::display(void) Circle object(3,4,2.5);
{ [Link]();
cout<<"point =
["<<x<<“,“<<y<<"]“; } }

Output
point=[3,4] and radius = 2.5
25
Overriding Functions Cont..
class Point class Circle : public Point
{
{ protected:
protected: double radius;
int x,y; public:
public: Circle(int ,int ,double);
~Circle()
Point(int ,int ); {cout<<"Circle Class
void display(void); Destructor"; }
~Point(){ };
Circle::Circle(int a,int b,double c):
cout<<"Point Class Destructor";}
}; Point(a,b)
{
Point::Point(int a,int b) cout<<"Circle Class
{ Constructor";
cout<<"Point Class Constructor"; radius = c;
} }

26
Overriding Functions Cont..
class Cylinder: public Circle{
double height;
public:
Cylinder(int ,int ,double ,double);
~Cylinder() { cout<<"\nCylinder Class Destructor\n"; }
};
Cylinder::Cylinder(int a,int b,double r,double h):Circle(a,b,r) {
cout<<"\nCylinder Class Constructor\n";}
void main(void){ Output
Cylinder object(3,4, 2.5, Point Class Constructor
3.7); Circle Class Constructor
} Cylinder Class Constructor
Cylinder Class Destructor
Circle Class Destructor
Point Class Destructor

27
Overriding Functions Cont..
class Cylinder: public Circle{
double height;
public:
Cylinder(int ,int ,double ,double);
~Cylinder() { cout<<"\nCylinder Class Destructor\n"; }
};
Cylinder::Cylinder(int a,int b,double r,double h):Circle(a,b,r) {
cout<<"\nCylinder Class Constructor\n";}
void main(void){ Output
Cylinder *object = new Cylinder(3,4, 2.5, Point Class Constructor
3.7); Circle Class Constructor
Cylinder Class Constructor
}

28
Types of Inheritance

29
Multiple Inheritance
• A feature of object-oriented computer programming language in
which an object or class can inherit characteristics and features from
more than one parent object or parent class

30
Multiple Inheritance Cont..
• Implement the following structure

31
Multiple Inheritance Cont..
class Person {
public:
Person(int x)
{ cout << "Person::Person(int ) called" << endl; }
};

class Faculty : public Person {


public:
Faculty(int x):Person(x) {
cout<<"Faculty::Faculty(int ) called"<< endl;
}
};

class Student : public Person {


public:
Student(int x):Person(x) {
cout<<"Student::Student(int ) called"<< endl;
}
};

32
Multiple Inheritance Cont..
class TA : public Faculty, public
class Person {
public: Student {
Person(int x) public:
{cout<<"Person(int) called"<<endl;} TA(int x):Student(x),
};
Faculty(x) {
cout<<"TA(int ) called"<<
class Faculty : public Person {
public: endl;
Faculty(int x):Person(x) { }
cout<<"Faculty(int) called"<< endl; };
}
};
int main() {
TA ta1(30);
class Student : public Person {
public: }
Student(int x):Person(x) {
cout<<"Student(int ) called"<< endl;
}
};

33
Multiple Inheritance Cont..
Class Person Class Student: public Person
{ {// code};
Protected:
int age; Class TA: public Faculty, public
Student
Public:
{ // code };
int getAge(){ return age;}
void main (){
};
TA t1;
Class Faculty: public Person [Link]();
{ // code }; }

Output
Compilation Error
34
Multiple Inheritance Cont..
• One may face such ambiguities while using multiple inheritance
○ This particular ambiguity is known as Diamond Problem
• The solution to this problem is by making inheritance virtual
i.e.
Class Faculty: virtual public Person
Class Student: virtual public Person

• This will ensure that only one sub-object of the Person class will be
created for every TA object

35
Pointers to Objects
class Triangle: public Polygon {
class Polygon {
public:
protected: int area() { return
int width, height; width*height/2;}
public: };
void set_values(int a, int b) void
{ width=a;
main () height=b;
{ }
}; Rectangle rect;
class Rectangle: public Polygon { Triangle trgl;
Polygon * ppoly1 = &rect;
public:
Polygon * ppoly2 = &trgl;
int area()
ppoly1->set_values (4,5);
{ return width*height; }
ppoly2->set_values (4,5); Output
}; cout << [Link]() << '\n'; 20
cout << [Link]() << '\n'; }
10
36
Pointers to Objects Cont..
• If a base class and derived class has same function and if you write
code to access that function using pointer of base class then, the
function in the base class is executed even if, the object of derived
class is referenced with that pointer variable

37
Pointers to Objects Cont..
int main() {
class B {
B *b;
public:
D d;
void display() {
b->display();
cout<<"Content of base class.\n";}
b = &d;
};
b->display();
class D : public B {
return 0;
public:
}
void display() {
cout<<"Content of derived class.\n"; }
};

Output
Content of base class.
Content of base class. 38
Polymorphism
• The Greek word polymorphism means one name, many forms
• Polymorphism is the ability to use an operator or a function in
different ways
○ Static polymorphism:

■ It can be achieved by using overloading (it is defined


at compilation time)
○ Dynamic polymorphism:

■ It can be implemented by using inheritance (it is


implemented at runtime)
39
Polymorphism Cont..
• Polymorphism can be achieved by using virtual
functions
○ Virtual functions gives programmer capability to call member function of different
class by a same function call depending upon different context
○ If you want to execute the member function of derived class then, you can declare
display( ) in the base class virtual which makes that function existing in appearance
only but, you can't call that function

40
Polymorphism Cont..
class B { void main()
class D2 : public B {
public: {
public: virtual void display(){ B *b;
cout<<"Content
void display() { of base class \n"; D1 d1;
}
cout<<"Content of 2nd derived class.\n";} D2 d2;
};
class
}; D1 : public B { b = &d1;
public: b->display();
void display(){ b = &d2;
cout<<"Content of 1st derived b->display();
class”; } }
};
Output
Content of 1st derived class
Content of 2nd derived class

41
Polymorphism Cont..
class base { int main(){
public: base b1;
void show() [Link]();
{ cout << “base \n”; } derived d1;
}; [Link]();
base *pb = &b1;
class derived : public base {
pb->show();
public:
pb = &d1;
void show()
pb->show();
{ cout << “derived\n”;}
}
};

Output
Base
Derived
Base
Base 42
Polymorphism Cont..
● While it is allow for a base class pointer to point to a
derived object, the reverse is not true.
base b1;
derived *pd = &b1; // compiler error

43
Polymorphism Cont..
Faculty{
private:
char *firstname, *lastname, *department;
int ServiceYears;
public:
Faculty (char *p_firstname, char *p_lastname, char *p_department,
int p_ServiceYears);

~Faculty ( );

void printInformation ( );
}

44
Polymorphism Cont..
Faculty::Faculty ( char *p_firstname, char *p_lastname, char
*p_department, int p_ServiceYears)
{
firstname = new char[ strlen(p_firstname)+1 ];
lastname = new char[ strlen(p_lastname)+1 ];
department = new char[ strlen(p_department)+1 ];

strcpy(firstname , p_firstname );
strcpy(lastname, p_lastname);
strcpy(department, p_department);

ServiceYears = p_ServiceYears;
}
45
Polymorphism Cont..
Faculty::~Faculty ( )
{
delete [ ] firstname;
delete [ ] lastname;
delete [ ] department;
}

void Faculty::printInformation ( )
{
cout << “Name = “ << firstname << “ “ <<lastname<<endl;
cout << “Department = “ << department << endl;
cout << “Year of Service = “ << ServiceYears;
}

46
Polymorphism Cont..
class Student
{ private:
char *firstname, *lastname, *department;
int rollNumber; float CGPA;

public:
Student (char *p_firstname, char *p_lastname, char
*p_department, int p_rollNumber, float p_CGPA);

~Student ( );

void printInformation ( );
};

47
Polymorphism Cont..
Student::Student ( char *p_firstname, char *p_lastname, char
*p_department, int p_rollNumber, float p_CGPA)
{
firstname = new char[ strlen(p_firstname)+1 ];
lastname = new char[ strlen(p_lastname)+1 ];
department = new char[ strlen(p_department)+1 ];

strcpy(firstname , p_firstname );
strcpy(lastname, p_lastname);
strcpy(department, p_department);
rollNumber = p_rollNumber;
CGPA = p_CGPA;
}
48
Polymorphism Cont..
Student::~Student ( )
{
delete [ ] firstname;
delete [ ] lastname;
delete [ ] department;
}

void Student::printInformation ( )
{
cout << “Name = “ << firstname << “ “ << lastname << endl;
cout << “Department = “ << department << endl;
cout << “Roll Number = “ << rollNumber << endl;
cout << “CGPA = “ << CGPA << endl;
}
49
Polymorphism Cont..
int main ( )
{
Faculty f1 (“John”, “Tait”, “Computer Science”, 30 );
Faculty f2(“Leif”, “Azzopardi”, “Computer Science”, 20);
Faculty f3(“Yasir” ,”Zaki”, “Management Sciences”, 5);
Student s1(“Cheng”, “Xiang”, “Computer Science”, 1233, 3.4 );
Student s2(“Nasir”, “Rashid”, “Electrical Eng”, 8323, 3.2);

[Link]();
[Link]();
[Link]();
[Link]();
[Link]();

return 0;
}
50
Polymorphism Cont..
class BaseDBClass
{ private:
char *firstname, *lastname, *department;
int rollNumber,ServiceYears;
float CGPA;

public:
virtual void printInformation ( )
{

}
};
51
Polymorphism Cont..
class Faculty : public BaseDBClass
{

public:
Faculty (char *p_firstname, char *p_lastname,
char *p_department, int p_ServiceYears);

~Faculty ( );

void printInformation ( );

};

52
Polymorphism Cont..
Faculty::Faculty ( char *p_firstname, char *p_lastname, char
*p_department, int p_ServiceYears)
{
firstname = new char[ strlen(p_firstname)+1 ];
lastname = new char[ strlen(p_lastname)+1 ];
department = new char[ strlen(p_department)+1 ];

strcpy(firstname , p_firstname );
strcpy(lastname, p_lastname);
strcpy(department, p_department);

ServiceYears = p_ServiceYears;
}
53
Polymorphism Cont..
Faculty::~Faculty ( )
{
delete [ ] firstname;
delete [ ] lastname;
delete [ ] department;
}

void Faculty::printInformation ( )
{
cout << “Name = “ << firstname << “ “ << lastname <<
endl;
cout << “Department = “ << department << endl;
cout << “Year of Service = “ << ServiceYears;
}
54
Polymorphism Cont..
class Student : public BaseDBClass
{
public:
Student (char *p_firstname, char *p_lastname,
char *p_department, int p_rollNumber, float
p_CGPA);

~Student ( );

void printInformation ( );
};

55
Polymorphism Cont..
Student::Student ( char *p_firstname, char *p_lastname, char
*p_department, int p_rollNumber, float p_CGPA)
{
firstname = new char[ strlen(p_firstname)+1 ];
lastname = new char[ strlen(p_lastname)+1 ];
department = new char[ strlen(p_department)+1 ];

strcpy(firstname , p_firstname );
strcpy(lastname, p_lastname);
strcpy(department, p_department);
rollNumber = p_rollNumber;
CGPA = p_CGPA;
}
56
Polymorphism Cont..
Student::~Student ( )
{
delete [ ] firstname;
delete [ ] lastname;
delete [ ] department;
}

void Student::printInformation ( )

cout << “Name = “ << firstname << “ “ << lastname << endl;
cout << “Department = “ << department << endl;
cout << “Roll Number = “ << rollNumber << endl;
cout << “CGPA = “ << CGPA << endl;
}
57
Polymorphism Cont..
int main ( ){
Faculty f1 (“John”, “Tait”, “Computer Science”, 30 );
Faculty f2(“Leif”, “Azzopardi”, “Computer Science”, 20);
Faculty f3(“Yasir” ,”Zaki”, “Management Sciences”, 5);
Student s1(“Cheng”, “Xiang”, “Computer Science”, 1233, 3.4 );
Student s2(“Nasir”, “Rashid”, “Electrical Eng”, 8323, 3.2);
BaseDBClass *db[5];
db[0] = &f1;
db[1] = &f2;
db[2] = &f3;
db[3] = &s1;
db[4] = &s2;
for ( int i = 0; i < 5; i ++ )
db[i]->printInformation();
return 0;}
58
Pure Virtual Functions &
Abstract Classes
• A pure virtual function is a function that has the notation "=
0" in the declaration of that function
class SomeClass {
public:
virtual void pure_virtual() = 0; pure
specifier
};
a pure virtual function has
no function body

59
Pure Virtual Functions &
Abstract Classes Cont..
• Any class that has at least one pure virtual function is called
an abstract class
• An abstract class cannot have an instance of itself created
E.g (previous example)
SomeClass a; Error! SomeClass is
abstract

• Any class that is derived from an abstract class must override the
definition of the pure virtual function otherwise the derived class
becomes an abstract class as well

60
Pure Virtual Functions &
Abstract Classes Cont..
○ “Non-pure" virtual function provides a definition, which means that the class in
which that virtual function is defined does not need to be declared abstract
○ You would want to create a pure virtual function when it doesn’t make sense to
provide a definition for a virtual function in the base class itself, within the context
of inheritance

61
Pure Virtual Functions &
Abstract Classes Cont..
• Example
class Circle: Public
Figure
it doesn’t make {
sense to public:
actually provide class Figure
void draw( ) { //code }
a definition for {
}
the draw public:
function, void draw( ) class Square: Public
because of the } Figure
simple and {
obvious fact public:
that a “Figure” void draw( ) { //code
has no specific }
shape }

62
Pure Virtual Functions &
Abstract
• Example
Classes Cont..
Suppose there is a class Shape that has a member data float length and member
functions getLength() and calArea(). There are three classes Square, Rectangle and
Triangle derived from it. Implement the classes to calculate area of all the derived
classes. Write main() to test the implementation.

63
Virtual Destructors
class Base{
public:
Base(){ cout<<“Base Constructor\n";}
~Base(){cout<<"Base Destructor\n";}
};
class Derived: public Base{
public:
Derived(){ cout<<“Derived Constructor\n";}
~Derived(){cout<<“Derived Destructor\n";}
};
Output
int main(){
Base Constructor
Base* obj=new Derived;
Derived Constructor
delete obj;
Base Destructor
}
64
Virtual Destructors Cont..
• When base class destructor is made Virtual then first, Derived class's
destructor is called and then Base class's destructor is called, which
is the desired behavior
• You should always make your destructors virtual if you’re dealing
with inheritance (specially polymorphism)

65
Virtual Destructors Cont..
class Base{
public:
Base(){ cout<<“Base Constructor\n";}
virtual ~Base(){cout<<"Base Destructor\n";}
};
class Derived: public Base{
public:
Derived(){ cout<<“Derived Constructor\n";}
~Derived(){cout<<“Derived Destructor\n";}
}; Output
int main(){ Base Constructor
Base* obj=new Derived; Derived Constructor
delete obj; Derived Destructor
} Base Destructor
66
Pure Virtual Destructor
• Pure Virtual Destructors are legal in C++. Also, pure virtual
Destructors must be defined, which is against the pure virtual
behavior
• The only difference between Virtual and Pure Virtual Destructor is,
that pure virtual destructor will make its Base class Abstract, hence
you cannot create object of that class
• There is no requirement of implementing pure virtual destructors in
the derived classes

67
Pure Virtual Destructor Cont..
class Base{
public:
virtual ~Base()=0; // Pure virtual destructor
};
Base::~Base() {cout << "Pure virtual destructor is called";}

class Derived : public Base{


public:
~Derived() {cout << "~Derived() is executed\n"; }
};
int main()
{ Output
Base *b = new Derived; ~ Derived is executed
delete b;
}
Pure virtual destructor
68
Inheritance vs Association
Vehicle

Land Vehicle Water Vehicle Air Vehicle

Car Boat Helicopter

Van Cruise Plane


69
Inheritance vs Association
Cont..
• Specialisation
○ Extending the functionality of an existing class
• Generalisation
○ Sharing commonality between two or more classes

70
Inheritance vs Association
Cont..
Abstract Vehicle Generalizatio
Class n

Land Vehicle Water Vehicle Air Vehicle

Abstract Classes

Car Boat Helicopter

Concrete Classes Specialization

Van Cruise Plane

71
Relationships
• Is-a Relationship (Inheritance)
○ Relationship between Car class and Land Vehicle class
○ Also known as ‘Kind of’ or ‘Type of’ relationship
• ‘Has-a’ Relationship (Association)
○ Relationship between Car and a Wheel
• E.g. If you are asked to make a generalized class from
○ Dog, Cow, Whale, Elephant, Eagle

72
Relationships Cont..
• Association is a relationship where all objects have their own
lifecycle and there is no owner
○ E.g. Teacher and Student, multiple students can associate with single teacher and
single student can associate with multiple teachers, but there is no ownership
between the objects and both have their own lifecycle
○ Association is further divided in to
■ Aggregation
■ Compositions

73
Relationships Cont..
• Aggregation is a specialized form of Association where all objects
have their own lifecycle, but there is ownership and child objects can
not belong to another parent object
○ Aggregation models “has-a” relationship
○ E.g. Department and teacher, a single teacher can not belong to multiple
departments, but if we delete the department teacher object will not be destroyed

74
Relationships Cont..
• Composition is again strong type of Aggregation and sometimes
known as a “death” relationship
○ In composition, the lifetime of the 'part' is controlled by the “owner” class
• Child object does not have its lifecycle and if parent object is deleted,
all child objects will also be deleted
○ E.g. House and Rooms, house can contain multiple rooms - there is no independent
life of room and any room can not belong to two different houses
○ If we delete the house - room will automatically be deleted

75
Relationships Cont..
Class A
{
public:
A(){ };
~A(){ };
};

Class B{
private:
A *a; A aggregation to
public: B
B(){ };
~B(){ };
SayHello( ) { a = new A( ); }
};

76
Relationships Cont..
class A
{
public:
A(){ };
~A(){ };
};
Class B
{
public:
B(){ };
~B(){ };
private:
A a; // A composition to
}; B

77
Relationships Cont..
class B{ class A {
private: private:
int ID; B* bPtr;
public: C c;
B(int id){ public:
this->ID = id; A() {cout<<"construct A
cout<<"construct a B object: object”;}
"<<id<<endl; ~A() {cout<<"destruct A
} object”;}
~B(){ void Aggregation()
cout<<"destruct the B {
class object: "<<this- bPtr = new B(1);
>ID<<endl; }
} void
}; main()
}; {
cout<<"Demo for class aggregation
class C{
relationship"<<endl;
public:
A a;
C() {cout<<"construct C
[Link]();
object"<<endl;}
}
~C() {cout<<"destruct C
78
object"<<endl;}

You might also like