Classes and Objects
Classes and Objects
RAMAPURAM CAMPUS
OBJECT ORIENTED
PROGRAMMING
Class
• A class is one of the most fundamental needs for object oriented programming. For a problem to
be solved in C++ the entities of the problem are to be represented as classes. Suppose we have to
write a program for printing student mark sheets. The student, examiner, supervisor, mark sheet,
merit list all are the entities in this case.
• Entities are represented as classes in a C++ program. The information that we are interested in
storing about these entities is known as the attributes of the class. Attributes are of two types: data
and methods (functions).
• One of the entities of the mark sheet program is the examiner. The data attributes of this entity
(examiner) may be examiner’s subject, name, address, phone number, and affiliation. Function
attributes of the examiner entity can be reading and printing details about examiners, assigning
bunch of answer sheets etc.
Definition of a class
• A class is a way to bind the data and its associated functions together. It
allows the data to be hidden if necessary from external use. While defining a
class, we are creating a new abstract data type that can be treated as a built-in
data type. A class specification has two parts:
2. Class function definitions – describe how the class functions are implemented.
The keyword class specifies that what follows is an abstract data of type class_name.
The body of the class is enclosed within braces and terminated by a semicolon.
The class body contains the declaration of variables and functions. These functions and
variables are collectively called class members.
The keywords private and public are known as visibility labels and it specify which
members are private which of them are public. These should followed by a colon.
• Private members can be accessed only within the class whereas public members can
be accessed form outside the class. By default, the members of a class are private. If
both the labels are missing, the members are private to the class.
Objects
• Defining a class will not create any object as specifying just int will not create
any variable. Once a class has been declared we can create variable of that type
by using the class name.
• For example :
item x,y,z;
Access Specifiers
Access specifiers are used to access the data members and member functions of the class. There are
three types of access specifiers :
private
public
protected
private: the attributes that provide services (access) only to member functions are kept to be private.
private is the default access mode for class members.
public : the attributes that provide services (access) to outsiders are to be kept as public.
protected : a member declared as protected is accessible by the member functions within its class and
class immediately derived from it. It cannot be accessed by functions outside these two classes (base
and derived).
class alpha
{
private: // optional
……. // visible to member functions
……. // within its class
protected:
……. // visible to member functions
……. // of its own and derived class
public:
……. // visible to all functions
……. // in the program
}
Private Yes No No
•The variables declared inside the class are known as data members and the
functions as member functions. Only the member functions can have access to
the private data and private functions. But the public members can be accessed
from outside the class.
• Example:
class item
{
int num; // variables declarations private by default
float cost;
public:
void getdata(int, float); // function declaration using prototype
void putdata();
};
Example:
void item :: getdata(int a,float b)
{
num = a;
cost = b;
}
void item ::putdata()
{
cout << “Num = “<< num <<”\nCost = “<<cost;
}
Characteristics:
Several different classes can use the same function name. The ‘membership’ label will resolve their scope.
Member function can access the private data while a non member function cannot.
A member function can call another member function directly, without using dot operator.
• This method replaces the function declaration by the function definition inside the class. When the
function is defined inside the class it is treated as a inline function. Normally only small functions are defined
inside the class definition.
class item
{
int num; // variables declarations private by default
float cost;
public:
void getdata(int, float); // function declaration using prototype
void putdata()
{
cout << “Num = “<< num <<”\nCost = “<<cost;
}
};
The private data of a class can be accessed only by its member functions. Hence
main() cannot access the private data of a class. The syntax for calling the member
function is
object-name.function-name(actual-arguments);
With respect to the above example,
x.getdata(27,65.5);
It assigns the value 27 to num and 65.5 to cost. Similarly,
x.putdata();
will display the values of num and cost.
Remember a member function can be invoked only by using an object.