Tuesday, December 7, 2010

Access specifier

In Object Oriented Programming there are three types of access specifier is available. They are:

1.private
2.public
3.protected

syntax:
access_specifer:
member declarations;

private:
private members are private to class,which mean it allows the instance member to access within the class, access beyond that class scope makes error.
private data members are accessed through class public member functions.
In C++ private is default to class it means if we didn't specify any specifier its private to that class

syntax:
private:
member declaration;

Example:
private:
int reg_no;

public:
public members are public to a program/class, which means its like global variable of a program/class, we can access anywhere in the program/class. In java public is default to class it means if we didn't specify any specifier its public to that program/class.

syntax:
public:
member declaration;

Example:
public:
reg_no;

protected:
protected is same as private, we cant able to access the protected variable beyond a class scope. But, when we inherit the protected members of a class, we can able to access those protected member as private to that derived class.

syntax:
protected:
member declartion;

Example:
protected:
char address[40];

No comments:

Post a Comment