Access Specifiers
Access Specifiers
A class is a way to bind the data and its associated functions together. Generally a class specification has two parts: 1)class declaration 2) class function definitions The general form of a class declaration is: class class_name { private : Variable declarations; Function declarations; Public: Variable declarations; Function declarations; };
A typical class declaration would look like : Class item { Int number; //variables declaration Float cost; //private by default Public : Void getdata ( int a , float b); //functions declaration Void putdata(void) ; //using prototype }; //ends with semicolon
Member functions can be defined in two places : Outside the class definition. Inside the class definition.
The general form of a member function definition is : Return-type class-name :: function-name(argument declarations)
The general form of a member function definition is : Return-type class-name :: function-name(argument declaration) { Function body } The membership label class-name :: tells the complier that the functionname belongs to class class-name. The symbol :: is called the scope resolution operator.
Another method of defining a member function is to replace the function declaration by the actual function definition inside the class. For example we could define the item class as follows : Class item { int number; float cost; Public : Void getdata ( int a , float b); //declaration Void putdata(void) //defining inside the class { cout<<number<<\n; Cout<<cost<<\n; } };
Once a class has been declared , we can create variables of that type by using the class name(like any other built-in type variable ). For example , Item x; // memory for x is created. Creates a variable x of type item .
In C++ the class variable are known as objects. Therefore x is called an object of type item. We may also declare more than one object in one statement. Example : Item x,y,z;
The private data of a class can be accessed only through the member functions of that Class. The following is the format for calling a member function : Object-name.function-name(actual-arguments); For example the function call statement x.getdata(100,75.5); Is valid and assigns the value 100 to number and 75.5 to cost of object x by implementing the getdata() function.
The class body contains the declaration of variables and functions. These functions and variables are collectively called class members. They are usually grouped under two sections , namely , private and public .
The class members that have been declared as private can be accesses only from within the class. On the other hand ,public members can be accesses from outside the class also. The data hiding (using private declaration) is the key feature of object oriented programming. By default the members of class are private .
Protected members C++ provide a third visibility modifier ,protected which serve a limited purpose to inheritance . A member declared as protected is accessible by the member functions within its class and any class immediately derived from it. It cannot be accesses by functions outside these two classes. A class can now use all the three visibility modes as illustrated below : Class alpha { Private : Protected : .. .. Public : .. };
//visible to member functions //of its own and derived class . // visible to all functions //in the program
#include<iostream.h> Using namespace std; class item { int number; //private by default float cost; //private by default Public : void getdata(int a, float b); //prototype declaration to be defined void putdata(void) { cout<< number:<< number << \n; cout<< cost :<< cost <<\n; } };
//main program.. int main() { item x; //create object x; cout<<\n object x<< \n; x.getdata(100,299.95); //call member functions x.putdata(); //call member functions item y; cout<<\n object y <<\n; y.getdata(200,175.75); y.putdata(); return 0 ; } //create another object
Here is the output of the program Object x Number :100 Cost : 299.95
Object y Number :200 Cost : 175.5