classes and objects
classes and objects
INTRODUCTION
The elementary concept of object oriented programming begins with the use of classes and
objects. A class is a very powerful keyword in C++. A class declaration defines new user-defined
data types that link data and the code that manipulates the data. In other words, a class
combines data elements and functions (operations) into a single entity.
Let us look at the terminologies in procedural programming language and object
oriented programming.
The data elements in a class are called member data and the functions in a class are
called member functions.
Definition and declaration of classes and objects:
A class definition is a process of naming a class and data variables, and interface operations of
the class.
A class declaration specifies the representation of objects of the class and set of operations that
can be applied to such objects.
The definition and declaration of a class indicates the following:
➢The data variables known as member data of a class, describe the characteristics of a
class.
➢Member functions are the set of operations that are performed on the objects of the class.
There may be zero or more member functions in a class. This is also known as interface.
➢The access control specifiers to the class members within a program are specified.
➢Class tag name for external operations for accessing and manipulating the instance of a class.
The General syntax for defining a class is as follows:
class user_defined_name
{
private:
Member data Member functions
protected:
Member data Member functions
public:
Member data Member functions
};
classes and objects
Keyword class is used to declare a class. User_defined_name is the name of the class.
Class body is enclosed in a pair of flower brackets. Class body contains the declaration of its
members (data and functions). There are generally three types of members namely
private, public and protected. These are discussed in the following section.
The class function definition describes how the class functions are implemented.
Example : Let us declare a class for representation of bank account.
class account
{
private: //implicit by default int accno;
char name[20[; char acctype[4]; int bal _amt;
public: //member functions
void get_data(); void displaydata();
}
In the above example, class name account is a new type identifier that is used to
declare instance of that class type. The class account contains four member data and two
methods or member functions. Both of these member data are private by default, while
both member functions are public by default.
The functions get_data( ) and put_data( ) are used to write instructions to perform operations
on member data. These two functions only can provide access to get member data from outside
the class. Therefore, the data cannot be accessed by any other function that is not a member of
the class account.
Access specifiers
An object is defined to be an instance of a class. Every data member of a class is specified by
three levels of access protection for hiding data and function members internal to the class. The
access specifiers define the scope of data. The access specifiers are indicated using
the following keywords.
private access means a member data can only be accessed by the member function. Members
declared under private are accessible only within the class. If no access specifier is mentioned,
then by default, members are private.
Example : private:
int x;
float y;
public
public access means that members can be accessed by any function outside the class also.
Example: public:
Void getdata();
Thus keyword public identifies both class data and functions that constitute the public interface
for the class.
Protected
The members which are declared using protected can be accessed only by the member
functions, friends of the class and also by the member functions derived from this class. The
members cannot be accessed from outside. The protected access specifier is therefore similar to
private specifier.
Member functions
Member functions can be defined in two places:
• Inside class definition
• Outside class definition
{
int length;
int breadth; public: void get_data ()
{
cin>>length; cin>>breadth;
}
void put_data (void)
{
cout <<length ; cout <<breadth;
}
};
{
function body;
}
• Type and number of arguments in member function must be same as types and number
of data declared in class definition.
• The symbol :: is known as scope resolution operator. Usage of :: along with class name is
the header of function definition. The scope resolution operator identifies the function as
a member of particular class.
▪ Several classes can use same function name. Membership label will resolve their scope.
• Member function can access private data of a class. A non-member function cannot.
Example : The following program segment shows how a member function is defined
outside class.
Class operation
{
private:
int a; int b; public:
int sum();
int product();
};
int operation::sum()
{
return(a+b);
}
int operation(product)
{
return(a*b);
}
private: //members
public: //methods
};
User_defined_name objec1, oject2,….;
Example: class num
{
private:
private:
int x; int y;
public:
int sum(int p, int q); int diff(int p, int q);
};
void main()
{
num s1,s2; s1.sum(200,300); s2.diff(600,500);
}
Note that an object is an instance of a class template.
Array of objects
An array having class type elements is known as array of objects. An array of objects is declared
after class definition and is defined in the same way as any other array.
In pass by value, a copy of the object is passed to the function. The function creates its own
copy of the object and uses it. Therefore changes made to the object inside the function do not
affect the original object.
∗∗∗∗∗