You are on page 1of 40

Inheritance and Polymorphism

Lecture 2
What will we do today?
●  Learn more about inheritance,
polymorphism, abstraction,
encapsulation

●  Code using polymorphism and


inheritance
Review of Student Class (Lab)
●  Let’s look at the solution!
Inheritance
Inheritance
Inheritance
● Process of forming a new class from an existing class that is from the existing
class called as base class, new class is formed called as derived class.
Polymorphism
Polymorphism
● The ability to use an operator or function in different ways in other words
giving different meaning or functions to the operators or functions is called
polymorphism.
What is inheritance?
●  To make a class
inherit from another
one, use the : public
[parent class] syntax
Some basic syntax of inheritance
Example class for inheritance
Virtual functions (polymorphism)
● A virtual member is a member function that can be redefined in a derived class,
while preserving its calling properties through references. The syntax for a
function to become virtual is to precede its declaration with the virtual keyword!

● virtual int area () { return 0; }


Problem: Mother and daughter
● Write a program with a mother class and
an inherited daughter class. Both of them
should have a method void display () that
prints a message (different for mother and
daughter). In the main define a daughter
and call the display() method on it.
Diagram for mother daughter inheritance

Mother

Means
daughter
derives from
Mother
Daughter
Tips
● Create a mother class

● Create a daughter class that inherits from mother

● Create a method display in both

● Print out the result of display


Constructor Overloading
● Whenwe have the same name for a method but different
parameters!

● Ex. Car() = default constructor

● Car(int fuel, string brand)

● We can call Car() or Car(100, “Audi”)!


Constructor Overloading
● Using
this Constructor you can provide different values to
data members of different objects, by passing the
appropriate values as parameters.

● Pass in more using commas!

● Ex. (int x, string name)


Parameterized Constructors
Parameterized Constructors
● Creating the parameterized constructor
(make sure they aren’t the same as the variable names!)

● Calling the parameterized constructor


Inheriting Constructors
● If we have a parameterized constructor and we make a subclass, we must
inherit the constructor.

● Syntax:

class A {

public: A(int x) {}

};

class B: public A {

B(int x) : A(x) {}

};
Exercise: Let’s make shapes!
Write a program that defines a shape class with a constructor that gives
value to width and height. The define two sub-classes triangle and
rectangle, that calculate the area of the shape area ().

In the main, define two variables a triangle and a rectangle and then call
the area() function in this two variables.
Let’s break it down:
1.  Make a shape class
2.  Add a constructor that takes a width and height
3.  Make 2 new classes: triangle and rectangle
4.  Add a new method area() to both triangle and rectangle
5.  Make a triangle and rectangle in main
6.  Display their areas using area() and cout
Destructors
● Destructor is a special class function which destroys the
object as soon as the scope of object ends. The destructor
is called automatically by the compiler when the object goes
out of scope.
Destructors
class A {

public:

~A(){cout << “destroyed” };

};

No overloading allowed!
Problem: Make a Shape(continued)
● Add a destructor for the shape class!
What did we just learn?
● How to make new subclasses using inheritance

● How to use polymorphism to call the right methods

● How to use different objects using the same parent class!

● Using destructors
What will Square inherit from?
What will Square inherit from?
Is a Rectangle a Shape?
● Yes

● No
Is a Rectangle a Shape?
● Yes

● No
Is a Rectangle a Triangle?
● Yes

● No
Is a Rectangle a Triangle?
● Yes

● No
What does Derived class inherit from Base class?
● Feature A

● Feature B

● Feature C

● Feature D
What does Derived class inherit from Base class?
If additional time…
● Extend the car class to add extra functions (ex. Drive(), setSpeed(),
convertSpeed())

● Use polymorphism on the car class to make different brands of cars

● Add another function to the country class (ex. convertCurrency())


Next time: Laboratory

How to use Code:Blocks in the


laboratory, I/O

You might also like