Tuesday, October 4, 2016

Muhammad Akbar

Access specifiers in C++ Tutorial

C++ offers possibility to control access to class members and functions by using access specifiers. Access specifiers are used to protect data from misuse.
In the Person class from the previous topic we used only public access specifiers for all data members:

Types of access specifiers in C++

  1. public

  2. private

  3. protected

Public Specifier

Public class members and functions can be used from outside of a class by any function or other classes. You can access public data members or function directly by using dot operator (.) or (arrow operator-> with pointers).

Protected Specifier

Protected class members and functions can be used inside its class. Protected members and functions cannot be accessed from other classes directly. Additionally protected access specifier allows friend functions and classes to access these data members and functions. Protected data members and functions can be used by the class derived from this class. More information about access modifiers and inheritance can be found in C++ Inheritance

Private Specifier

Private class members and functions can be used only inside of class and by friend functions and classes.
We can modify Person class by adding data members and function with different access specifiers:
class Person
{
public://access control
	string firstName;//these data members
	string lastName;//can be accessed
	tm dateOfBirth;//from anywhere
protected:
	string phoneNumber;//these members can be accessed inside this class,
	int salary;// by friend functions/classes and derived classes
private:
	string addres;//these members can be accessed inside the class
	long int insuranceNumber;//and by friend classes/functions
};
Access specifier affects all the members and functions until the next access specifier:
For classes, default access specifier is private. The default access specifier for unions and structs is public.

    

  Accessors (getters) and Mutators (setters)

Muhammad Akbar

About Muhammad Akbar -

Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

Subscribe to this Blog via Email :

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you