1) What Is Inheritance?why Inheritance Is Used in C++? Ans
1) What Is Inheritance?why Inheritance Is Used in C++? Ans
1) What Is Inheritance?why Inheritance Is Used in C++? Ans
Ans=
A derived class is defined by specifying it relationship with the base class in addition to its own
details.
The general form of defining a derived class is:
...
...
};
where,
class is the required keyword,
derived-class-name is the name given to the derived class,
base-class-name is the name given to the base class,
: (colon) indicates that the derived-class-name is derived from the base-class-name,
visibility-mode is optional and, if present, may be either private or public. The default
visibility-mode is private. Visibility mode specifies whether the features of the base class are
privately derived or publicly derived.
Examples:
members of ABC
};
members of ABC
};
members of ABC
};
When a base class is privately inherited by a derived class, ‘public members’ of the base
class becomes private members of the derived class and therefore the public members of
the base class can only be accessed by the member functions of the of the derived class.
They are inaccessible to the objects of the derived class.
Remember, a public member of the class can be accessed by its own objects by using the
dot operator. The result is that no member of the base class is accessible to the objects of
the derived class in case of private derivation.
On the other hand, when the base class is publicly inherited, ‘public members’ of the base
class becomes ‘public members’ of the derived class and therefore they are accessible to
the objects of the derived class.
In both the cases, the private members are not inherited and therefore, the private members of a
base class will never become the members of its derived class.
In inheritance, some of the base class data elements and member functions are inherited
into the derived clas. We can also add our own data and member functions and thus extend
the functionality of the base class. Inheritance, when used to modify and extend the
capabilities of the existing classes, becomes a very powerful tool for incremental program
development.
class vehicle
{
int fuel_cap;
public:
drive();
};
#include <iostream>
class base
private:
int x;
protected:
int y;
public:
int z;
x = 1;
y = 2;
z = 3;
};
//y and z becomes private members of class derive and x remains private
public:
void showdata()
};
int main()
a.showdata();
return 0;
} //end of program
Output
x is not accessible
value of y is 2
value of z is 3
#include <iostream>
private:
int x;
protected:
int y;
public:
int z;
x = 1;
y = 2;
z = 3;
};
public:
void showdata()
};
int main()
a.showdata();
return 0;
} //end of program
Output
x is not accessible
value of y is 2
value of z is 3
#include <iostream>
class base
private:
int x;
protected:
int y;
public:
int z;
x = 1;
y = 2;
z = 3;
};
public:
void showdata()
};
int main()
return 0;
} //end of program
Output
x is not accessible
value of y is
one class. i.e. one sub class is inherited by one base class only.
2. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class
can inherit from more than one classes. i.e one sub class is inherited from
// C++ program
inheritance:
Multipath inheritance:
A derived class with two base classes and these two base classes have one common
base class is called multipath inheritance. An ambiguity can arrise in this type of
inheritance.
Hierarchical Inheritance: In this type of inheritance, more than one sub class is
inherited from a single base class. i.e. more than one derived class is created from a
Protected will acts as public within the same package and acts as private outside the
package.
Protected will also act as public outside the package only with respect to subclass
objects.
Protected fields or methods cannot be used for classes and Interfaces.
The Fields, methods, and constructors declared as protected in a superclass can be
accessed only by subclasses in other packages.
The classes in the same package can also access protected fields, methods and
constructors as well, even if they are not a subclass of the protected member’s class.
#include <iostream>
using namespace std;
class Animal
{
public:
void eat()
{
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark()
{
cout<<"Barking...";
}
Dogark();
return 0;
Output:
Eating...
Barking...
C
↑
|
---------------
↑ ↑
| |
A B
↑
|
D
In C++ hierarchical inheritance, the feature of the base class is inherited onto more than
one sub-class.
For example, a car is a common class from which Audi, Ferrari, Maruti etc can be derived.
..............
};
...........
} ;
...........
} ;
class D : access_specifier A // derived class from A
...........
} ;