Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

1) What Is Inheritance?why Inheritance Is Used in C++? Ans

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

1) What is inheritance?why inheritance is used in c++?

Ans=

2) state general form of defining derived class.


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:

class derived-class-name : visibility-mode base-class-name

...

... //members of derived class

...

};

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:

class ABC : private XYZ //private derivation

members of ABC

};

class ABC : public XYZ //public derivation

members of ABC

};

class ABC : XYZ //private derivation by default

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.

3) Define derived class.give one example.


Ans =
Inheritance is one of the important feature of OOP which allows us to make hierarchical
classifications of classes. In this, we can create a general class which defines the most common
features. Other more specific classes can inherit this class to define those features that are
unique to them. In this case, the classes which inherit from other classes, is referred as derived
class.
- For example, a general class vehicle can be inherited by more specific classes car and bike.
The classes car and bike are derived classes in this case.

class vehicle
{
   int fuel_cap;
   public:
       drive();
};

class car : public class vehicle


{
   public:
       roll_windows();
};
class bike : public class vehicle
{
   public:
       kick_start();
};
4) state any two access specifier with example.
Ans =
Access specifier can be either private or protected or public. In general access specifiers
are the access restriction imposed during the derivation of different subclasses from the
base class.

 private access specifier


 protected access specifier
 public access specifier

1 . private access specifier


 private access specifier is used while creating a class, then the public and protected data
members of the base class become the private member of the derived class and private
member of base class remains private.
// private access specifier.cpp

#include <iostream>

using namespace std;

class base

private:

int x;

protected:
int y;

public:

int z;

base() //constructor to initialize data members

x = 1;

y = 2;

z = 3;

};

class derive: private base

//y and z becomes private members of class derive and x remains private

public:

void showdata()

cout << "x is not accessible" << endl;

cout << "value of y is " << y << endl;


cout << "value of z is " << z << endl;

};

int main()

derive a; //object of derived class

a.showdata();

//a.x = 1; not valid : private member can't be accessed outside of class

//a.y = 2; not valid : y is now private member of derived class

//a.z = 3; not valid : z is also now a private member of derived class

return 0;

} //end of program

Output

x is not accessible

value of y is 2

value of z is 3

2. Protected Access Specifier


 protected access specifier is used while deriving class then the public and protected data
members of the base class becomes the protected member of the derived class and private
member of the base class are inaccessible.
// protected access specifier.cpp

#include <iostream>

using namespace std;


class base

private:

int x;

protected:

int y;

public:

int z;

base() //constructor to initialize data members

x = 1;

y = 2;

z = 3;

};

class derive: protected base


{

//y and z becomes protected members of class derive

public:

void showdata()

cout << "x is not accessible" << endl;

cout << "value of y is " << y << endl;

cout << "value of z is " << z << endl;

};

int main()

derive a; //object of derived class

a.showdata();

//a.x = 1; not valid : private member can't be accessed outside of class

//a.y = 2; not valid : y is now private member of derived class

//a.z = 3; not valid : z is also now a private member of derived class

return 0;

} //end of program

Output

x is not accessible
value of y is 2

value of z is 3

3. public access specifier


public access specifier is used while deriving class then the public data members of the
base class becomes the public member of the derived class and protected members
becomes the protected in the derived class but the private members of the base class are
inaccessible.
// public access specifier.cpp

#include <iostream>

using namespace std;

class base

private:

int x;

protected:

int y;

public:

int z;

base() //constructor to initialize data members


{

x = 1;

y = 2;

z = 3;

};

class derive: public base

//y becomes protected and z becomes public members of class derive

public:

void showdata()

cout << "x is not accessible" << endl;

cout << "value of y is " << y << endl;

cout << "value of z is " << z << endl;

};

int main()

derive a; //object of derived class


a.showdata();

//a.x = 1; not valid : private member can't be accessed outside of class

//a.y = 2; not valid : y is now private member of derived class

//a.z = 3; not valid : z is also now a private member of derived class

return 0;

} //end of program

Output

x is not accessible

value of y is

5) explain various types of inheritance with example.


Ans =
Types of Inheritance in C++

1. Single Inheritance: In single inheritance, a class is allowed to inherit from only

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

more than one base classes.

3. Multilevel Inheritance: In this type of inheritance, a derived class is created

from another derived class.

// C++ program

// C++ program to implement

Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by


combining more than one type of inheritance. For example: Combining
Hierarchical inheritance and Multiple Inheritance.
Below image shows the combination of hierarchical and multiple

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

single base class.

6) how protected access specifier is different from private.


Ans=
Protected Access Specifier

 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.

7) write a program showing use of single inheritance.


Ans =

#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...

8) Explain multiple inheritance.


Ans=
Multiple inheritance is a feature of some object-oriented computer programming
languages in which an object or class can inherit characteristics and features from more
than one parent object or parent class. It is distinct from single inheritance, where an
object or class may only inherit from one particular object or class.
Multiple inheritance has been a controversial issue for many years, [1][2] with opponents
pointing to its increased complexity and ambiguity in situations such as the "diamond
problem", where it may be ambiguous as to which parent class a particular feature is
inherited from if more than one parent class implements said feature. This can be
addressed in various ways, including using virtual inheritance.[3] Alternate methods of
object composition not based on inheritance such as mixins and traits have also been
proposed to address the ambiguity.

9) what is hybrid inheritance? Give one example.


Ans=
A hybrid inheritance is a combination of more than one types of inheritance. For
example when class A and B extends class C & another class D extends class A
then this is a hybrid inheritance, because it is a combination of single and
hierarchical inheritance.

C

|
---------------
↑ ↑
| |
A B

|
D

10) illustrate the hierarchical inheritance.


Ans=
When several classes are derived from common base class it is called hierarchical
inheritance.

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.

Following block diagram highlights its concept


C++ Hierarchical Inheritance Syntax
class A // base class

..............

};

class B : access_specifier A // derived class from A

...........

} ;

class C : access_specifier A // derived class from A

...........

} ;
class D : access_specifier A // derived class from A

...........

} ;

You might also like