inheritance
TRANSCRIPT
The OO principle of inheritance enables youto create a generalized class and then derivemore specialized classes from it.
Inheritance is the ability to take on thecharacteristics of the class or derived class onwhich it is based.
Specifies an “is-a” kind of relationship
Person
Employee
Full-time Employee
Part-time Employee
Student
Shape
Rectangle
Square
Circle
New classes that we create from the existing class are called derived classes; the existing classes are called base classes.
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.
class Circle : public Shape{
.
.
.};
class Circle : private Shape{
.
.
.};
1. The private members of a base class areprivate to the base class; hence themembers of the derived class cannotdirectly access them. In other words, whenyou write the definitions of the memberfunctions of the derived class, you cannotdirectly access the private members of thebase class.
2. The public members of a base class can beinherited either as public members or asprivate members by the derived class. Thatis, the public members of the base class canbecome either public or private members ofthe derived class.
3. The derived class can include additionalmembers – data and/or functions.
4. The derived class can redefine the publicmember functions of the base class. That is,in the derived class, you can have a memberfunction with the same name, number andtypes of parameters as function in the baseclass. However, this redefinition appliesonly to the object of the derived class, not tothe objects of the base class.
5. All member variables of the base class arealso member variables of the derived class.Similarly, the member functions of the baseclass(unless redefined) are also memberfunctions of the derived class. (RememberRule 1 when accessing a member of the baseclass in the derived class.
class Derived:Base{
int y;public :
void print() const;};void Derived::print()const{
cout<<y<<endl;}
class Base{
int x;public :void print()const;
};void Base::print()const{
cout<<x<<endl;}
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.
class RectangleType{
public:void setDimension(double, double);double getLength()const;double getWidth() const;double area()const;double perimeter()const;void print()const;RectangleType();RectangleType(double, double);
private:double length;double width;
};
#include "RectangleType.h"#include<iostream>using namespace std;
void RectangleType::setDimension(double l, double w)
{if (l>=0)
length = l;else
length =0;
if (w>=0)width = w;
elsewidth = 0;
}
double RectangleType::getLength()const
{return length;
}double RectangleType::getWidth
()const{
return width;}
double RectangleType::area()const{
return length * width;}double RectangleType::perimeter
()const{
return 2*(length + width);}void RectangleType::print() const {
cout<<"Length = "<<length<<"Width = " <<width;
}
RectangleType::RectangleType(double l, double w)
{setDimension(l,w);
}RectangleType::RectangleType(){
length =0;width =0;
}
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
In general, while writing the definitions of themember functions of a derived class tospecify a call to a public member function ofthe base class we do the following: If the derived class overrides a public member
function of the base class, then to specify a call tothat public member function of the base class usethe name of the base class followed by the scoperesolution operator, ::, followed by the functionname with the appropriate parameter list.
If the derived class does not override a publicmember function of the base class, you mayspecify a call to that public member function byusing the name of the function and theappropriate parameter list.
Recall: private members of a class are private to the class and
cannot be directly accessed outside the class. Onlymember functions of that class can access the privatemembers.
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 theclass, you must declare the member under thememberAccessSpecifier 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.
Example:class B : memberAccessSpecifier A{
::
}; memberAccessSpecifier is either private, public
or protected
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.
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.
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.