Inheritance

What is inheritance? why we use it? plzz can anybody explain me with one example and where we use that concept?

Questions by chaitugoud

Showing Answers 1 - 18 of 18 Answers

It's a concept of deriving all the functionality of a class/structure into other class/structure (generally both are user defined). Inheritance will omit the the requirement of declaring a base object inside a derived object.

struct base{
// members of base
};

Now, if you are using C then we can have the derived structure functionality implemented as below:

struct derived{
base obj;
// members of derived
};

but in C++ it is like following:

struct derived : <inheritance type> base{
// members of derived
};
where inheritance type = public, protected, private


  Was this answer useful?  Yes

Inheritance is the capability of one class(derived class) to inherit properties i.e data members & functions of the base class.It supports reusability of code & is able to simulate the transitive nature of real life objects..It has many forms':Single inheritance,
multiple inheritance,
hierarchial
& multilevel inheritance

using inheritance we can derive some codes from one class to another if it is not private.
inheritance is used to establish a relation between one class and another class(it can be done with two non related classes also but we should not do that it would be a bad programming).
with the help of inheritance we can avoid typing some codes repeatedly.

for example
class base
{
public void display()          //if display() is private we will get an error
{
cout<<"inherited";
}
};
class der : public base
{
};
void main()
{
der o=new der();
o.display();                //though display() is not in der we had inherited it from base so
}                                //it would print inherted

  Was this answer useful?  Yes

The main use of inheritance is to provide reusability of codes .Through inheritance base class objects and functions are acquired by derived class so there is no need to specify it again in derived class

  Was this answer useful?  Yes

It is an object oriented feature which provides the reusablity of code. All the properties defined in the parent class can be used by its child classes without writing the code again.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions