Friday 30 September 2011

ACCESS SPECIFIERS



ACCESS SPECIFIERS:

The access specifier provides access rights for the class members. Access specifiers available in C++ are: private, public and protected. They are used in C++ to implement a concept called data hiding. It means it cannot be accessed by functions outside the class even by mistake.

1.      PRIVATE:
The mechanism used to hide data is to put it in a class and make it private. Private data and functions can be accessed from within the class only. We usually declare data in this section. A class guarantees maximum protection to private members.
2.      PUBLIC:
A class does not provide any security to the public members. They can be accessed from both inside and outside the class. We usually declare functions in this section.
3.      PROTECTED:
The access specifier protected is same as that of private. But the difference is a private member can’t be inherited where as protected member can be inherited by the derived classes. Derived classes will be having rights over the base classes protected members.

Syntax:  

   class class-name
    {
          private/public/protected:
       data_members;
       member_functions;

     };

IMPORTANT FEATURES OF ACCESS SPECIFIERS:

1.      By default, all the members of a class are private and they cannot be accessible outside the class.
2.      Private members accessible only with in the class.
3.      Private members can be accessed through public members.
4.      The member variables and functions that are declared as public can be accessed by the members outside the class.
5.      Protected members can be inherited by the derived classes.

Example:

 #include<iostream.h>
 class  shape
 {
private:
 int a, b;
 void set_val()
{
     a=10;
     b=20;
}
 void display()
{
      cout<<“a=“<<a<<“\nb=“<<b;
}
 public:
 void sum()
{
      set_val();
      display();
       cout<<“\nsum=“<<a+b;
}

void sub(void);
};

void shape::sub()
{
  a=30;
  b=10;
  cout<,”\nsubtraction=“<<a-b;
}

void main()
{
   shape s;
   s.sum(); // sum() is a public member. It accesses set_val() and display() which are private
                    members.
   s.sub();
}

Output:

a=10
b=20
sum =30
Subtraction=20



Base class access control
1.      The derived class will have the members of the base class based on the declaration type such as private, public or protected and the access specifier at the time of inheriting the base class.
2.      The members of the base class will be protected from the derived class based on the access specifier. All the private members of base class will not be inherited by the derived class irrespective of the access specifier.
In case the derived class is a class, the following cases will be based on the access specifer:
Case 1: Access specifier is public:
a.       All the public members of base class will become the public members of the derived class.
b.      All the protected members of the base class will become the protected members of derived class.
c.       All the private members of the base class will not be inherited to the derived class.
Case 2: Access specifier is protected:
a.       All the public members of the base class will become protected members of the derived class.
b.      All the protected members of the base class will become the protected members of the derived class.
c.       All the private members of the base class will not be inherited to the derived class.
Case 3: Access specifier is private:
a.       All the public members of the base class will become private members of the derived class.
b.      All the protected members of the base class will become the private members of the derived class.
c.       All the private members of the base class will not be inherited to the derived class.

Visibility of inherited members
Base class visibility
Derived class visibility
Public derivation
Private derivation
Protected derivation
Private
Not inherited
Not inherited
Not inherited
Protected
Protected
Private
Protected
Public
Public
Private
Protected

 

Example for access specifier is of the base class is public

object and its features



OBJECT:

An object is an instance of the class. It is an entity that has an existence.

The basic idea behind object oriented language is to combine into a single unit both data and  functions that operate on the data. Such a unit is called an “object”.

For example, a fiat car with registration number AP 09D 3233 is a particular instance of the class cars. It has a unique identity. A car with a different registration number is a different object of the same class cars.

An object fundamentally has three characteristics:

1.      A State
2.      A Behavior
3.      An identity

For example, car is an object of the class cars. Where each car can have the following features:

State: Color, Size, Weight, Engine Capacity etc;
Behavior: Start, Stop, Accelerate etc.
Identity: Registration number etc.

HOW TO CREATE OBJECTS:

class shape
{
private:
int l, b;
public:
void area ()
{
cout<<”area of a rectangle is:” l*b;
}
};

Objects are declared in the similar way how variables are declared.

Shape s1, s2;      //s1 and s2 are objects of type Shape

IMPORTANT FEATURES OF OBJECT:

1.      Each object will be having its own copy of member variables.
2.      But only one copy of member functions is maintained for all the objects of the class.
3.      The size of an object is equal to the sum of the sizes of its member variables.
Member functions add nothing to the size of objects of the Class. Only member variables add to the size of objects of the class.