0% found this document useful (0 votes)
22 views25 pages

Access Specifiers

Access specifiers determine the accessibility of class members. Private members can only be accessed by member functions of the same class. Protected members have access similar to private but with more significance in inheritance. Public members can be accessed from anywhere in the program. Static members are shared across all class objects and are initialized to zero when the first object is created if no other initialization is present. Static member functions can only access static members declared in the same class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views25 pages

Access Specifiers

Access specifiers determine the accessibility of class members. Private members can only be accessed by member functions of the same class. Protected members have access similar to private but with more significance in inheritance. Public members can be accessed from anywhere in the program. Static members are shared across all class objects and are initialized to zero when the first object is created if no other initialization is present. Static member functions can only access static members declared in the same class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

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

• Similar to sending objects as parameters to


functions, it is also possible to return objects
from functions.
• The syntax used is similar to that of returning
variables from functions.
• The return type of the function is declared as
the return object type.
ARRAY OF OBJECTS
• an array of variables of a class data type can
also be defined, and such variables are called
an array of objects.
• An array of objects is often used to handle a
group of objects, which reside contiguously in
the memory
ARRAY OF OBJECTS

class student
{
private:
int roll_no; // roll number
char name[ 20 ]; // name of a student
}

The following definition creates an array of objects of the student class:


STATIC DATA AND MEMBER FUNCTIONS

• A static member is shared by all objects of the


class.
• All static data is initialized to zero when the
first object is created, if no other initialization
is present. 
Static Variables inside Functions

void counter()
void counter() {
{
static int count=0;
int count=0;
cout << count++;
cout << count++;
}
}

int main() int main(0


{ {
for(int i=0;i<5;i++) for(int i=0;i<5;i++)
{ {
counter(); counter();
} }
} }
00000 01234
Static Data Member in Class

• Static data members of class are those


members which are shared by all the objects.
• Static data member has a single piece of
storage, and is not available as separate copy
with each object, like other non-static data
members.
Static Data Member in Class
• The static data members can be initialized
during their definition outside all the member
functions, in the same way as global variables
are initialized
Static Data Member in Class
The static data members which are declared public are
similar to normal global variables.
class Test Test::private_int = 12; //
{ wrong, do not touch the
public: private data members
static int public_int; Test myobj;
private: myobj.public_int = 145; //ok
static int private_int; myobj.priyate_int = 12; //
}; wrong, do not access the
void main() private data member
{ }
Test::public_int = 145; // ok D:\c++programs\static1.cpp
#include <iostream> };
using namespace std; int MyClass::count = 0;
class MyClass int main()
{ {
static int count; // static member MyClass obj1;
int number; obj1.show();
public: obj1.set( 100 );
// initializes object’s member and obj1.show();
increments function call MyClass obj2, obj3;
void set(int num) obj2.set( 200 );
{ obj2.show(); //same result even with
number = num; objl.show and obj3.show();
++count; obj2.set( 250 );
} obj3.set( 300 );
void show() obj1.show(); //same result even with
{ obj2.show and obj3.show();
cout << "\nNumber of calls made to 'set()' return 0;
through any object: " }
<< count;
}
Static Member Functions
• These static functions can access only the
static members(data or function) declared in
the same class;
• non static data are unavailable to these
functions.
• Static member functions declared in the public
part of a class declaration can be accessed
without specifying an object of the classD:\c++
programs\staticmemf.cpp
#include <iostream> //static function II
using namespace std; static void set_name(string name){
class Nokia{ phone_name = name;
private: }
/* };
*declaring variable as static, so //Initializing private static member
that- string Nokia::phone_name = "";
*it will be accessible to the static int main()
functions {
*/ //no object has been created
static string phone_name; //accessing static function directly
public: with class name
//static function I Nokia::set_name("Nokia 2600");
static void name(){ Nokia::name();
cout << "Phone: "<< return 0;
phone_name; }
}

You might also like