Thursday, August 26, 2021

inheritence in c++

inheritance:

it is a mechanism in which one class acquire the properties of another class allow us to create new class derived class from existing class parent class derived class inherits the function from base class and have additional functions to.

we use access specifiers to provide data security

access specifier:

        i.            public : using this public keyword we allow anyone to access data anywhere he wants

      ii.            protected: only child classes access data or class it self

    iii.            private :no one can access data only class access the data

 

Public

Protected

Private

Within class

Yes

Yes

Yes

Child class

Yes

Yes

No

Main

Yes

No

No


code:

#include<iostream>
using namespace std;
class animal
{
    public:
    int legs=4;
    int tail=1;
};
class dogpublic animal
{
    public:
    void disp()
    {
    cout<<"i provide security in day because i sleep in day"<<endl;
}
};
int main()
{
    dog d1;
    cout<<"legs are ="<<d1.legs<<endl;
    cout<<"tail ="<<d1.tail<<endl;
    d1.disp();
}

No comments:

Post a Comment

How to apply for Canada visa?

  Applying for a Canada Visa: A Comprehensive Guide Explore the Beauty of Canada - Your Step-by-Step Visa Application Guide Introduction Can...