in

CPP – Access Labels

Okay as I previously introduced you the 3 access labels here is a detailed post on these buddies. Believe me these 3 things will be with us forever if you wanted to be in Computer Science field XD.

Access specifiers in C++ class defines the access control rules. C++ has 3 new keywords introduced, namely,

  1. Public

  2. Private

  3. Protected

These access specifiers are used to set boundaries for availability of members of class be it data members or member functions

Access specifiers in the program, are followed by a colon. You can use either one, two or all 3 specifiers in the same class to set different boundaries for different class members. They change the boundary for all the declarations that follow them

Public

Public, means all the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too. Hence there are chances that they might change them. So the key members must not be declared public.

class Rectangle{ //Class Definiton
public://Access Label
  int length; 
  int breadth;
};

Private

Data members and member functions declared under private section will not be available to everyone. The data members and member function declare private can’t be accessed by other classes. Only the class in which these members are declared can be accessed. Basically private section contains those members which a programmer want’s to hide from the world.

class Rectangle{ //Class Definiton
private://Access Label
  int length; 
  int breadth;
};

Protected

Data members and member functions declared under protected section will only available to the derived class (Class which inherits the properties of another class; that’s why it is called derived class) and the class in which these members are declared.

class Shape{ //Class Definiton
protected://Access Label
  int length = 11; 
  int breadth = 9;
};

class Rectangle: private Shape{ 
//all members declared in protected section will now serve as private member

//private:  
// int length = 11;
// int breadth = 9; 

public://Access Label
  void ShowArea(){
   cout << length * breadth << endl;
  }
};

Consider the above example we have two classes Shape and Rectangle, here Rectangle is a derived class which means Rectangle class contains the properties of Shape Class i.e. all the members declared in public and protected sections are accessible to this class, that is called inheritance and we will dive deeper into inheritance later.

How Execution happened here?

So, in order to run this program we need to first create an object of Rectangle class because now this class contains properties of Shape class.

int main(){
 Rectangle Rect1;
 Rect1.ShowArea();
 return 0;
}

We created an object called Rect1 and using that object we are accessing the member function of Rectangle class. So output will be:

99

This is the example of inheritance which we learn more about it later and it is major concept of CPP.

Written by Anandesh

I am a #Linux lover and very much excited about technology and new thing. Love to #read_books. Curiosity is my best teacher. I am still☺️ understanding myself.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CPP – Classes & Objects

How to get Pluralsight 3 month subscription for free – 2018