0% found this document useful (0 votes)
46 views

L14 Inheritance2020 PDF

This document discusses inheritance in C++. It defines inheritance as allowing a new class to be based on an existing class, inheriting its member variables and functions. Derived classes have the characteristics of the base class plus additional characteristics. The base class is the general parent class, while the derived class is the more specialized child class. Constructors are called from base to derived class, while destructors are called in reverse order. Functions can be redefined in derived classes.

Uploaded by

andrew
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

L14 Inheritance2020 PDF

This document discusses inheritance in C++. It defines inheritance as allowing a new class to be based on an existing class, inheriting its member variables and functions. Derived classes have the characteristics of the base class plus additional characteristics. The base class is the general parent class, while the derived class is the more specialized child class. Constructors are called from base to derived class, while destructors are called in reverse order. Functions can be redefined in derived classes.

Uploaded by

andrew
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

CPSC 122 – Computer Science II

Gonzaga University – Department of Computer Science


Classes Cont.
Inheritance

š allows a new class to be based on an existing class.

š new class inherits all the member variables and functions of the class it is based on
š except the constructors and destructor

š Real world you can find many objects that are specialized versions of other more general
objects.
Inheritance
Inheritance

š When an “is a” relationship exists between classes, it means that the


specialized class has all of the characteristics of the general class,
plus additional characteristics that make it special.

š The base class is the general class (parent)

š The derived class is the specialized class. (child)

class derivedClass : public baseClass


Accessibility

šDerived class can access all public members of the base


class

šDerived class cannot directly access any private


members from the base class
šThey can be accessed by the member functions of the base
class
Protected Members

Protected members of a base class are like private


members, but they may be accessed by derived classes.
Base Class Access
class derivedClass : public baseClass
Check point

š What is the difference between private members and protected members?


Constructors and Destructors

šThe base class’s constructor is called before the derived


class’s constructor.

šThe destructors are called in reverse order, with the


derived class’s destructor being called first.
Rectangle – Cube Example
Calling Base Constructors through Derived
Class
Check point!
Redefining Base Class Functions

š A base class member function may be redefined in a derived class.

Graded Activity Class Curved Activity Class


Class Hierarchy

You might also like