Anda di halaman 1dari 6

Inheritance in C++

One of the most important concepts in

functions from multiple base classes. To

object-oriented programming is that of

define a derived class, we use a class

inheritance. Inheritance allows us to define a

derivation list to specify the base class(es).

class in terms of another class, which makes

A class derivation list names one or more

it

base classes and has the form:

easier

application.

to

create
This

and
also

maintain
provides

an
an

opportunity to reuse the code functionality

class derived-class: access-specifier base-

and fast implementation time.

class

When creating a class, instead of writing

Where access-specifier is one of public,

completely new data members and member

protected, or private, and base-class is the

functions, the programmer can designate

name of a previously defined class. If the

that the new class should inherit the

access-specifier is not used, then it is private

members of an existing class. This existing

by default.

class is called the base class, and the new


class is referred to as the derived class.

Consider a base class Shape and its derived


class Rectangle as follows:

The idea of inheritance implements the is


a relationship. For example, mammal IS-A

#include <iostream>

animal, dog IS-A mammal hence dog IS-A


animal as well and so on.

Base & Derived


Classes:

using namespace std;

// Base class
class Shape
{

A class can be derived from more than one


classes, which means it can inherit data and

public:

void setWidth(int w)

Rectangle Rect;

{
Rect.setWidth(5);

width = w;

Rect.setHeight(7);

}
void setHeight(int h)

// Print the area of the object.

cout << "Total area: " << Rect.getArea()

height = h;

<< endl;

}
protected:

return 0;

int width;
int height;
};

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

// Derived class
class Rectangle: public Shape

Total area: 35

Access Control and

{
public:
int getArea()
{

Inheritance:
A derived class can access all the non-

return (width * height);


}
};

private members of its base class. Thus


base-class members that should not be
accessible to the member functions of
derived classes should be declared private in
the base class.

int main(void)
{

We can summarize the different access types

We

hardly

according to who can access them in the

use protected or private inheritance,

following way:

but public inheritance is commonly used.


While using different type of inheritance,

Access
Same
class

public

protected private

yes

yes

yes

following rules are applied:

Derived
classes
Outside
classes

yes

yes

from

a public base

become public members of the derived class


and protected members of the base class

yes

no

become protected members of the derived

no

class. A base class's private members are


never accessible directly from a derived
class, but can be accessed through calls to

methods with the following exceptions:

the public andprotected members

constructors of the base class.

of

the

base class.

Constructors, destructors and copy

Protected

deriving

Overloaded operators of the base

class

class, public members of the base class

no

A derived class inherits all base class

Public Inheritance: When deriving

from

Inheritance: When
a protected base

class, public and protectedmembers of the

class.

base class become protected members of


The friend functions of the base

the derived class.

class.
Private Inheritance: When deriving

Type of Inheritance:
When deriving a class from a base class, the
base class may be inherited through public,
protected orprivate inheritance. The type of
inheritance is specified by the accessspecifier as explained above.

from

a private base

class, public and protected members of the


base class become private members of the
derived class.

Multiple Inheritances:

{
height = h;

A C++ class can inherit members from more

than one class and here is the extended

protected:

syntax:

int width;
int height;

class derived-class: access baseA, access


baseB....
Where

};
access

is

one

of public,

protected, or private and would be given

// Base class PaintCost

for every base class and they will be

class PaintCost

separated by comma as shown above. Let us

try the following example:

public:
int getCost(int area)

#include <iostream>

{
return area * 70;

using namespace std;


}
// Base class Shape

};

class Shape
// Derived class

{
public:
void setWidth(int w)

class Rectangle: public Shape, public


PaintCost
{

{
width = w;
}
void setHeight(int h)

public:
int getArea()
{

return (width * height);


}

cout << "Total area: " << Rect.getArea()


<< endl;

};
// Print the total cost of painting
cout << "Total paint cost: $" <<

int main(void)

Rect.getCost(area) << endl;

{
Rectangle Rect;

return 0;

int area;
}
Rect.setWidth(5);
Rect.setHeight(7);

area = Rect.getArea();

When the above code is compiled and


executed, it produces the following result:
Total area: 35
Total paint cost: $2450

// Print the area of the object.

Anda mungkin juga menyukai