by Dinesh Thakur Category: Classes in C++

The member functions are created and placed in the memory space only once when they are defined as a part of a class specification. Since· all the objects belonging to that class use the same member functions, no separate space is allocated for member functions when the objects are created. For each object, memory is allocated only for member data. Separate memory locations for the objects are essential since the member variables hold different data values for different objects.

 

Example

 

Employee is a class which contains name, age and salary as member data doctor, nurse, worker are three objects of type employee. For each object, memory is allocated only for member data. However, member functions of each object share a common memory area.

 

#include <iostream.h>

class employee

{

private:

char name[15];

int age;

float salary;

public:

void getdata()

{

cout<<"Enter name :";

cin>>name;

cout<<"Enter age :";

cin>>age;

cout<<"Enter salary: ";

cin>>salary;

}

void putdata()

{

cout<<"Name : "<<name<<endl;

cout<<" age : "<<age<<endl;

cout<<"salary : "<<salary<<endl;

} };

void main()

{

employeedoctor,nurse,worker;

cout <<"enter doctor details"<<endl;

doctor.getdata();

cout <<"enter nurse details"<<endl;

nurse.getdata();

cout <<"enter worker details"<<endl;

worker.getdata();

cout <<"Doctor details"<<endl;

doctor.putdata();

cout <<"Nurse details"«endl;

nurse.putdata();

cout <<"Worker details"«endl;

worker.putdata();

}



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles