[go: up one dir, main page]

0% found this document useful (0 votes)
20 views26 pages

Lec09 (Topic 5 Inheritance)

Uploaded by

Abc
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)
20 views26 pages

Lec09 (Topic 5 Inheritance)

Uploaded by

Abc
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
You are on page 1/ 26

LECTURE 9

Inheritance (Part 1)
The “is-a” Relationship
• By nature we commonly group objects that share attributes
and/or behaviors into classes/categories.

• A few examples of classes are animals, stationery and human.


• Under each of these classes there can be zero, one, or more
subclasses/subcategories.

• Further, these subclasses can further be broken down into more


specific classes.

• Under the animal class, we can create three subclasses: bird,


mammal and fish.
The “is-a” Relationship
• Note the following statements:
• Eagle is a bird. Parrot is a bird. Both are also animals.
• Cat is a mammal. Monkey is a mammal. Both are also animals.
• Goldfish is a fish. Shark is a fish. Both are also animals.

• A bird is different from a mammal, a mammal is different from a fish,


and a fish is different from a bird.
• Even though they are all different from each other, they are the same in
regard that they are all animals.
Example 2:
• Under the Human class, we can create two subclasses: Lecturer
and Student.

• Sharaf is an instance of Lecturer. Tom is an instance of Student.

• Sharaf and Tom are also human.


Inheritance Terminology
• The "is-a" relationship between a superclass and its subclasses is
commonly referred to as inheritance.

• We say that a subclass "inherits from"/"derives from" a superclass.


Example: Bird inherits from Animal.

• Subclass shares all the characteristics (attributes and methods) of


its superclass.
• All objects of Bird share a set of attributes and methods of Bird,
and also share another set of attributes and/or behaviors of
Animal.
• Example: Birds have feathers, animals have skin, hence birds
also have skin
Definition of Inheritance
• Inheritance is the mechanism which allows a class B Superclass/
to inherit attributes and methods of a class A. We Base class/
say “B inherits from A". Parent class
• Objects of class B thus have access to non-private
attributes and methods of class A.
Is-a
• If class B inherits from class A, then A is called the
superclass/base class/parent class) of B. B is called Subclass/
the subclass/derived class/child class) of A. Derived class/
Child class
• Superclass is more general than subclass
• Subclass is more specific than superclass
Object Relationship Diagrams
• Object Relationship Diagrams (ORD) is used to represent the
inheritance hierarchies between classes.

• We identify an inheritance relationship between two classes using an


arrow pointing from the subclass to the superclass.
Object Relationship Diagrams
We can show multiple subclasses using either of the following
notations:
Object Relationship Diagrams
• Now let's look at how attributes and methods are represented in
inheritance relationships.

• Let's assume that Human share the attribute name, and the method
speak().
Object Relationship Diagrams
• This ORD tells us that all lecturers share attribute room, attribute
name, and method speak().
• attribute name and method speak() inherit from the superclass
Human.

• As there are no additional methods for lecturers, we leave the


methods section (under attributes section) empty.
Object Relationship Diagrams
• Assume that students have additional attribute CGPA and method
take_note().

• Our completed ORD will indicate that:


• All students share the attributes name and CGPA, and the
methods speak() and take_note().
• All lecturers share the attributes name and room, and the
method speak().
Example: Inheritance
• Now we are going to write the C++ class definition for the ORD
below (with constructors added):
Superclass: Human
We begin by declaring our superclass,
Human:
class Human {
public:
Human(string name);
void speak(string
sentence);
private:
string name;
};
Defining subclass
To indicate that a subclass inherits from superclass, we use a single
colon ":", followed by an access privilege keyword and the name of
the superclass in the declaration.

class subclass : <keyword> superclass


{
...
};

Note that subclass does not inherit superclass' constructors and


destructor
Subclass Lecturer
Class Lecturer with inheritance relationship:

class Lecturer : public Human {


public:
Lecturer(string room);
private:
string room;
};
Defining subclasses
• Right now, our subclass constructor takes only a single parameter
room for initialization.

• However, as the subclass Lecturer contains all attributes of


superclass Human, we'd better add parameter name to our
constructor for initialization (unless we want to use default values).

class Lecturer : public Human {


public:
Lecturer (string name,
string room);
private:
string room;
};
Defining subclasses
• Our first attempt to initialize superclass from subclass might look like this:

Lecturer::Lecturer (string name, string room)


{
this->name = name; // error
this->room = room;
}

• However, it'd be an error because name is a private attribute of superclass


and cannot be accessed in subclass.

• To keep with encapsulation and code reuse, we should reuse superclass's


code whenever possible.
Calling Superclass Constructor
To initialize superclass, we should call superclass constructor at
constructor initializer list.

Lecturer::Lecturer (string name, string room)


: Human(name) {
this->room = room;
} "calls" the Human constructor with
the argument name.
Student class
class Student : public
Human {
public:
Student(string name,
float CGPA)
: Human(name) {
this->CGPA = CGPA;
}
void take_note() {...}
private:
float CGPA;
};
The Student constructor in the ORD
misses the attribute name
There is an error here.
Can you find the error?
class Human { class Student : public Human {
string name; float CGPA;
public: public:
Human (string name) {
Student (string name, float CGPA)
this->name = name; }
void speak (string sentence) { : Human(name) {
cout << "My name is " << name this->CGPA = CGPA; }
<< endl << sentence; void take_note() {}
} };
}; int main() {
class Lecturer : public Human {
Human h("Hugo");
string room;
public: Lecturer l("Lee", "BR1111");
Lecturer(string name,string room) Student s("Sarah", 3.99);
: Human(name) { h.speak ("");
this->room = room; l.speak (""); // speak from Human
} s.speak (""); // speak from Human
};
return 0;
Output: }
My name is Hugo
My name is Lee
My name is Sarah
Multiple Levels of Inheritance
• We can have more than one level of inheritance
• For example, the Student class might itself be a superclass for
Local Student and Foreign Student
Multiple Levels of Inheritance

• At each level, the subclasses continue to inherit from their superclass


• Thus, a foreign student is both a student and a human

• Subclasses shares the attributes and methods of all superclass above it


• A foreign student will, therefore, have:
• all of the attributes and methods of Human class,
• plus the additional attributes and/or methods of Student class,
• plus the additional attributes and/or methods of Foreign
Student class
Multiple Inheritance

Multiple inheritance occurs when a class derives from two or


more superclasses.

Use comma "," to separate the surperclasses.

class C : public A, public B {


public:
C() : A(), B() // constructor initialization
list
{}
}; Superclass A Superclass B
Constructor Constructor
for class A for class B

Subclass C
Virtual Inheritance
• Multiple inheritance may introduce diamond inheritance problem,
which arises when two classes B1 and B2 inherit from A, and class C
inherits from both B1 and B2.

• C would have duplicate sets (2 sets) of the members inherited from


A, which might not be desirable.
• We generally just want one set

• This can be avoided through virtual inheritance (also known as virtual


base class).

B1 B2

C
Virtual Inheritance
Problem
A
class A {};
class B1: public A {};
class B2: public A {};
class C: public B1, public B2 B1 B2
{};

Solution: virtual inheritance


class A {}; C
class B1: virtual public A {};
class B2: virtual public A {};
class C: public B1, public B2
{};

You might also like