Access Specifiers
Access Specifiers
Access-control Specifiers
• Each user has different access privileges to the
object. A class differentiates between access
privileges
• by partitioning its contents and associating each
one of them with any one of the following
keywords:
• private
• public
• protected
Access-control Specifiers
Private Members
• The private members of a class have strict
access control.
• Only the member functions of the same class
can access these members.
• The private members of a class are
inaccessible outside the class,
Private Members
class Inaccessible
{
int x;
void Display(}
{
cout << “\nData = “ << x;
}
};
void main()
{
Inaccessible objl; // Creating an object.
objl.x = 5; // Error: Invalid access.
objl.Display(); // Error: Invalid access.
}
Protected Members
• The access control of the protected members is
similar to that of private members and has
more significance in inheritance.
Public Members
• The members of a class, which are to be visible
(accessible) outside the class, should be declared
in public section.
• All data members and functions declared in the
public section of the class can be accessed
without any restriction from anywhere in the
program, either by functions that belong to the
class or by those external to the class.
Public Members
ACCESS BOUNDARY OF OBJECTS
EMPTY CLASSES
• Although the main reason for using a class is to
encapsulate data and code, it is however, possible to
have a class that has neither data nor code.
• In other words, it is possible to have empty classes. The
declaration of empty classes is as follows:
class xyz { };
class Empty { };
class abc
{
};
EMPTY CLASSES
• During the initial stages of development of a
project, some of the classes are either not
fully identified, or not fully implemented.
• In such cases, they are implemented as empty
classes during the first few implementations of
the project.
• Such empty classes are also called stubs.
PASSING OBJECTS AS ARGUMENTS
#include <iostream>
using namespace std; int main()
class Demo {
{ //object declarations
private: Demo d1;
int a; Demo d2;
public: Demo d3;
void set(int x) //assigning values to the data member of
{ objects
a = x; d1.set(10);
} d2.set(20);
void sum(Demo ob1, Demo ob2) //passing object d1 and d2
{ d3.sum(d1,d2);
a = ob1.a + ob2.a; //printing the values
} d1.print();
void print() d2.print();
{ d3.print();
cout<<"Value of A : "<<a<<endl;
} return 0;
}; }
Passing Objects by Reference
• Accessibility of the objects passed by reference
is similar to those passed by value.
• Modifications carried out on such objects in the
called function will also be reflected in the
calling function.
Passing Objects by Pointer
• The members of objects passed by pointer are
accessed by using the -> operator, and they
have similar effect as those passed by value.
RETURNING OBJECTS FROM FUNCTIONS
class student
{
private:
int roll_no; // roll number
char name[ 20 ]; // name of a student
}
void counter()
void counter() {
{
static int count=0;
int count=0;
cout << count++;
cout << count++;
}
}