inheritance, polymorphisam, abstract classes and composition)

195
1 Inheritance

Upload: farhan-amjad

Post on 06-May-2015

11.594 views

Category:

Education


3 download

DESCRIPTION

All important topics on one presentation...........

TRANSCRIPT

Page 1: Inheritance, polymorphisam, abstract classes and composition)

1

Inheritance

Page 2: Inheritance, polymorphisam, abstract classes and composition)

Encapsulation Data Hiding Inheritance Polymorphism Reuse

Page 3: Inheritance, polymorphisam, abstract classes and composition)

• In our daily lives we use the concept of classes divided

into subclasses, for example vehicles are divided into cars,

trucks, buses and motor cycles.

• The principle in this sort of division is that each sub-class shares

some common features with the base class from which it

is derived, but also has its own particular features.

Vehicle

wheels

engineCar

wheels

engine

trunk

Truck

wheels

engine

trailertrunk trailer

base class

sub-classes or

derived classes

Page 4: Inheritance, polymorphisam, abstract classes and composition)

A class defines the behavior of a set of objects◦ E.g., Vehicle, Animals, Books, etc.

There may be another set of objects that posses a specialization of this behavior◦ E.g., Cars, Jeeps, Busses, Reptiles, Notebooks

etc Only define the specialization while

reusing the existing code.

Page 5: Inheritance, polymorphisam, abstract classes and composition)
Page 6: Inheritance, polymorphisam, abstract classes and composition)
Page 7: Inheritance, polymorphisam, abstract classes and composition)
Page 8: Inheritance, polymorphisam, abstract classes and composition)
Page 9: Inheritance, polymorphisam, abstract classes and composition)

A tandem bicycle is a kind of bicycle◦ Bicycle with two seats

A mountain bicycle is a kind of bicycle ◦ Bicycle with shocks

A racing bicycle is a kind of bicycle◦ Lightweight aerodynamic construction

Tandem, mountain, and racing bicycles are specialized bicycles

Page 10: Inheritance, polymorphisam, abstract classes and composition)

Be able to create specialized program objects without starting from scratch

Inheritance is the object-oriented programming mechanism for specialization

Page 11: Inheritance, polymorphisam, abstract classes and composition)
Page 12: Inheritance, polymorphisam, abstract classes and composition)

Ability to define new classes of objects using existing classes as a basis

The new class inherits the attributes and behaviors of the parent classes

New class is a specialized version

of the parent class

Bicycle

Mountain Bikes

Racing Bikes

Tandem Bikes

is-a relationships

Page 13: Inheritance, polymorphisam, abstract classes and composition)

A natural way to reuse code◦ Programming by extension rather than reinvention◦ Object-oriented paradigm is well-suited for this style

ofprogramming

Terminology◦ Base class (superclass)◦ Derived class (subclass) Bicycle

MountainBikes

RacingBikes

TandemBikes

is-a relationships

Page 14: Inheritance, polymorphisam, abstract classes and composition)

Base class

Derived

class

Derived

class

Shape class

Triangle

class

Circle

class

Right-angle

Triangle

Equilateral

Triangle

Isosceles

Triangle

Page 15: Inheritance, polymorphisam, abstract classes and composition)

Specialization goes down the way.

Generalization goes up the way.

Shape class

Triangle

class

Circle

class

Right-angle

Triangle

Equilateral

Triangle

Isosceles

Triangle

Shape class

Triangle

class

Circle

class

Right-angle

Triangle

Equilateral

Triangle

Isosceles

Triangle

Page 16: Inheritance, polymorphisam, abstract classes and composition)

student

print()

year_group()

CR

print()

inherits (isa)

student_id,

year, name

dept,

thesis

Page 17: Inheritance, polymorphisam, abstract classes and composition)
Page 18: Inheritance, polymorphisam, abstract classes and composition)

The simplest example of inheritance requires two classes: a base class and a derived class. ◦ The base class OR super-class does not

need any special syntax. ◦ The derived class OR subclass on the

other hand, must indicate that it’s derived from the base class.

This is done by placing a colon after the name of the derived class, followed by a keyword such as public and then the base class name.

Page 19: Inheritance, polymorphisam, abstract classes and composition)

class derivedClass : public baseClass {

private :

// Declarations of additional members, if needed.

public:

// Declarations of additional members, if needed.

}

The derived class inherits from the base class: all public members (see later)

The additional members defined can have the same name (and type) as

those of the base class (as when some base members are to be redefined)

Page 20: Inheritance, polymorphisam, abstract classes and composition)

class Teacher{ // Base classprivate:

string name;int age, numOfStudents;

public:void setName (string new_name){

name = new_name; }

};class Principal : public Teacher{ // Derived class

string school_name;int numOfTeachers;

public:void setSchool(const string & s_name){

school_name = s_name; }

};

Page 21: Inheritance, polymorphisam, abstract classes and composition)

int main(){

Teacher t1;Principal p1;p1.setName(" Principal 1");t1.setName(" Teacher 1");p1.setSchool(" Elementary School");return 0;

} The p1 object can also access, in addition to its

own member function setSchool(), the member function from Parent (Base), which is setName().

Page 22: Inheritance, polymorphisam, abstract classes and composition)

We have seen two access modes in C++ classes: public and private◦ Public members are directly accessible by users

of the class◦ Private members are NOT directly accessible by

users of the class, not even by inheritors

There is a 3rd access mode: protected◦ Protected members are directly accessible by

derived classes but not by other users

Page 23: Inheritance, polymorphisam, abstract classes and composition)

class Shape {

protected:

int width, height;

public:

void setDims (int a, int b){

width=a; height=b;}

}; class Rectangle: public Shape {

public:

int area ( ) {

return (width * height);

}

};

class Triangle: public Shape {

public:

int area ( ) {

return (width * height/2);

}

};

class Square: public Rectangle {

public:

void setDims (int a){

width=a; height=a;}

};

Page 24: Inheritance, polymorphisam, abstract classes and composition)

An object of a derived class inherits all the member data and functions of the base class. ◦ Private members

Private members: of the base class are inherited by the derived class, but they are not visible in the derived class. The derived class may access them only through the public interface of the base class.

Not visible in the derived class. The derived class may access them only through

the public interface of the base class. ◦ Protected Data members

Visible in the derived class. The derived class may access protected members

directly.

Page 25: Inheritance, polymorphisam, abstract classes and composition)

Some members (data or function) of the base class may not be suitable for the derived class. These members should be redefined in the derived class.

For example, assume that the Teacher class has a print function that prints properties of teachers on the screen. But this function is not sufficient for the class Principal, because principals have more properties to be printed. So the print function must be redefined.

Page 26: Inheritance, polymorphisam, abstract classes and composition)

class Teacher{ // Base classprivate: string name;int age, numOfStudents;

public:void setName (const string & new_name){

name = new_name; }void print() const;

};

void Teacher::print() const {cout << "Name: " << name<< " Age: " << age

<< endl;cout << "Number of Students: " << numOfStudents

<< endl;}

Page 27: Inheritance, polymorphisam, abstract classes and composition)

class Principal : public Teacher{ // Derived classstring school_name;int numOfTeachers;

public:void setSchool(const string & s_name){

school_name = s_name; } void print() const;

};

void Principal::print() const {

// We can write this only if base class has protected data members

//cout << "Name: " << name << " Age: " << age << endl;//cout << "Number of Students: " << numOfStudents << endl;cout << "Name of the school: " << school_name << endl;

}

Page 28: Inheritance, polymorphisam, abstract classes and composition)

print() function of the Principal class overrides (hides) the print() function of the Teacher class. Now the Principal class has two print() functions. The members of the base class can be accessed by using the scope operator (::).

void Principal::print() const // Print method of Principal class

{Teacher::print();

// invokes the print function of the teacher class

cout << "Name of the school: " << school_name;}

Page 29: Inheritance, polymorphisam, abstract classes and composition)

A class can have public or private members

Once inheritance enters the picture, other access possibilities arise for derived classes. In addition to private and public members a base class can also have protected members

Derived class inherits all members of base class whether public, private, or protected. Member functions of a derived class can access public and protected members of the base class, but not private members.

Objects of a derived class in main can access only public members of the base class.

Page 30: Inheritance, polymorphisam, abstract classes and composition)

Inheritance Type Base class member access

Derived class member access

public public

public protected protected

private inaccessible

public protected

protected protected protected

private inaccessible

public private

private protected private

private inaccessible

Page 31: Inheritance, polymorphisam, abstract classes and composition)

class Animal{ protected;

string str;public:

Animal(const string s) : str(s) {}; void Display() { cout << ”Animal : ” << str << endl; }

};

Page 32: Inheritance, polymorphisam, abstract classes and composition)

class Dog : public Animal

{

public:

Dog(const string s) : Animal(s) {};

void Display() { cout << ”Dog : ” << str << endl; }

};

class Cat : public Animal

{

public:

Cat(const string s) : Animal(s) {};

void Display() { cout << ”Cat : ” << str << endl; }

};

Page 33: Inheritance, polymorphisam, abstract classes and composition)

Animal *zoo[3]; // defines an array of pointers to Animal

Animal animal(”Flipper”); // creates an Animal object

Dog dog(”Lassie”); // creates a Dog object

Cat cat(”Jerry”); // creates a Cat object

zoo[0] = &animal; // pointer zoo[0] points to animal

zoo[1] = &dog; // pointer zoo[0] points to animal

zoo[2] = &cat; // pointer zoo[0] points to animal

for (int i=0; i<3; i++)

zoo[i]->Display(); // static binding to Animal::Display()

Output:

Animal : Flipper

Animal : Lassy

Animal : Jerry

Page 34: Inheritance, polymorphisam, abstract classes and composition)

Animal

Display()zoo[0]->Display();

zoo[1]->Display();

zoo[2]->Display();

Dog

Display()

Cat

Display()

Page 35: Inheritance, polymorphisam, abstract classes and composition)
Page 36: Inheritance, polymorphisam, abstract classes and composition)

Inheritance Hierarchy for Person-derived Classes

Page 37: Inheritance, polymorphisam, abstract classes and composition)

37

In multiple inheritance, a derived class inherits members from all its base classes.

Example:class B_class1{public:void setX(int);// ...};class B_class2{public:void setY(int);// ...};

Page 38: Inheritance, polymorphisam, abstract classes and composition)

38

class D_class :public B_class1, public B_class2 {// ...};int main(){D_class d;d.setX(5);d.setY(10);}Under multiple inheritance, a derived class object

contains sub-objects for each of its base classes.

Page 39: Inheritance, polymorphisam, abstract classes and composition)

Multiple inheritance occurs when a class inherits from two or more base classes, like this:

class Base1{public:

int a;void fa1();char *fa2(int);

};

class Base2{public:

int a;char *fa2(int, const char*);int fc();

};

Page 40: Inheritance, polymorphisam, abstract classes and composition)

class Deriv : public Base1 , public Base2{public:

int a;float fa1(float);int fb1(int);

};

int main(){

return 0;}

Page 41: Inheritance, polymorphisam, abstract classes and composition)

In inheritance functions are not overloaded. They are overridden.

Page 42: Inheritance, polymorphisam, abstract classes and composition)

class Common{protected:

int cdata; };

class Box : public Common{ };

class Contents : public Common{ };

class CerealPack : public Box, public Contents{ Private: int data;void func1()

{

}};

Page 43: Inheritance, polymorphisam, abstract classes and composition)

Both Box and Contents inherit from Common, and CerealPack inherits from both Box and Contents.

Recall that each object created through inheritance contains a subobject of the base class.

A Box object and Contents object will contain subobjects of Common,

and a CerealPack object will contain subobjects of Box and Contents,

so a CerealPack object will also contain two Common subobjects, one inherited via Box and one inherited via Contents.

This is a strange situation. There are two Sub-Objects when really there should be one.

Page 44: Inheritance, polymorphisam, abstract classes and composition)

You can fix this using a new keyword, virtual, when deriving Box and Contents from Common

class Common{protected:

int cdata; };

class Box : virtual public Common{ };

class Contents : virtual public Common{ };

class CerealPack : public Box, public Contents{ void func1()

{int temp = cdata; }

};

Page 45: Inheritance, polymorphisam, abstract classes and composition)

The virtual keyword tells the compiler to inherit only one subobject from a class into subsequent derived classes. That fixes the ambiguity problem, but other more complicated problems arise that are too complex to delve into here.

In general, you should avoid multiple inheritance, although if you have considerable experience in C++, you might find reasons to use it in unusual situations.

Page 46: Inheritance, polymorphisam, abstract classes and composition)

Private data◦ Slow and Reliable

◦ Safety Critical Systems Protected data

◦ Fast, author of the derived class is responsible to maintain reliability

◦ Real Time Systems

Page 47: Inheritance, polymorphisam, abstract classes and composition)

Private data: Slow and ReliableIn general, class data should be private. Public data is open to modification by any function anywhere in the program and should almost always be avoided. Protected data is open to modification by functions in any derived class. Anyone can derive one class from another and thus gain access to the base class’s protected data. It’s safer and more reliable if derived classes can’t access base class data directly.

Protected data: Fast, author of the derivedclass is responsible to maintain reliabilityBut in real-time systems, where speed is important, function calls to access private members is a time-consuming process. In such systems data may be defined as protected to make derived classes access data directly and faster.

Page 48: Inheritance, polymorphisam, abstract classes and composition)

class A{ // Base classprivate:

int i; // safepublic:

void access(int new_i){ // public interface to access iif (new_i > 0 && new_i <= 100)i=new_i;

}};class B:public A{ // Derived classprivate:

int k;public:

void set(new_i, new_k) { A::access(new_i); // reliable but slow //…….continuation

}};

Page 49: Inheritance, polymorphisam, abstract classes and composition)

class A{ // Base classprotected:

int i; // derived class can access directlypublic:

:};

class B:public A{ // Derived classprivate:

int k;public:

void set(new_i, new_k){ i=new_i; // fast //……… continuation}

};

Page 50: Inheritance, polymorphisam, abstract classes and composition)

Inheritance is a form of software reusability in which new classes are created from existing classes. The new class will inherit the properties of the base class.Why inheritance?◦ Software reusability saves time◦ Encourages the use of proven software

Page 51: Inheritance, polymorphisam, abstract classes and composition)

51

Multiple inheritance increases opportunities for name conflicts.

Page 52: Inheritance, polymorphisam, abstract classes and composition)

52

Example:class B_class1{public:void f();// ...};class B_class2{public:void f();// ...};

Page 53: Inheritance, polymorphisam, abstract classes and composition)

53

class D_class :public B_class1, public B_class2 {// no local f()};int main(){D_class d;d.f(); // error!d.B_class1::f(); // okd.B_class2::f(); // ok}

Page 54: Inheritance, polymorphisam, abstract classes and composition)

54

In multiple inheritance, constructors and destructors are invoked in the declaration order within the class derivation list.

Page 55: Inheritance, polymorphisam, abstract classes and composition)

55

Example:class B_class1 {public:B_class1(){ cout << ``creating B_class1\n''; }~B_class1(){ cout << ``destroying B_class1\n''; }// ...};class B_class2 {public:B_class2(){ cout << ``creating B_class2\n''; }~B_class2(){ cout << ``destroying B_class2\n''; }// ...};

Page 56: Inheritance, polymorphisam, abstract classes and composition)

56

class D_class : public B_class1, public B_class2{

public:D_class(){ cout << ``creating D_class\n''; }~D_class(){ cout << ``destroying D_class\

n''; }// ...};int main(){D_class d; // output?}

Page 57: Inheritance, polymorphisam, abstract classes and composition)

57

In object-oriented language, a class can obtain the properties of another class through the mechanism of inheritance.

In C++, inheritance is supported by class derivation.

A derived class inherits the properties of its base class.

Page 58: Inheritance, polymorphisam, abstract classes and composition)

58

Page 59: Inheritance, polymorphisam, abstract classes and composition)

59

The class that is being inherited from is called the base class. The new class is called the derived class. The set of the base and derived class instances is called the class inheritance hierarchy.

A derived class inherits data members and member functions of its base class, and can also add its own list of generalization and specialization.

Page 60: Inheritance, polymorphisam, abstract classes and composition)

60

Generalization:Extend the behavior of the base class. Add new

member functions and/or data to derived class. e.g. doResearch() for class Gradstudent.

Specialization:Modify the behavior of the base class. Change

implementations in the derived class without changing the base class interface.

e.g. getPaid() for class Student and Gradstudent.

Page 61: Inheritance, polymorphisam, abstract classes and composition)

61

Benefits of Class Inheritance: Increase software reuse and quality

– Programmers can reuse the base classes instead of

writing new classes– Using well-tested base classes helps reduce bugs in

software.– Reduce code size.

Enhance software extensibility and comprehensibility– Helps support more flexible and extensible architectures.– Useful for modeling and classifying hierarchically related domains.

Page 62: Inheritance, polymorphisam, abstract classes and composition)

62

Single Inheritance– Only one base class per derived class– Form an inheritance tree e.g. Smalltalk, Simula, Java

Multiple Inheritance– More than one base class per derived class– Form an inheritance “Directed Acyclic

Graph”(DAG) e.g. C++, Eiffel

Page 63: Inheritance, polymorphisam, abstract classes and composition)

63

Page 64: Inheritance, polymorphisam, abstract classes and composition)

64

Example:class Pen {public:void SetLocation(int, int);void SetStatus(int);private:int x, y, status; }; //end class declaration.class ColorPen : public Pen {public:void SetColor(int);private:int color;};

Page 65: Inheritance, polymorphisam, abstract classes and composition)

65

class derived_class_name: public base_class_name;

In which public can be replaced by protected and private access specifier to describes the access for the inherited members.

Page 66: Inheritance, polymorphisam, abstract classes and composition)

66

Pen is the base class; ColorPen is the derivedclass. The keyword public indicates here thetype of inheritance is public.

Page 67: Inheritance, polymorphisam, abstract classes and composition)

67

Page 68: Inheritance, polymorphisam, abstract classes and composition)

68

A derived class object consists of sub-objects of its base classes and a derived class portion.

Page 69: Inheritance, polymorphisam, abstract classes and composition)

69

Public MemberIn public inheritance, public members from

the base class are public in its derived classes.

Private MemberPrivate members in a base class are

accessible only in the base class; they are not accessible in its derived classes.

Page 70: Inheritance, polymorphisam, abstract classes and composition)

70

Example:x, y and status from Pen are not accessible in

class ColorPen since they are private members of base class Pen.

Page 71: Inheritance, polymorphisam, abstract classes and composition)

71

Page 72: Inheritance, polymorphisam, abstract classes and composition)

72

Example:class Pen {public:void SetLocation(int, int);void SetStatus(int);private:int x, y, status;};class ColorPen : public Pen {public:void SetColor(int);void setX(int xx){ x = xx; } // Error! x is not

visible in //ColorPenprivate:int color;};

Page 73: Inheritance, polymorphisam, abstract classes and composition)

73

The private member x, y, status in class Pen can be accessed only by the implementors of class Pen.

Page 74: Inheritance, polymorphisam, abstract classes and composition)

74

Without inheritance, protected members in a class act the same as private members. They can be accessed only by the implementor of its own class.

In public inheritance, protected members are different from private members.

– A private member from base class is inaccessible in its derived class.

– A protected member from base class is accessible in its derived class by the derived class implementor.

Page 75: Inheritance, polymorphisam, abstract classes and composition)

75

Example:class Pen {public:void SetLocation(int, int);void SetStatus(int);protected:int x, y;private:int status;};class ColorPen : public Pen {public:void SetColor(int);private:int color;};

Page 76: Inheritance, polymorphisam, abstract classes and composition)

76

Page 77: Inheritance, polymorphisam, abstract classes and composition)

77

void ColorPen::SetColor(){x = 1; // ok: x is protected in Peny = 1; // ok: y is protected in Penstatus = 0; // error: status is invisiblecolor = 2; // ok: color is private in Colorpen}int main(){ColorPen p;p.x = 1; // errorp.y = 1; // errorp.status = 1; // errorp.color = 1; // error}

Page 78: Inheritance, polymorphisam, abstract classes and composition)

78

The protected member x, y from base class can be accessed by the implementors of class Pen, and the implementors of derived class ColorPen.

x, y cannot be accessed by the user of class Pen, or the user of class ColorPen.

Page 79: Inheritance, polymorphisam, abstract classes and composition)

79

Note:

ColorPen has access to the protected members of only one Pen class object: its own Pen sub-object. ColorPen can not access the protected members of other Pen class objects.

Page 80: Inheritance, polymorphisam, abstract classes and composition)

80

Example:class Pen {public:void SetLocation(int, int);void SetStatus(int);protected:int x, y;private:int status; };class ColorPen : public Pen {public:bool CompPosition( Pen &p ){return ( x == p.x && y == p.y ); // Error! } // ...};

Page 81: Inheritance, polymorphisam, abstract classes and composition)

81

The access control of an inherited member can be changed in derived class through the use of using declaration.

Page 82: Inheritance, polymorphisam, abstract classes and composition)

82

Example:class B_class{public:void SetX(int xx){ x = xx; }private:int x;};class D_class : public B_class{private:using B_class::setX; // now setX() is private in

D_class// ... };int main(){D_class d;d.setX(5); // Can’t access, becomes private}

Page 83: Inheritance, polymorphisam, abstract classes and composition)

83

An object of the derived class consists of sub-object

of its base classes and the derived class portion.

To construct an object of the derived class, objects

of the base classes are constructed first.

Page 84: Inheritance, polymorphisam, abstract classes and composition)

84

Page 85: Inheritance, polymorphisam, abstract classes and composition)

85

Example:class B_class {// ...};class D_class : public B_class{// ...};class DD_class : public D_class{// ...};DD_class dd;

Page 86: Inheritance, polymorphisam, abstract classes and composition)

86

Page 87: Inheritance, polymorphisam, abstract classes and composition)

87

In an inheritance hierarchy, constructors execute in a base class to derived class order.

Page 88: Inheritance, polymorphisam, abstract classes and composition)

88

Derived objects are constructed by the following steps:

1. Allocate space for the entire object( base class members and derived class members)

2. Invoke the base class constructor to initialize the base class part of the object.

3. Initialize the members of derived class directed by the derived class constructor initializer

4. Executing the body of the derived class constructor

Page 89: Inheritance, polymorphisam, abstract classes and composition)

89

Now in step 2, which constructor in based class will be invoked?

Page 90: Inheritance, polymorphisam, abstract classes and composition)

90

Example:class B_class {B_class(){ cout << ``Creating B_class\n'';}; // line

1};class D_class : public B_class{public:D_class(){ cout << ``constructing D_class\n''; }};int main(){D_class d; // output?}

Page 91: Inheritance, polymorphisam, abstract classes and composition)

91

Question: What happens if the line 1 is removed?

If the base class has no constructor, then a system provided default constructor for base class is invoked when creating an object of the derived class.

Page 92: Inheritance, polymorphisam, abstract classes and composition)

92

Consider the following code. What is wrong?class B_class {public:B_class(int xx, int yy) : x(xx), y(yy) {}private:int x, y;};class D_class : public B_class{public:D_class( int zz ) : z(zz) {}private:int z; };int main(){D_class d( 10 );// ...}

Page 93: Inheritance, polymorphisam, abstract classes and composition)

93

Solution 1:Provide a default constructor to the base class B class.class B_class {public:B_class(int xx = 0, int yy = 0) : x(xx), y(yy) {}private:int x, y;};class D_class : public B_class{public:D_class( int zz ) : z(zz) {}private:int z;};int main(){D_class d( 10 );}

Page 94: Inheritance, polymorphisam, abstract classes and composition)

94

Solution 2:Explicitly invokes a B class constructor in D class

constructor initializer.class B_class {public:B_class(int xx, int yy) : x(xx), y(yy) {}private:int x, y;};class D_class : public B_class{public:D_class( int zz ) : B_class(0,0), z(zz) {} // here !private:int z;};int main(){D_class d( 10 );}

Page 95: Inheritance, polymorphisam, abstract classes and composition)

95

Example:class Animal{public:Animal() : species("Animal") {}Animal( string s ) : species(s) {}private:string species;};class Primate : public Animal {public:Primate() : Animal( "Primate" ), heart_cham(0) {}Primate( int n ) : Animal( "Primate" ),

heart_cham(n) {}private:int heart_cham;};

Page 96: Inheritance, polymorphisam, abstract classes and composition)

96

class Human : public Primate {public:Human() : Primate(4) {}// ...};int main(){Primate Whazat(7);Human Jenny;// ...}

Page 97: Inheritance, polymorphisam, abstract classes and composition)

97

When a derived class object is destroyed, the derived class portion is destroyed first.

In an inheritance hierarchy, destructors execute in a derived class to base class order, which is the reverse order of constructors.

Page 98: Inheritance, polymorphisam, abstract classes and composition)

98

Example:class B_class {public:B_class(){ cout << ``Creating B_class \n''; }~B_class(){ cout << ``Destroying B_class \n''; }};class D_class : public B_class{public:D_class(){ cout << ``Creating D_class \n''; }~D_class(){ cout << ``Destroying D_class \n''; }};int main(){D_class d; // Output?}

Page 99: Inheritance, polymorphisam, abstract classes and composition)

99

The user defined copy constructor and operator= in derived class should explicitly invoke the copy constructor and operator= of its base class.

Page 100: Inheritance, polymorphisam, abstract classes and composition)

100

Example:D_class::D_class( const D_class& x) : B_class(x)

{// then performs copy for members of derived

class ...}D_class& D_class::operator=( const D_class&

x ){B_class::operator=(x);// then performs copy for members of derived

class ...}

Page 101: Inheritance, polymorphisam, abstract classes and composition)

101

Base class members and its derived class members belong to different class scopes.

The scope of the derived class can be viewed as nested within the scope of its base class.

The name resolution is performed before the access control is performed.

Page 102: Inheritance, polymorphisam, abstract classes and composition)

102

Example:class B_class{protected:string x;int y;private:int z;};class D_class : public B_class{public:void f();private:int x;};

Page 103: Inheritance, polymorphisam, abstract classes and composition)

103

void D_class::f(){x = 1; // okx = "hello!"; // error:// x is resolved to D_class::x// type mismatchB_class::x = "hello!"; // oky = 1; // okz = 1; // error:// z is resolved to B_class::z,// but not accessible.}

Page 104: Inheritance, polymorphisam, abstract classes and composition)

104

If the derived class adds a member with the same name of a member in the base class, the local member hides the inherited member.

Page 105: Inheritance, polymorphisam, abstract classes and composition)

105

Example:class B_class{public:void f(){ /*...*/ }};class D_class : public B_class{public:void f(int){ /*...*/ }};int main(){D_class x;x.f();}Here, B class::f() and D class::f(int) are not

overloaded.

Page 106: Inheritance, polymorphisam, abstract classes and composition)

106

How to overload a set of functions between base class and derived class?

Qualify the base class member with the class scope operator so it become visible in the derived class scope, and therefore the same named functions can be overloaded.

Page 107: Inheritance, polymorphisam, abstract classes and composition)

107

Example:class B_class{public:void f(int);void f(double);};class D_class : public B_class{public:using B_class::f;void f(int);};

Page 108: Inheritance, polymorphisam, abstract classes and composition)

108

The using declaration enters f(int) and f(double) of the base class into the scope of the derived class.

Page 109: Inheritance, polymorphisam, abstract classes and composition)

109

Class derivation has three types: public, protected and private.

class B_class{//...};// protected inheritanceclass D_class : protected B_class{//...};// private inheritanceclass D_class : B_class{ // Caution://... // inheritance is PRIVATE}; // by default !!!

Page 110: Inheritance, polymorphisam, abstract classes and composition)

110

public inheritance

protected inheritance

private inheritance

public member in base class

public in derived class

protected in derived class

private in derived class

protected member in base class

protected in derived class

protected in derived class

private in derived class

private member in base class

hidden in derived class

hidden in derived class

hidden in derived class

Page 111: Inheritance, polymorphisam, abstract classes and composition)

111

A public derivation is also called type inheritance; it is used to model the is-a relationship between the derived class and the base class.

A private derivation is also called implementation inheritance. It does not model the is-a, but the is-implemented-in-terms-of relationship between the derived class and the base class.

Page 112: Inheritance, polymorphisam, abstract classes and composition)

112

Example:class List{public:void InsertAfter (DATATYPE &item);void InsertBefore (DATATYPE &item);void MakeCurrent(int);void RemoveCurrent();bool IsEmpty ();// ...protected:ListNode *head;ListNode *current;ListNode *previous;// ...};

Page 113: Inheritance, polymorphisam, abstract classes and composition)

113

class CircularList : public List{public:void ToForward(int);void ToBackward(int);// ...};class Queue : List{public:void Enqueue( DATATYPE& );void Dequeue();bool IsEmpty();void PrintQueue();};

Page 114: Inheritance, polymorphisam, abstract classes and composition)

114

A CircularList is a List public inheritance.

CircularList supports the public interface of List and two new methods: ToForward(int) and ToBackward(int).

Page 115: Inheritance, polymorphisam, abstract classes and composition)

115

A Queue is implemented in terms of a List private inheritance.

Example:void Queue::Dequeue(){MakeCurrent( 1 );RemoveCurrent();}Queue supports only its own public

interface: Enqueue(), Dequeue(), IsEmpty(), PrintQueue()

Page 116: Inheritance, polymorphisam, abstract classes and composition)

116

In multiple inheritance, a derived class inherits members from all its base classes.

Example:class B_class1{public:void setX(int);// ...};class B_class2{public:void setY(int);// ...};

Page 117: Inheritance, polymorphisam, abstract classes and composition)

117

class D_class :public B_class1, public B_class2 {// ...};int main(){D_class d;d.setX(5);d.setY(10);}Under multiple inheritance, a derived class object

contains sub-objects for each of its base classes.

Page 118: Inheritance, polymorphisam, abstract classes and composition)

118

Multiple inheritance increases opportunities for name conflicts.

Page 119: Inheritance, polymorphisam, abstract classes and composition)

119

Example:class B_class1{public:void f();// ...};class B_class2{public:void f();// ...};

Page 120: Inheritance, polymorphisam, abstract classes and composition)

120

class D_class :public B_class1, public B_class2 {// no local f()};int main(){D_class d;d.f(); // error!d.B_class1::f(); // okd.B_class2::f(); // ok}

Page 121: Inheritance, polymorphisam, abstract classes and composition)

121

In multiple inheritance, constructors and destructors are invoked in the declaration order within the class derivation list.

Page 122: Inheritance, polymorphisam, abstract classes and composition)

122

Example:class B_class1 {public:B_class1(){ cout << ``creating B_class1\n''; }~B_class1(){ cout << ``destroying B_class1\n''; }// ...};class B_class2 {public:B_class2(){ cout << ``creating B_class2\n''; }~B_class2(){ cout << ``destroying B_class2\n''; }// ...};

Page 123: Inheritance, polymorphisam, abstract classes and composition)

123

class D_class : public B_class1, public B_class2{

public:D_class(){ cout << ``creating D_class\n''; }~D_class(){ cout << ``destroying D_class\

n''; }// ...};int main(){D_class d; // output?}

Page 124: Inheritance, polymorphisam, abstract classes and composition)

124

In C++, objects of derived class can be converted to objects of base class implicitly.

Page 125: Inheritance, polymorphisam, abstract classes and composition)

125

Example:class B_class{ /* ... */ };class D_class : public B_class{ /* ... */ };void f(B_class x);int main(){D_class y;f( y );}

Page 126: Inheritance, polymorphisam, abstract classes and composition)

126

An object of derived class is an object of base class, but an object of base class is not an object of derived class.

Similarly, a base class pointer or reference can refer to an object of a derived class, but a derived class pointer or reference can not refer to a base class object.

Page 127: Inheritance, polymorphisam, abstract classes and composition)

127

Example 1:B_class *bptr;D_class *dptr;bptr = new D_class; // okdptr = new B_class; // error

Page 128: Inheritance, polymorphisam, abstract classes and composition)

128

Example 2:void f(B_class *ptr, B_class &ref);void g(D_class *ptr, D_class &ref);B_class b;D_class d;f( &b, b );f( &d, d );g( &b, b ); // ?g( &d, d );

Page 129: Inheritance, polymorphisam, abstract classes and composition)

129

Example: a student - graduate student class hierarchy.

Page 130: Inheritance, polymorphisam, abstract classes and composition)

130

class Student{public:Student();string name();double grade();private:double midterm, final, assignments;string name;};string Student::name(){return name;}

Page 131: Inheritance, polymorphisam, abstract classes and composition)

131

double Student::grade(){// calculate grade based on midterm final and

assignments}class GradStudent : public Student{public:GradStudent();double grade();private:double thesis;};double GradeStudent::grade(){// calculate grade based on midterm, final,

assignments// and THESIS}

Page 132: Inheritance, polymorphisam, abstract classes and composition)

132

A function compare name takes two reference to Student objects.

bool compare_name( Student& s1, Student& s2 ){

return s1.name() < s2.name();}Student s1, s2;GradStudent g1, g2;compare_name( s1, s2 ); // okcompare_name( s1, g1 ); // okcompare_name( g1, g2 ); // ok

Page 133: Inheritance, polymorphisam, abstract classes and composition)

133

Since GradStudent is derived from Student, we can pass a GradStudent object to compare name(). In the function call, the Student reference is bound to the Student portion inside the GradStudent object.

Page 134: Inheritance, polymorphisam, abstract classes and composition)

134

What is the problem here?bool compare_grade( Student& s1,

Student& s2 ){return s1.grade() < s2.grade();}Student s1, s2;GradStudent g1, g2;compare_grade( s1, s2 );compare_grade( s1, g1 ); // ?compare_grade( g1, g2 ); // ?

Page 135: Inheritance, polymorphisam, abstract classes and composition)

135

GradStudent class has redefined the grade() member function.

How can we invoke the correct version based on the actual type of object passed into the function?

Page 136: Inheritance, polymorphisam, abstract classes and composition)

136

Constructors are not inherited, even though they have public visibility.

However, the super reference can be used within the child's constructor to call the parent's constructor.

In that case, the call to parent's constructor must be the first statement. Suppose I have:

class A{A(int x, int y);};class B: public A{.... some declarations};then I try:int s, t;B bObject(s, t);

you have to write : B(int s, int t) : A(s,t) { }

Page 137: Inheritance, polymorphisam, abstract classes and composition)

• Polymorphic code: Code that behaves differently when it acts on objects of different types

• Virtual Member Function: The C++ mechanism for achieving polymorphism

15-

137

Page 138: Inheritance, polymorphisam, abstract classes and composition)

Consider the Animal, Cat, Dog hierarchy where each class has its own version of the member function id( ).

• A pointer to a derived class can be assigned to a pointer to a base class

• A base class pointer can point to derived class objects

Animal *pA = new Cat;

15-

138

Animal

Cat Dog

Poodle

Page 139: Inheritance, polymorphisam, abstract classes and composition)

class Animal{ public: void id(){cout << "animal";}

}class Cat : public Animal{ public: void id(){cout << "cat";}}class Dog : public Animal{ public: void id(){cout << "dog";}}

15-

139

Page 140: Inheritance, polymorphisam, abstract classes and composition)

• Consider the collection of different Animal objects

Animal *pA[] = {new Animal, new Dog,

new Cat}; and accompanying code for(int k=0; k<3; k++) pA[k]->id();

• Prints: animal animal animal, ignoring the more specific versions of id() in Dog and Cat

15-

140

Page 141: Inheritance, polymorphisam, abstract classes and composition)

• Preceding code is not polymorphic: it behaves the same way even though Animal, Dog and Cat have different types and different id() member functions

• Polymorphic code would have printed "animal dog cat" instead of "animal animal animal"

15-

141

Page 142: Inheritance, polymorphisam, abstract classes and composition)

• The code is not polymorphic because in the expression

pA[k]->id() the compiler sees only the type of the

pointer pA[k], which is pointer to Animal

• Compiler does not see type of actual object pointed to, which may be Animal, or Dog, or Cat

15-

142

Page 143: Inheritance, polymorphisam, abstract classes and composition)

Declaring a function virtual will make the compiler check the type of each object to see if it defines a more specific version of the virtual function

15-

143

Page 144: Inheritance, polymorphisam, abstract classes and composition)

If the member functions id() are declared virtual, the code

Animal *pA[] = {new Animal, new Dog, new Cat}; for(int k=0; k<3; k++) pA[k]->id(); will print animal dog cat

15-

144

Page 145: Inheritance, polymorphisam, abstract classes and composition)

How to declare a member function virtual:

class Animal{ public: virtual void id(){cout << "animal";}}class Cat : public Animal{ public: virtual void id(){cout << "cat";}}class Dog : public Animal{ public: virtual void id(){cout << "dog";}}

15-

145

Page 146: Inheritance, polymorphisam, abstract classes and composition)

• In pA[k]->id(), Compiler must choose which version of id() to use: There are different versions in the Animal, Dog, and Cat classes

• Function binding is the process of determining which function definition to use for a particular function call

• The alternatives are static and dynamic binding

15-

146

Page 147: Inheritance, polymorphisam, abstract classes and composition)

• Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually pointed to

• Static binding is done at compile time• Static binding is very efficient

15-

147

Page 148: Inheritance, polymorphisam, abstract classes and composition)

• Dynamic Binding determines the function to be invoked at execution time

• Can look at the actual class of the object pointed to and choose the most specific version of the function

• Dynamic binding is used to bind virtual functions

• More flexible but less efficient than static binding

15-

148

Page 149: Inheritance, polymorphisam, abstract classes and composition)

The class D is derived from two base classes B1, B2:

class D : public B1, public B2 { };

15-

149

B1 B2

D

Page 150: Inheritance, polymorphisam, abstract classes and composition)

• Multiple inheritance may result in the same base class appearing twice in a derived class

• This causes ambiguity, and is a reason why multiple inheritance is rarely used.

15-

150

Page 151: Inheritance, polymorphisam, abstract classes and composition)

D3 inherits two versions of base class B

15-

151

B

D1 D2

D3

Page 152: Inheritance, polymorphisam, abstract classes and composition)

Solving the problem of a multiply-inherited class:◦ When a base class is declared virtual, the

compiler makes sure only one version of the class gets into any derived class

class D1: virtual public B { }; class D2: virtual public B { };

15-

152

Page 153: Inheritance, polymorphisam, abstract classes and composition)

Unit 02 153

What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes

Page 154: Inheritance, polymorphisam, abstract classes and composition)

Unit 02 154

A subclass (programmer) normally has the option of whether to override the methods of a superclass or not.

However, there are situations where the superclass (programmer) would like to force the subclass (programmer) to override a certain method.

This is usually the case if there is no suitable default implementation.

• For example, consider following examples

of Employee and HourlyEmployee

classes.

Page 155: Inheritance, polymorphisam, abstract classes and composition)

• An abstract class is a class that contains no objects that are not members of subclasses (derived classes)

• For example, in real life, Animal is an abstract class: there are no animals that are not dogs, or cats, or lions…

15-

155

Page 156: Inheritance, polymorphisam, abstract classes and composition)

• Abstract classes are an organizational tool: useful in organizing inheritance hierarchies

• Abstract classes can be used to specify an interface that must be implemented by all subclasses

15-

156

Page 157: Inheritance, polymorphisam, abstract classes and composition)

• The member functions specified in an abstract class do not have to be implemented

• The implementation is left to the subclasses• In C++, an abstract class is a class with at

least one abstract member function

15-

157

Page 158: Inheritance, polymorphisam, abstract classes and composition)

• In C++, a member function of a class is declared to be an abstract function by making it virtual and replacing its body with = 0;

class Animal{ public: virtual void id()=0; };• A virtual function with its body omitted and

replaced with =0 is called a pure virtual function, or an abstract function

15-

158

Page 159: Inheritance, polymorphisam, abstract classes and composition)

• Multiple inheritance is the derivation of a new class from more than one base class

• Multiple inheritance is useful in modeling objects that have the characteristics of several different classes

15-

159

Page 160: Inheritance, polymorphisam, abstract classes and composition)

Unit 02 160

class Employee {

public: char name[30];

double payRate; void getdata()

{ cout<<"\n Enter name....";

cin>>name; cout<<"\n Enter payrate..."; cin>>payRate; } virtual double pay()= 0;

void toString() { cout<<"Name is..."<<name<<" payRate is...."<<payRate;

} }; Employee class must be abstract because each type of employee has different pay rate and pay calculation is different. So we make is abstract by making the pay function as pure virtual.

Employee class must be abstract because each type of employee has different pay rate and pay calculation is different. So we make is abstract by making the pay function as pure virtual.

Page 161: Inheritance, polymorphisam, abstract classes and composition)

161

class HourlyEmployee: Employee

{

private:

int hours;

public:

void getdata(){ {

Employee::getdata();

cout<<"\n Enter number of hours

worked....";

cin>>hours; }

void toString()

{ Employee::toString();

cout<<" Hours worked..."<<hours;

}

double pay()

{

return payRate * hours;

}

};

Pay() function must to be

redefined Pay() function must to be

redefined

Page 162: Inheritance, polymorphisam, abstract classes and composition)

Unit 02 162

Clearly, if the pay method is not overridden, by mistake, the pay of the HourlyEmployee will be computed wrongly.

To force the subclass programmer to override this method, the method should be abstract.

Page 163: Inheritance, polymorphisam, abstract classes and composition)

class person{ protected: char name[30]; public: void getname() { cout<<"\n Enter the name...."; cin>>name; } void putname() { cout<<"\n Name is...."<<name; } virtual void getdata()=0; virtual bool is_outstanding()=0;

};

163

We are defining the class person as Abstract class because it provides interface for the class student and professor. The functions getdata() and is_outstanding() are pure virtual and needs to be redefined in each derived class

We are defining the class person as Abstract class because it provides interface for the class student and professor. The functions getdata() and is_outstanding() are pure virtual and needs to be redefined in each derived class

This class saves the data of students and professors which are of person type. The program finds the out_standing students and professors.

This class saves the data of students and professors which are of person type. The program finds the out_standing students and professors.

Page 164: Inheritance, polymorphisam, abstract classes and composition)

class student:public person { private: float gpa; public: void getdata() { person::getname(); cout<<"\n Enter gpa of student..."; cin>>gpa; } bool is_outstanding() { return (gpa>3.75)?true:false; } };

15-

164

We are defining the class person as Abstract class because it provides interface for the class student and professor. The functions getdata() and is_outstanding() are pure virtual and needs to be redefined in each derived class. A student is outstanding if his GPA is more than 3.75 and getdata() fucntion calls getname() function of person class.

We are defining the class person as Abstract class because it provides interface for the class student and professor. The functions getdata() and is_outstanding() are pure virtual and needs to be redefined in each derived class. A student is outstanding if his GPA is more than 3.75 and getdata() fucntion calls getname() function of person class.

Page 165: Inheritance, polymorphisam, abstract classes and composition)

class professor:public person { private: int no_of_pub; public: void getdata() {person::getname(); cout<<"\n Enter number of

publications...."; cin>>no_of_pub; } bool is_outstanding() { return ( no_of_pub>10)?true:false; } };

15-

165

Professor class creates the getdata() function and calls the person class function getname() and number of publications.Professor is outstanding if his publications are more than 10.

Professor class creates the getdata() function and calls the person class function getname() and number of publications.Professor is outstanding if his publications are more than 10.

Page 166: Inheritance, polymorphisam, abstract classes and composition)

int main() { person* ptr[100]; int n=0; char choice; do{ cout<<"\n Enter the data for student or professor....(s/p) "; cin>>choice; if(choice=='s') ptr[n]=new student; else ptr[n]=new professor; ptr[n]->getdata(); n++; cout<<"\n Do you want to enter more (y/n)...."; cin>>choice; }while(choice!='n');

15-

166

Ptr is a pointers array of base class (person). Due to pointer it can point to derived class objects like student and professor.

Ptr is a pointers array of base class (person). Due to pointer it can point to derived class objects like student and professor.

Page 167: Inheritance, polymorphisam, abstract classes and composition)

for(int i=0; i<n; i++) { ptr[i]->putname(); if(ptr[i]->is_outstanding()) { cout<<"\n cha giya "; } else cout<<"\n Farig ha "; } system("pause"); return 0; }

15-

167

Now the array ptr has the record of students and professors. Because of pointers the dynamic binding is applied which needs the function to be defined as virtual. Because of virtual function is_outstanding() is called accordingly.

Now the array ptr has the record of students and professors. Because of pointers the dynamic binding is applied which needs the function to be defined as virtual. Because of virtual function is_outstanding() is called accordingly.

Page 168: Inheritance, polymorphisam, abstract classes and composition)

Unit 02 168

Suppose we wish to write a program that manipulates two-dimensional geometrical shapes, namely:◦ Squares◦ Rectangles and◦ Circles

Suppose further that we want each such shape to tell its name, its area and its perimeter.

Since each of these shapes is a type of shape, it makes sense to have a superclass, Shape, from which the others can be derived.

However, since the computation of area and perimeter for each of these shapes is different, the subclasses should be forced to override these methods. Thus the Shape class must be abstract.

Page 169: Inheritance, polymorphisam, abstract classes and composition)

Unit 02 169

Moreover, since square is a special type of rectangle, we can derive it from the Rectangle class.

Thus, we have a two level hierarchy tree shown as follows:

Page 170: Inheritance, polymorphisam, abstract classes and composition)

Unit 02 170

1. Write a class, TestEmpolyee, that has the following methods:a. printPay(Employee emp) : that prints the name and salary of emp.b. a main method that creates an instance of MonthlyEmployee and that of HourlyEmployee, and prints them using printPay method.

2. Write a class, Manager, that extends MonthlyEmployee, and has another instance variables, bonus and an instance method, setBonus. Override the pay method and update TestEmployee accordinly.

Page 171: Inheritance, polymorphisam, abstract classes and composition)

1. Write a class, TestShapes, that creates an instance of each of our shape classes and prints it.

2. Suppose you wish to write an application that stores information about reading materials in a BookShop. Reading materials could be books, journals, magazines and newspapers. Identify the classes (including abstract classes, if any) that are needed for this application. Draw a class diagram for the classes and implement them.

15-

171

Page 172: Inheritance, polymorphisam, abstract classes and composition)

Unit 01 172

Write each of the following classes:

1. A class, Person, with an instance variable, name. Write appropriate accessor and mutator methods for the class. Also write a toString method for the class.

2. Employee, that extends Person, and has: two additional instance variables, ID and payRate; and an additional method, pay, that returns the payRate. Override the toString method accordingly.

3. Faculty, that extends Employee, and has two additional variables; officeHours of type String and teachingCourses, which is an array of Courses. Each course has course code and course title. Write appropriate accessor, mutator and toString methods for the class.

4. TestPersons, that has two methods: void printPerson(Person p) that prints its argument. And a main method that creates an instance of each of the above classes and prints it using printPerson method.

Page 173: Inheritance, polymorphisam, abstract classes and composition)

Unit 02 173

Suppose we wish to write a program that manipulates two-dimensional geometrical shapes, namely:◦ Squares◦ Rectangles and◦ Circles

Suppose further that we want each such shape to tell its name, its area and its perimeter.

Since each of these shapes is a type of shape, it makes sense to have a superclass, Shape, from which the others can be derived.

However, since the computation of area and perimeter for each of these shapes is different, the subclasses should be forced to override these methods. Thus the Shape class must be abstract.

Page 174: Inheritance, polymorphisam, abstract classes and composition)

174

Composition is another way to relate two classesIn composition, one or more members of a class are

objects of another class typeComposition is a “has-a” relation between classes Since constructors cannot be called like other

functions (no data type), the calls to constructors of member objects will be specified in the heading of definition of class’s constructor

In composition, we use the member object name to invoke the functions and constructors of the object

Page 175: Inheritance, polymorphisam, abstract classes and composition)

175

Inheritance is appropriate where a class can be said to be ‘a kind of’ other class

Car

VehicleA car is ‘a kind of’ vehicle

car class can inherit from

vehicle class

Page 176: Inheritance, polymorphisam, abstract classes and composition)

176

Inheritance is appropriate where a class can be said to be ‘a kind of’ other class

Car

Wheel

A wheel isn’t ‘a kind of’ car.

A wheel is ‘a part of’ a car

- this is dealt with by aggregation

which is this week’s topic

Page 177: Inheritance, polymorphisam, abstract classes and composition)

177

Define class studentType with composition

class studentType{ public:

void setStudent(string first, string last, int sid);

void print() const;studentType(string first=“”, string last=“”,

int sid=0); private:

personType name;int SID;

};

Page 178: Inheritance, polymorphisam, abstract classes and composition)

178

Implementation of the functions of class studentType

void studentType::setStudent(string first, string last, int sid){ name.setName(first, last); SID = sid;}void studentType::print() const{ name.print(); cout<<“The student ID is ”<<SID<<endl;}studentType::studentType(string first, string last, int sid)

:name(first, last){ SID = sid;}

Page 179: Inheritance, polymorphisam, abstract classes and composition)

A classification hierarchy shows how classes inherit from each other and shows the position in the hierarchy as 'a kind of' relationship.

i.e. An Estate car is 'a kind of' car and a car is 'a kind of' vehicle.

Associations also form hierarchies but they are very different from inheritance. These are described by the following terms

Aggregation Composition Part-Whole A Part Of (APO) Has a Containment

Page 180: Inheritance, polymorphisam, abstract classes and composition)

In this type of hierarchy, classes do not inherit from other classes but are composed of other classes

This means that an object of one class may have it's representation defined by other objects rather than by attributes.

The enclosing class does not inherit any attributes or methods from these other included classes.

This means that this is a relationship (association) between objects.

An object of the enclosing class is composed wholly or partly of objects of other classes

Any object that has this characteristic is know as an aggregation

Page 181: Inheritance, polymorphisam, abstract classes and composition)

With aggregation we sometimes have to call parametrised constructors

When an object is created, any contained objects must be created at the same time.

Some objects may not have parametrised constructors so this is easy

If some objects do need parameters in the constructors some mechanism is required to pass the parameter to the associated objects.

This is done using the colon (:) operator as shown in the following example

Page 182: Inheritance, polymorphisam, abstract classes and composition)

class Wheel

{

private:

int diameter;

public:

Wheel(int diameter_in){ diameter = diameter_in; }

int getDiameter() { return diameter; }

};

class Engine

{

private:

int cc;

public:

Engine(int cc_in) { cc = cc_in; }

int getCC()return cc; }

};

Page 183: Inheritance, polymorphisam, abstract classes and composition)

class Car

{

private:

Wheel nearside_front, offside_front,

nearside_rear, offside_rear;

Engine engine;

int passengers;

public:

Car(int diameter_in, int cc_in, int passengers_in);

void showSelf();

};

Page 184: Inheritance, polymorphisam, abstract classes and composition)

Car::Car(int diameter_in, int cc_in, int passengers_in) :

nearside_front(diameter_in),

offside_front(diameter_in),

nearside_rear(diameter_in),

offside_rear(diameter_in),

engine(cc_in)

{

passengers = passengers_in;

}

Page 185: Inheritance, polymorphisam, abstract classes and composition)

15-

185

Page 186: Inheritance, polymorphisam, abstract classes and composition)

HL 186

1. Main component / sub component

Composition

Aggregation

"contains " relation

"has" relation: Main component "has" a sub

component as a part.

2. Association

"owns"

"knows"

"uses"

"serves“

“observes”

etc.

3. Generalisation / specialisation

Inheritance

General case / Special case

Super class / Sub class

Base class / Derived class

Base class / Inherited class

"is" relation. An instance of derived class "is" an

instance of base class.

Page 187: Inheritance, polymorphisam, abstract classes and composition)

HL 187

Composition means same as

- contains

- is part of

Example 1.

Point can be a sub component of Circle.

class Circle {

public:

Circle(float x0=0.0, float y0=0.0, r0=0.0);

Circle(const Point &cp0, r0=0.0);

private:

float r;

Point cp; //sub component

};

Constuctor implementation on the next page.

Example 2.

Date can be used as a data member in class Person:

class Person {

public:

Person(const string& name, int year,

int month, int day);

Person(const string& name, const Date& bd);

private:

string name; // sub component 1

Date birthDay; // sub component 2

};

Page 188: Inheritance, polymorphisam, abstract classes and composition)

HL 188

An object can have an object as a subcomponent.

In this example circles have a point as a

subcomponent (representing their center point */

#include <iostream.h>

//Class definition of Point

class Point {

public:

Point(float x0=0.0, float y0=0.0);

void read();

void print();

private:

float x;

float y;

};

//Class definition of Circle

class Circle{

public:

Circle(float x0=0.0, float y0=0.0, float r0=0.0);

Circle(const Point &cp0, float r0=0.0);

void read();

void print();

private:

float radius;

Point centerPoint;

};

Page 189: Inheritance, polymorphisam, abstract classes and composition)

HL 189

//Application

void main (void) {

Point p(11.0, 12.0);

Circle c1, c2(1.0, 2.0, 10.0), c3(p, 20.0);

c1.print();

c2.print();

c3.print();

}

//Member function implementations

//Member functions of Point

Point::Point(float x0, float y0) {

x = x0;

y = y0;

}

void Point :: read() {

cout << "\n x-coordinate: ";

cin >> x;

cout << " y-coordinate: ";

cin >> y;

}

void Point :: print() {

cout << "(" << x << "," << y << ")";

}

Page 190: Inheritance, polymorphisam, abstract classes and composition)

HL 190

//Member functions of Circle

//Constructor 1 version 1

Circle::Circle(float x0, float y0, float r0): centerPoint(x0, y0){

radius = r0;

}

/* Constructor 1 version 2 (Point constructor is called twice)

Circle::Circle(float x0, float y0, float r0) {

radius = r0;

centerPoint = Point(x0, y0);

} */

//Constructor 2 version 1

Circle::Circle(const Point &cp0, float r0):centerPoint(cp0){

radius = r0;

}

/*Constructor 2 version 2 (Point constructor is called twice)

Circle::Circle(const Point &cp0, float r0) {

radius = r0;

centerPoint = Point(cp0); //Default copy constructor of Point

} */

void Circle :: read() {

cout << "\nEnter radius : ";

cin >> radius;

cout << "Enter centerpoint :";

centerPoint.read();

}

void Circle :: print() {

cout << "\nThe circle data is :";

cout << "\nRadius is : " << radius;

cout << "\nCenter point is :";

centerPoint.print();

}

Page 191: Inheritance, polymorphisam, abstract classes and composition)

HL 191

Memory allocation is done at the same time (as one block of memory) for the main component and for sub component, because sub

component is part of the memory area of the main component.

The object is created in the following phases:

1. Memory is allocated for the whole object.

2. The constructor call of sub component class that is defined in the initialisation list of main component is executed. If there is no sub

component constructor call in the initialisation list, the default constructor of sub component is called.

3. The function body of main component constructor is called.

This rule of order means that it is guaranteed that sub component is already constructed when constructor body on main component is

executed.

It also guarantees that sub components are always initialised at least with default constructor of sub component class.

This is useful as you can understand thinking about the differences between defining a name member in the Person class as char *name

or

string name!. The implementor of the Person class needs to do many things in the former case but almost nothing in the latter case.

Page 192: Inheritance, polymorphisam, abstract classes and composition)

HL 192

When an object containing a sub-component object is de-allocated, the things happen in the following order:

1.The destructor of the main component is called first.

2. The destructor of the sub-component is called next.

3. The memory area is de-allocated as a whole.

The default assignment operator of the main component:

If we don’t overload the assignment operator in the main component class the default assignment is used. If we have overloaded

assignment for the sub-component that is used in the default assignment when sub-component part is copied.

The default copy constructor of the main component:

If we don’t write the copy constructor for the main component class the default copy constructor is used. If we have written copy

constructor for the sub-component it is used in the default copy constructor when sub-component part is constructed.

Page 193: Inheritance, polymorphisam, abstract classes and composition)

Unit 02 193

1. Write a class, TestEmpolyee, that has the following methods:a. printPay(Employee emp) : that prints the name and salary of emp.b. a main method that creates an instance of MonthlyEmployee and that of HourlyEmployee, and prints them using printPay method.

2. Write a class, Manager, that extends MonthlyEmployee, and has another instance variables, bonus and an instance method, setBonus. Override the pay method and update TestEmployee accordinly.

Page 194: Inheritance, polymorphisam, abstract classes and composition)

1. Write a class, TestShapes, that creates an instance of each of our shape classes and prints it.

2. Suppose you wish to write an application that stores information about reading materials in a BookShop. Reading materials could be books, journals, magazines and newspapers. Identify the classes (including abstract classes, if any) that are needed for this application. Draw a class diagram for the classes and implement them.

15-

194

Page 195: Inheritance, polymorphisam, abstract classes and composition)

Unit 01 195

Write each of the following classes:

1. A class, Person, with an instance variable, name. Write appropriate accessor and mutator methods for the class. Also write a toString method for the class.

2. Employee, that extends Person, and has: two additional instance variables, ID and payRate; and an additional method, pay, that returns the payRate. Override the toString method accordingly.

3. Faculty, that extends Employee, and has two additional variables; officeHours of type String and teachingCourses, which is an array of Courses. Each course has course code and course title. Write appropriate accessor, mutator and toString methods for the class.

4. TestPersons, that has two methods: void printPerson(Person p) that prints its argument. And a main method that creates an instance of each of the above classes and prints it using printPerson method.