0% found this document useful (0 votes)
70 views13 pages

Access Specifiers

- A class binds data and functions together through a class declaration with two parts: a class declaration and function definitions. - A class declaration specifies private and public variables and functions using access specifiers. Member functions can be defined inside or outside the class. - Objects of a class are created by declaring variables of the class type. Private data can only be accessed by member functions while public members can be accessed inside and outside the class. - The protected access specifier allows access to members within the class and derived classes but not outside. A C++ program demonstrates creating class objects and using member functions to access private data.

Uploaded by

Ankesh Kunwar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views13 pages

Access Specifiers

- A class binds data and functions together through a class declaration with two parts: a class declaration and function definitions. - A class declaration specifies private and public variables and functions using access specifiers. Member functions can be defined inside or outside the class. - Objects of a class are created by declaring variables of the class type. Private data can only be accessed by member functions while public members can be accessed inside and outside the class. - The protected access specifier allows access to members within the class and derived classes but not outside. A C++ program demonstrates creating class objects and using member functions to access private data.

Uploaded by

Ankesh Kunwar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

SPECIFYING A CLASS

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 simple class example

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

Defining member functions

Member functions can be defined in two places : Outside the class definition. Inside the class definition.

Outside the class definitions

The general form of a member function definition is : Return-type class-name :: function-name(argument declarations)

Outside the class definitions

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.

Inside the class definition

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; } };

Creating class objects

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;

Accessing class members

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.

Access specifiers --- public , private and protected

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 .

Private members and public members

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 : .. };

//optional //visible to member functions within its class

//visible to member functions //of its own and derived class . // visible to all functions //in the program

A C++ program with classes

#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; } };

Void item::getdata(int a , float b) { number =a; cost =b; }

//use membership label


//private variables //directly used

//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

You might also like