Tuesday, September 6, 2011

Access specifiers in C++ programming


Here are the different access specifiers in C++ programming language.
pubic members

public members of any class can be accessed  from outside the class. For this reason function members are generally public as they should be accessed from outside the class. To make a member public, explicitly declare the member as public ( keyword). Normally we declare data members as private ( not in case of inheritance) and method members as public.

private members

private members can be accessed only from within a class where they are declared. A private member can neither be accessed by member function outside the class nor it can be inherited to a new class.

protected members

Protected members can not be accessed by member functions outside the class, but it allows to inherit the members to a new class

static members in C++

A data member of a class can be made static data member.

#include< iostream.h>
#include< conio.h>
#include< iomanip.h>

class student
{
static int roll;
private:
char name[30];
int m1,m2,m3,total;
float per;
public:
void accept();
void calculate();
void display();
};
int student::roll=1;
void student::accept()
{
cout<< "\nEnter the student record:";
roll++;
cout<< "\nEnter the name:";
cin >>name;
cout<< "\nEnter three marks:";
cin >>m1 >>m2 >>m3;
}
void student::calculate()
{
total=m1+m2+m3;
per=total/3.0;
}
void student::display()
{
cout<< "\nRecord of the student is:";
cout<< setw(4)<< roll<< setw(15)<< name<< setw(4)
<< m1<< setw(4)<< m2<< setw(4)<< m3<< setw(4)<< total<< setw(10.2)<< per;
}
void main()
{
clrscr();
student st1,st2;
cout<< "\nFor 1st object:";
st1.accept();
st1.calculate();
st1.display();
cout<< "\nFor 2nd object:";
st1.accept();
st1.calculate();
st1.display();

getch();
}


In case of static data member only one copy of the member is created for the entire class and is shared by all the objects of that class. A static data member is useful when all objects of a class must share a common item of information. A static data member is available only within the class. A static data member requires an unusual format. For normal variables they can be declared and defined in the same statement (compiler sets aside memory to hold the variable). Static data members requires two different statements. The variable declaration appears in the class while defined outside the class. This special approach for static data members is due to the reason that if they were defined inside a class, it would violate the idea that class declaration is only a blueprint and does not aside any memory. 

The declaration and definition of a static member is a must. If we simply declare a static member and forget to define it, compiler will show error. On the other hand while defining a static member, the class name is a must, otherwise linker will show error.

Like static data members there can be static function members also in C++ language.

#include< iostream.h>
#include< conio.h>
class sample
{

private:
static int count;
public:
sample()
{
count++;
}
static void show()
{
cout<< endl<< "COUNT="<< count;
}
};
int sample::count=0;
void main()
{
clrscr();
sample sp1;
sample::show();
sample sp2;
sample::show();
getch();
}

In general static functions accessed in class reference although they can be accessed using object of the class. Ordinary functions can not be accessed using class name.


1 comment:

  1. sir if you give about the specifiers more clearly we will be happy

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner