The "inherit" keyword signals that the derived class inherits from the base class. Discuss your thoughts on inheritance and how you would use it in a program. What advantages does using inheritance...

1 answer below »

The "inherit" keyword signals that the derived class inherits from the base class. Discuss your thoughts on inheritance and how you would use it in a program. What advantages does using inheritance give you?

Answered 1 days AfterMar 17, 2021

Answer To: The "inherit" keyword signals that the derived class inherits from the base class. Discuss your...

Shashi Kant answered on Mar 17 2021
143 Votes
Inheritance->
===========
     As the name suggest inherit we can understand that an object will acquires all the functions/operations/properties of its parent object.It allows us to create a new class from an existing class.
    
     ex:-
        //A man 'X' has one child 'Y' so here X will be(Pa
rent) and the Y will be (child of the parent)
        //Y will acqire all the properties of X (like:- eat , sleep, talk etc.)
*In Technical Language:-
With the help of example we can understand that (Class X) is the "Parent class" which is also known as "Base class or Super class" cause it is being inherited and (Class Y) is the "Child class" cause it inherits from anothe class, and this class is also known as "Derived class or Sub class".
Definition
==========>
Base Class->
-----------
     A base class is a class from which other classes are derived.If another class is inheriting this class(base class) then that class(derived class) will have all members of a base class and the derived class can also have some additional properties.A base class is also known as (Parent class or Super class).
    
     Syntax-> Class base_classname
{
            //body of the base_class
        };
Derived Class->
--------------
     A derived class is a class that is created from an existing class.Derived class inherits the features from the Base class and it can have additional properties of its own.A derived class is also known as (Child class or Sub class).
    
     Syntax-> Class derived_classname : access_mode base_classname
            {
                //body of the derived_class
            };
Modes of Inheritance:-
--------------------
1. Public mode:-
-----------
In this mode the member is declared as public and it is accessible to all the functions of the program.
2. Protected mode:-
------------
In this mode the member is declared as private, it is accessible within the class only.
3. Private mode:-
------------
In this mode the member is declared as protected, it is accessible within its own class as well as the class immediately derived from it.
Example in c++ code :-
--------------
#include
using namespace std;
// base class
class Parent {
public:
void eat() {
cout << "I can eat" << endl;
}
void sleep() {
cout << "I can sleep" << endl;
}
};
// derived class
class Child : public Parent {

public:
void talk() {
cout << "I can talk" << endl;
}
};
int main() {
// Creating object of the Child class
Child child1;
// Calling members of the base class
child1.eat();
child1.sleep();
// Calling member of the derived class
child1.talk();
return 0;
}
*Here in the code we can see that derived class inherits base class and derived class has also its own properties, and...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here