You are on page 1of 5

Access Control and Inheritance:

A derived class can access all the non-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.
We can summarize the different access types according to who can access them in the following
way:
Access public protected private
Same class yes yes yes
Derived classes yes yes no
Outside classes yes no no
A derived class inherits all base class methods with the following exceptions:
Constructors, destructors and copy constructors of the base class.
Overloaded operators of the base class.
The friend functions of the base class.
Type of Inheritance:
When deriving a class from a base class, the base class may be inherited through public,
protected or private inheritance. The type of inheritance is specified by the access-specifier as
explained above.
We hardly use protected or private inheritance, but public inheritance is commonly used.
While using different type of inheritance, following rules are applied:
Public Inheritance: When deriving a class from a public base class, public members of
the base class become public members of the derived class and protected members of
the base class become protected members of the derived class. A base class's private
members are never accessible directly from a derived class, but can be accessed through
calls to the public and protected members of the base class.
Protected Inheritance: When deriving from a protected base class, public and
protected members of the base class become protected members of the derived class.
Private Inheritance: When deriving from a private base class, public and protected
members of the base class become private members of the derived class.
Multiple Inheritances:
A C++ class can inherit members from more than one class and here is the extended syntax:
class derived-class: access baseA, access baseB....
Where access is one of public, protected, or private and would be given for every base class
and they will be separated by comma as shown above. Let us try the following 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();

// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;

// Print the total cost of painting
cout << "Total paint cost: $" << Rect.getCost(area) << endl;

return 0;
}
When the above code is compiled and executed, it produces the following result:
Total area: 35
Total paint cost: $2450


};

int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = &rect;
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << ppoly1->area() << '\n';
cout << ppoly2->area() << '\n';
return 0;
}
20
10


In this example, objects of different but related types are referred to using a unique type of
pointer (Polygon*) and the proper member function is called every time, just because they are
virtual. This can be really useful in some circumstances. For example, it is even possible for a
member of the abstract base class Polygon to use the special pointer this to access the proper
virtual members, even though Polygon itself has no implementation for this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// pure virtual members can be called
// from the abstract base class
#include <iostream>
using namespace std;

class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area() =0;
void printarea()
{ cout << this->area() << '\n'; }
};

class Rectangle: public Polygon {
public:
20
10
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
int area (void)
{ return (width * height); }
};

class Triangle: public Polygon {
public:
int area (void)
{ return (width * height / 2); }
};

int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = &rect;
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly1->printarea();
ppoly2->printarea();
return 0;
}


Virtual members and abstract classes grant C++ polymorphic characteristics, most useful for
object-oriented projects. Of course, the examples above are very simple use cases, but these
features can be applied to arrays of objects or dynamically allocated objects.

Here is an example that combines some of the features in the latest chapters, such as dynamic
memory, constructor initializers and polymorphism:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// dynamic allocation and polymorphism
#include <iostream>
using namespace std;

class Polygon {
protected:
int width, height;
public:
Polygon (int a, int b) : width(a), height(b) {}
virtual int area (void) =0;
void printarea()
{ cout << this->area() << '\n'; }
};

class Rectangle: public Polygon {
public:
Rectangle(int a,int b) : Polygon(a,b) {}
int area()
{ return width*height; }
};

class Triangle: public Polygon {
public:
Triangle(int a,int b) : Polygon(a,b) {}
20
10
25
26
27
28
29
30
31
32
33
34
35
36
37
int area()
{ return width*height/2; }
};

int main () {
Polygon * ppoly1 = new Rectangle (4,5);
Polygon * ppoly2 = new Triangle (4,5);
ppoly1->printarea();
ppoly2->printarea();
delete ppoly1;
delete ppoly2;
return 0;
}


Notice that the ppoly pointers:
1
2
Polygon * ppoly1 = new Rectangle (4,5);
Polygon * ppoly2 = new Triangle (4,5);


are declared being of type "pointer to Polygon", but the objects allocated have been declared
having the derived class type directly (Rectangle and Triangle).

You might also like