Friday, May 31, 2019

Access Modifiers

Access Modifiers

Public, Private, Protected are the access modifiers.
1. Public: Can access any place.
2. Private: Only in Same.
3. Protected:  In same class + Child Class.

Uses of access modifiers are as follows:
#include<iostream.h>
#include<conio.h>
//base class
class student
{
          protected:
                   int x;
          public:
                   void display()
                   {
                             cout<<” base class”;
                   }
};
class collegemember: public student
{
          public :
                   void display1()
                   {
                             x=9848;
                   }
                   cout<<x;
};
void main()
{
          clrscr();
          collegemember   y;
          y.display();
          y.display1();
          getch();


}


Inheritance and Constructor :
Constructor caliing : top to bottom.
 At first will call base class constructor, then will call child class constructor.
Inheritance and method :
Method calling : bottom to top.
E.g.:
#include<iostream.h>
#include<conio.h>
class a
{
          public :
                   a()
                   {
                             cout<<”base class constructor”;
                   }
                   void display()
                   {
                                      cout<<”hello base class”;
                   }
};
class b : public a
{
public:
          b()
          {
                   cout<<”child class”;
          }
          void display()
          {
                   cout<<”hello child class”;
          }
};
void main()
{
          clrscr();
          b   z;
          z.display();
          getch();
}

No comments:

Post a Comment