Docsity

Multiple Inheritance and Containership-Object Oriented Programming-Lecture Slides, Slides of Object Oriented Programming

Partial preview of the text

Download Multiple Inheritance and Containership-Object Oriented Programming-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object Orientedj

Programming using C++

Lecture – 9

Multiple Inheritance &

Containership

Multiple Inheritance

„ So far we have seen classes derived from a

single base classsingle base class

„ However, you may require in your class to

derive the functionality of more than one

class

„ Such inheritance is called Multiple

Usman Younis

Inheritance

Multiple Inheritance

Personal

C ti

Mobile

C ti it

Handheld

Computing Connectivity FlFlexibility ibilit

Smart Electronic

Usman Younis

Smart

phones

(iPhone,

etc.)

Electronic

books

(Kindle,

etc.)

Example

class FirstBase

class SecondBase

public:

int Variable;

float Pi()

return 3.14;

public:

int Variable;

float Pi(int a)

return 3.14*2;

Usman Younis

return 3.14;

void Print()

{ cout<<“First Base”; }

return 3.14 2;

void Print2nd()

{ cout<<“Second Base”; }

Overloading Vs. Overriding

„ Overloading ‰ DiffDiff erent functionality is performed using thet f ti lit i f d i th functions having a same name ‰ However, they are differentiated with number/types of arguments ‰ E.g., void function();

Usman Younis

‰ void function(); ‰ void function(int a); ‰ void function(int a, int b);

Overloading Vs. Overriding

„ Overriding ‰ FF unction in a derived class that has the sameti i d i d l th t h th name and parameter list (usually) as a function in the base class ‰ Typically used to replace a function in base class with different actions in derived class ‰ When a function is overridden all objects of

Usman Younis

‰ When a function is overridden, all objects of derived class use the overriding function ‰ You can use the original function in the base class, by using the scope resolution operator

Overloading Vs. Overriding

Another Example:

class A

void main()

class A {

public:

float Pi() { return 3.14F; }

class B: public A

B Object;

cout<<Object.Pi()<<endl;

//You have to use this

cout<<Object.Pi(3)<<endl;

Error!!

Usman Younis

public:

float Pi(int a) { return 68.78F; }

cout Object.Pi(3) endl;

Ambiguity in Multiple Inheritance

Case 1: class Derived: public A, public B { class A { public: float Pi() { return 3.14; } };

void main() { Derived Object;

cout<<Object.Pi()<<endl; Error!!

Usman Younis

class B { public: float Pi() { return 3.14; } };

//Use cout<<Object.A::Pi()<<endl; //OR cout<<Object.B::Pi()<<endl; }

Ambiguity in Multiple Inheritance

Diamond Shaped Inheritance Tree

„ Object of substance

Matter

Solid Liquid Gas

Object of substance

will contain sub-

objects of Solid,

Liquid, and Gas.

„ Each sub-object will

contain further sub-

object of Matter.

Usman Younis

Substance

„ Therefore, object of

substance will contain

THREE objects of

matter inherited from

different base classes,

Ambiguous!

Ambiguity in Multiple Inheritance

Diamond Shaped Inheritance Tree (Solution)

class Matter { public: float mass; };

class Liquid: virtual public Matter { public: float Viscosity; };

Usman Younis

class Solid: virtual public Matter { public: float lattice_constant; };

class Gas: virtual public Matter { public: float pressure; };

Ambiguity in Multiple Inheritance

„ The virtual keyword tells the compiler to inherit only one sub object from a class intoinherit only one sub-object from a class into subsequent derived classes, which fixes the ambiguity problem.

„ In general, you should avoid such multiple

Usman Younis

inheritance; however, if you have considerable experience in C++ you might find reasons to use it in unusual situations.

Containership: Classes within classes

Containership

Aggregation: „ A class has object/objects of other classes l h b b f h l as members

„ A class aggregates class B, if A has an instance of B

„ Represents a Weak “has-a” relationship

Usman Younis

Represents a Weak has a relationship ‰ Entity can exist with out the composite „ E.g., ‰ An office “has-a” telephone

Containership

Composition: „ A class has object/objects of other classes l h b b f h l as members

„ Represents a Strong “has-a” relationship ‰ Entity cannot exist with out the composite „ E.g.,

Usman Younis

„ E.g., ‰ A Car “has-an” Engine

Redefining Access Specification

Redefining Access Specification

„ Public members of the base class would have private/protected access in the derived classprivate/protected access in the derived class, depending upon the access specifier used, i.e., :private, or :protected

Public Private

Base Derived

Private

Usman Younis

Base Derived

Public Protected

Base Derived

Protected