Inheritance in C++
The
capability of a class to derive properties and characteristics from another
class is called Inheritance.
Inheritance is one of the most important feature of Object Oriented
Programming.
Features Of Inheritance
• Dimension values, tolerances,
and boundaries
• Geometry tolerances (part of
Annotations)
• Parameters
References
Suppressed, resumed or erased state of features
Annotations
Fig:Inheritance in C++ |
Super Class:
The
class whose properties are inherited by sub class is called Base Class or Super
class.
Syntax:
class baseclassname
{
private:
datamember;
public:
datamember;
member function;
};
Sub Class:
The class that inherits
properties from another class is called Sub class or Derived Class.
Syntax:
class
derivedclassname :visibilitymode
baseclassname
{
private:
datamember;
public:
datamember;
member function;
};
Program:
#include<iostream.h>
#include<conio.h>
class
base
{
public:
void
display()
{
cout<<”base
class created”<<endl;
}
};
class
derived: public base
{
public:
void
display1()
{
cout<<”derived
class created”<<endl;
}
};
void
main()
{
Derived
a1;
a1.display();
a1.display1();
getch();
}
Output:
Base
class created
Derived
class created
Implementing
inheritance in C++:
For
creating a sub-class which is inherited from the base class.
Syntax:
class
subclass_name:access_mode base_class_name
{
//body
of subclass
}
Modes of Inheritance
1.
Public
mode:
If
we derive a sub class from a public base class. Then the public member of the
base class will become public in the derived class and protected members of the
base class will become protected in derived class.
2.
Protected
mode:
If we derive a sub class from a Protected base
class. Then both public member and protected members of the base class will
become protected in derived class.
3. Private mode:
If we derive a sub
class from a Private base class. Then both public member and protected members
of the base class will become Private in derived class.
No comments:
Post a Comment