2. The OO principle of inheritance enables you
to create a generalized class and then derive
more specialized classes from it.
Inheritance is the ability to take on the
characteristics of the class or derived class on
which it is based.
Specifies an “is-a” kind of relationship
3. Person
Employee Student
Full-time Part-time
Employee Employee
5. New classes that we create from the existing
class are called derived classes; the existing
classes are called base classes.
6. class className:memberAccessSpecifier baseClassName
{
memberList;
};
Where:
memberAccessSpecifier – is public, private, or protected. When no
memberAccessSpecifier is specified, it is assumed to be a private
inheritance.
9. 1. The private members of a base class are
private to the base class; hence the
members of the derived class cannot
directly access them. In other words, when
you write the definitions of the member
functions of the derived class, you cannot
directly access the private members of the
base class.
10. 2. The public members of a base class can be
inherited either as public members or as
private members by the derived class. That
is, the public members of the base class can
become either public or private members of
the derived class.
11. 3. The derived class can include additional
members – data and/or functions.
4. The derived class can redefine the public
member functions of the base class. That is,
in the derived class, you can have a member
function with the same name, number and
types of parameters as function in the base
class. However, this redefinition applies
only to the object of the derived class, not to
the objects of the base class.
12. 5. All member variables of the base class are
also member variables of the derived class.
Similarly, the member functions of the base
class(unless redefined) are also member
functions of the derived class. (Remember
Rule 1 when accessing a member of the base
class in the derived class.
13. class Derived:Base class Base
{ {
int y; int x;
public : public :
void print() const; void print()const;
}; };
void Derived::print()const void Base::print()const
{ {
cout<<y<<endl; cout<<x<<endl;
} }
14. To redefine a public member function of a
base class in the derived class, the
corresponding function in the derived class
must have the same name, number, and
types of parameters.
18. Define a class named BoxType
BoxType contains data members that stores the length,
width and height of a box.
It has the following member functions :
Function that sets the dimension of the box
Function that sets a value for each data member of the class
Function that returns the value of each data member of the
class
Function that prints the values of the data members of the class
Function that computes and returns the area of the box
Function that computes and returns the volume of the box
Default constructor which initializes data members to 0
Parameterized constructor which initializes data member to a
value set by the object of the class
19. In general, while writing the definitions of the
member functions of a derived class to
specify a call to a public member function of
the base class we do the following:
If the derived class overrides a public member
function of the base class, then to specify a call to
that public member function of the base class use
the name of the base class followed by the scope
resolution operator, ::, followed by the function
name with the appropriate parameter list.
20. If the derived class does not override a public
member function of the base class, you may
specify a call to that public member function by
using the name of the function and the
appropriate parameter list.
21. Recall:
private members of a class are private to the class and
cannot be directly accessed outside the class. Only
member functions of that class can access the private
members.
If public, anyone can access that member
So for a base class to give access to a member to its
derived class and still prevent its direct access outside the
class, you must declare the member under the
memberAccessSpecifier protected.
▪ The accessibility of a protected class is between public and private
▪ A derived class can directly access the protected members of the base
class.
22. Example:
class B : memberAccessSpecifier A
{
:
:
};
memberAccessSpecifier is either private, public
or protected
23. If memberAccessSpecifier is public – that is
inheritance is public - then:
The public members of A are public members of
B. They can be directly accessed in class B.
The protected members of A re protected
members of B. They can be directly accessed by
the member functions of B.
The private members of A are hidden in B. They
can be accessed by the member functions of B
through the public and protected members of A.
24. If memberAccessSpecifier is protected– that is
inheritance is protected - then:
The public members of A are protected members of B.
They can be accessed by the member functions of B.
The protected members of A are protected members
of B. They can be accessed by the member functions
of B.
The private members of A are hidden in B. They can
be accessed by the member functions of B through
the private or protected members of A.
25. If memberAccessSpecifier is private– that is
inheritance is private - then:
The public members of A are private members of B.
They can be accessed by the member functions of B.
The protected members of A are private members of
B. They can be accessed by the member functions of
B.
The private members of A are hidden in B. They can
be accessed by the member functions of B through
the private or protected members of A.