0% found this document useful (0 votes)
36 views

definition of member functions

Uploaded by

ayushamber02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

definition of member functions

Uploaded by

ayushamber02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Class Declaration

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:

1. Class declaration or class member list.

2. Class member function definitions.

Following is the blueprint to declare a class

Syntax

class Class_Name

// declaration of data members

// declaration of member functions

};

For example,

class student

// data members

char name [ 30 ] ;

int class_no, roll_no, age ;

int marks_subj1 , marks_subj2 , avg ;

// member functions only declaration/ prototype

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

int class_no , roll_no , age ;

int marks_subj1 , marks_subj2 , avg ;

public:

void input_details ( ) ;

void compute_average ( ) ;

void display_details ( ) ;

};

Array within a class


C++ lets you create array within the declaration of class. Declaring an array is simple. You need to
specify the size of the array next to array identifier when declaring it. You can declare an array using
the following syntax:
Data_Type Array_Name [ size ] ;
For example,
int students [ 10 ] ;
Example or creating a class having array as data member
class student

private:

char name [ 30 ] ;

int class_no , roll_no , age ;


// array as data member

int marks[5] ;

public:

void input_details ( ) ;

void compute_average ( ) ;

void display_details ( ) ;

};

Defining a member function


Definition of a function includes the function header and the function body. Two methods are used to
define the member functions of a class.
 Defining member function inside the class
 Defining member function outside the class
The second method is highly recommended as it helps to achieve abstraction.

Defining member function inside the class


In this simple method, the definition of the member function is included within the class body. It is
inelegant method of defining a member function as it increases the length of the class member list and
also makes the class look more complicated than it is. Following is an example of a program showing
the definition of member function inside the class.

// 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 member function outside 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 ;

Accessing class members using objects


You cannot access private members of the class from outside the class. You can only access the
data members and member functions of the class that are declared using the public access specifier.

Syntax to access data member


Class_Name . Data_Member ;
Syntax to access member function
Class_Name.Member_Function ( ) ;
For example,
Obj1.input();
Program 1
// defining member function of a class inside class
# include <iostream>
class rectangle //class definition starts here
{

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

// definition of member functions outside the class


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 ;
}
int main()
{
rectangle obj1; // creating object of class rectangle
obj1.input(); // accessing the public member of class rectangle using object obj1
obj1.compute_area();
obj1.display();
}

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

// 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 ;
}
int main()
{
rectangle obj1; // creating object of class rectangle
obj1.input();
obj1.compute_area();
obj1.display();

square obj2; //creating object of class square


obj2.input();
obj2.compute_area();
obj2.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( );

You might also like