definition of member functions
definition of member functions
Class is a user defined data type which is a collection of data members and member functions.
Structures in C were used to bind the related data items and did not include the functions. C++ lets
create and use classes just like any other default data type. A class consists of two parts:
Syntax
class Class_Name
};
For example,
class student
// data members
char name [ 30 ] ;
void input_details ( );
void compute_average ( ) ;
void display_details ( );
};
Definition of member functions of a class will be done outside the class in this case.
Access specifiers
There is another major difference between structures in C and classes in C++ and that is the concept
of data hiding. You can restrict the access to class members which is not possible to structures in C++.
You can declare class members inside the class declaration using two access specifiers called private
and public. Access specifier is used to specify the access rules for the class members.
Class members declared as private cannot be accessed from outside the class. Whereas class
members declared as public can be accessed from both inside and outside the class. Generally, the
data members are declared as private and member functions are declared as public.
class student
private:
char name [ 30 ] ;
public:
void input_details ( ) ;
void compute_average ( ) ;
void display_details ( ) ;
};
private:
char name [ 30 ] ;
int marks[5] ;
public:
void input_details ( ) ;
void compute_average ( ) ;
void display_details ( ) ;
};
// defining member function of a class inside the member list or body of the class
# include <iostream>
class rectangle
{
private:
int length , breadth , area ;
public:
void input ( )
{
std::cout<<" \n Enter length of the rectangle :- " ;
std::cin>>length ;
std::cout<<" \n Enter breadth of the rectangle :- " ;
std::cin>>breadth ;
}
void compute_area ( )
{
area = length * breadth ;
}
void display ( )
{
std::cout<<" \n Area of rectangle :- " << area ;
}
};
As you can see in the code above, all three member functions of the class rectangle are defined within
the body of the class.
Defining the member function outside the class is the right approach to follow. It helps reduce the size
of the class and makes the class structure easy to understand. Only the declarations of the member
functions and data members are included in the class body.
// defining member function of a class outside the member list or body of the class
# include <iostream>
Class rectangle
{
private:
int length , breadth , area ;
public:
// prototype/ declare of member functions
void input ( ) ;
void compute_area ( ) ;
void display ( ) ;
};
// definition of member functions of class rectangle
void rectangle::input ( )
{
std::cout<< " \n Enter length of the rectangle :- " ;
std::cin>>length ;
std::cout<< " \n Enter breadth of the rectangle :- " ;
std::cin>>breadth ;
}
void rectangle::compute_area ( )
{
area = length * breadth ;
}
void rectangle::display ( )
{
std::cout<<" \n Area of rectangle :- " << area ;
}
Prototypes or declarations of the member functions have been given inside the class body using the
following statements:
// prototypes of member functions
void input ( ) ;
void compute_area ( ) ;
void display ( ) ;
Function header and function definition of all the three member functions is given outside the class.
Consider the following definition of one member function:
void rectangle::input ( )
{
cout<< " \n Enter length of the rectangle :- " ;
cin>>length ;
cout<< " \n Enter breadth of the rectangle :- " ;
cin>>breadth ;
}
The function header contains a scope resolution operator. Scope resolution operator is used when
you define the member function outside the class. It is used to indicate that the member function
belongs to which class. For example, following function header indicates that the member function
belongs to class rectangle.
void rectangle::input ( )
It is particularly useful when the program consists of two classes having the same member functions.
// defining member function of a 2 classes outside the member list or body of the class
# include <iostream>
class rectangle
{
private:
int length , breadth , area ;
public:
//prototypes of member functions
void input ( ) ;
void compute_area ( ) ;
void display ( ) ;
};
class square
{
private:
int length , area ;
public:
// prototypes of member functions
void input();
void compute_area ( ) ;
void display ( ) ;
};
// definition of member functions of class rectangle
void rectangle::input ( )
{
std::cout<< " \n Enter length of the rectangle :- " ;
std::cin>>length ;
std::cout<< " \n Enter breadth of the rectangle :- " ;
std::cin>>breadth ;
}
void rectangle::compute_area ( )
{
area = length * breadth ;
}
void rectangle::display ( )
{
std::cout<< " \n Area of rectangle :- " << area ;
}
// definition of member functions of class square
void square::input ( )
{
std::cout<< " \n Enter length of the square :- " ;
std::cin>>length ;
}
void square::compute_area ( )
{
area = length * length ;
}
void square::display ( )
{
std::cout<< " \n Area of square :- " << area ;
}
Explanation
Consider the following 2 function definitions:
void rectangle::display ( )
{
cout<< " \n Area of rectangle :- " << area ;
}
void square::display ( )
{
cout<< " \n Area of square :- " << area ;
}
It is clear from the function header of the first function definition that it belongs to class rectangle and
second function definition belongs to class square.
Creating Object
Class is simply a user defined data type. You have to create objects of the same to make use of it.
Object is known as the instance of the class.
You can create objects of the class just like you create objects of the structure data type.
Syntax to create object of a class
Class_Name Object_List;
For example,
class student
{
// class member list
};
//after few statements
student s1, s2;
The last statement is used to create two objects of the class student.
You can also create the objects of a class using the following approach
class Class_Name
{
// class member list
} Object_List ;
For example,
class student
{
// class member list
} s1, s2 ;
private:
// data members
int length , breadth , area ;
public:
//member functions
void input ( )
{
std::cout<<" \n Enter length of the rectangle :- " ;
std::cin>>length ;
std::cout<<" \n Enter breadth of the rectangle :- " ;
std::cin>>breadth ;
}
void compute_area ( )
{
area = length * breadth ;
}
void display ( )
{
std::cout<<" \n Area of rectangle :- " << area ;
}
}; // class definition finishes here
int main() //program execution starts from here
{
rectangle obj1; // we have created an object obj1 of class rectangle
obj1.input(); // Accessing the public member functions of class rectangle using object obj1
obj1.compute_area();
obj1.display();
}
Program 2
// defining member function of a class outside class
# include <iostream>
class rectangle // class definition begins from here
{
private:
// data members
int length , breadth , area ;
public:
// prototypes of member functions
void input ( ) ;
void compute_area ( ) ;
void display ( ) ;
}; // class definition finishes here
Program 3
// defining member function of 2 classes outside class
# include <iostream>
class rectangle
{
private:
int length , breadth , area ;
public:
//prototypes of member functions
void input ( ) ;
void compute_area ( ) ;
void display ( ) ;
};
class square
{
private:
int length , area ;
public:
// prototypes of member functions
void input();
void compute_area ( ) ;
void display ( ) ;
};
Array of objects
Just like an array of default data types, you can also create an array of objects of a class. It is particularly
useful when you wish to create large number of objects of the class.
For example, array of objects for the employee class or student class.
Syntax to create array of objects:
Class_Name Object_Name[size] ;
For example,
class student obj[3];
The last statement is used to create an array of 3 objects of student class.
You can access the member function of the class using an element of object array as follows:
Object_Name[Index].Member_Function ( );
Program 4
To create an array of class objects
#include<iostream> // header files
class student // definition of class student
{
private:
int roll_no;
int class_no;
int marks;
public:
void input_details ( )
{
std::cout<<"Enter the students class :- ";
std::cin>>class_no;
std::cout<<"Enter the students roll number:- ";
std::cin>>roll_no;
std::cout<<"Enter the students marks :- ";
std::cin>>marks;
}
void display_details ( )
{
std::cout<<"\nStudents class:- "<<class_no;
std::cout<<"\nStudents Roll number:- "<<roll_no;
std::cout<<"\nStudents marks :- "<<marks;
}
};
int main ()
{
// array of objects of student class
student obj[3]; // creating array of objects
int a;
for (a=0; a<=2;a++)
{
std::cout<< “ \n Enter details of student no. “<< a + 1;
obj[a].input_details( );
}
for (a=0; a<=2;a++)
{
cout<< “ \n Details of student no. “<< a + 1;
obj[a].display_details( );
}
}
Following statement is used in the last program to create an array of 2 objects for the “student” class:
// array of objects of student class
student obj[2];
For loop is used to access each object of the array and one of the member function is accessed using the
following statement:
obj[a].input_details( );