Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Inheritance

C++ Inheritance

One of the most important concepts in object-oriented programming is inheritance. Inheritance allows us to define a class based on another class, making it easier to create and maintain an application. This also achieves the effect of reusing code functionality and improving execution efficiency.

When creating a class, you do not need to rewrite new data members and member functions; you only need to specify that the new class inherits the members of an existing class. This existing class is calledbase class, and the newly created class is calledderived class.

Inheritance represents a is a relationship. For example, a mammal is an animal, a dog is a mammal, therefore a dog is an animal, and so on.

The code is as follows:

// Base class

class

Animal

{

   

// eat() function

   

// sleep() function

}

;

// Derived class

class

Dog

:

public

Animal

{

   

// bark() function

}

;

Base Class & Derived Class

A class can be derived from multiple classes, meaning it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base classes. The class derivation list names one or more base classes, in the form:



class derived-class: access-specifier base-class

where the access modifier access-specifier is public, protected or private one of them, and base-class is the name of a previously defined class. If the access modifier access-specifier is not used, it defaults to private.

Suppose there is a base class Shape, Rectangle which is its derived class, as shown below:

Example

#include

<

iostream

using

namespace

std

;

// Base class

class

Shape

{

public

:

void

setWidth

(

int

w

)

{

width

=

w

;

}

void

setHeight

(

int

h

)

{

height

=

h

;

}

protected

:

int

width

;

int

height

;

}

;

// Derived class

class

Rectangle

:

public

Shape

{

public

:

int

getArea

(

)

{

return

(

width

height

)

;

}

}

;

int

main

(

void

)

{

Rectangle

Rect

;

Rect

.

setWidth

(

5

)

;

Rect

.

setHeight

(

7

)

;

// Output the area of the object

cout

<<

"

Total area:

"

<<

Rect

.

getArea

(

)

<<

endl

;

return

0

;

}

When the code above is compiled and executed, it produces the following result:



Total area: 35

Access control and inheritance

A derived class can access all non-private members of the base class. Therefore, if a base class member does not want to be accessed by the derived class's member functions, it should be declared private in the base class.

We can summarize different access types based on access permissions, as shown below:

Access the public protected private
Same class yes yes yes
derived class yes yes no
Classes outside yes no no

A derived class inherits all base class methods, except in the following cases:

Types of inheritance

When a class is derived from a base class, the base class can be inherited as public, protected or **private** types. The inheritance type is specified by the access modifier access-specifier explained above.

We almost never use protected or **private** inheritance; we usually use public inheritance. When using different types of inheritance, follow these rules:

Multiple inheritance

Multiple inheritance means a child class can have multiple parent classes; it inherits the characteristics of multiple parent classes.

A C++ class can inherit members from multiple classes, with the syntax:



class <派生类名>:<继承方式1><基类名1>,<继承方式2><基类名2>,…

{

<派生类类体>

};



where the access modifier inheritance method is public, protected or private one of them, used to modify each base class; the base classes are separated by commas as shown above. Now let us look at the example below:

Example

#include

<

iostream

using

namespace

std

;

// Base class Shape

class

Shape

{

public

:

void

setWidth

(

int

w

)

{

width

=

w

;

}

void

setHeight

(

int

h

)

{

height

=

h

;

}

protected

:

int

width

;

int

height

;

}

;

// Base class PaintCost

class

PaintCost

{

public

:

int

getCost

(

int

area

)

{

return

area

70

;

}

}

;

// Derived class

class

Rectangle

:

public

Shape

,

public

PaintCost

{

public

:

int

getArea

(

)

{

return

(

width

height

)

;

}

}

;

int

main

(

void

)

{

Rectangle

Rect

;

int

area

;

Rect

.

setWidth

(

5

)

;

Rect

.

setHeight

(

7

)

;

area

=

Rect

.

getArea

(

)

;

// Output the area of the object

cout

<<

"

Total area:

"

<<

Rect

.

getArea

(

)

<<

endl

;

// Output total cost

cout

<<

"

Total paint cost: $

"

<<

Rect

.

getCost

(

area

)

<<

endl

;

return

0

;

}

When the code above is compiled and executed, it produces the following result:



Total area: 35

Total paint cost: $2450