Member function Outside the class Body
i..Create a class called person that contains approprriate members for storing name
age,gender,tel no, write member function that can read outside the class and display these data.
#include<iostream>
#include<conio.h>
class person
{
private:
char name[100];
int age;
char gender[100];
int tel;
public:
void input();
void display();
};
void person:: input()
{
cout<<"Enter Name:";
cin>>name;
cout<<"Enter Age:";
cin>>age;
cout<<"Enter gender:";
cin>>gender;
cout<<"Enter telephone number:";
cin>>tel;
}
void person:: display()
{
cout<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Age:"<<age<<endl;
cout<<"Gender:"<<gender<<endl;
cout<<"Telephone number:"<<tel;
}
void main()
{
person p;
p.input();
p.display();
getch();
}
Output:
<*********************************************************************>
iii. Create a class Point that represents three dimensional coordinate system.Each object of point should have coordinates x,y,z and methods to assign coordinates to the objects,Add a method to calculate distance from origin and to point (x,y,z) Define member function outside the class body.
#include<iostream>
#include<math.h>
#include<conio.h>
class Point
{
private:
int x,y,z;
public:
void Details();
void calculate();
void display();
};
void Point::Details()
{
cout<<"Enter the value of x:";
cin>>x;
cout<<"Enter the value of y:";
cin>>y;
cout<<"Enter the value of z:";
cin>>z;
}
void Point::calculate()
{
float a;
a = sqrt(pow(x,2)+ pow(y,2)+pow(z,2));
cout<<endl;
cout<<"The distance of coordinate is:"<<a<<endl;
}
void main()
{
Point p1,p2;
p1.Details();
p1.calculate();
cout<<endl;
p2.Details();
p2.calculate();
getch();
}
Output

(Add Distance)
iv. Write a program to define class Distance with data members feet and inches
of appropriate type. Define member function of class which accepts two objects
of class and add them and call member function outside the class body.
#include<iostream.h>
#include<conio.h>
class Distance
{
private:
int feet,inches;
public:
void getdetails();
void display();
void addDistance(Distance,Distance);
};
void Distance::getdetails()
{
cout<<"Enter the value of feet:";
cin>>feet;
cout<<"Enter the value of inches:";
cin>>inches;
}
void Distance::display()
{
cout<<feet<<"-feet "<<inches<<"-inches"<<endl;
}
void Distance::addDistance(Distance d1,Distance d2)
{
inches=d1.inches + d2.inches;
feet=inches/12;
feet=feet+ d1.feet + d2.feet;
}
void main()
{
Distance distance1,distance2,distance3;
cout<<"Enter the first value for distance:"<<endl;
distance1.getdetails();
cout<<endl;
cout<<"Enter the second value for distance:"<<endl;
distance2.getdetails();
cout<<endl;
cout<<"The first value of distance is:"<<endl;
distance1.display();
cout<<endl;
cout<<"the second value of distance is:"<<endl;
distance2.display();
cout<<endl;
distance3.addDistance(distance1,distance2);
cout<<"The distance after addition is :"<<endl;
distance3.display();
getch();
}
Output:

v. Write a program to define a class named time with data members hours,
minutes,seconds. Use appropriate member function of the class which accepts
two objects of class and add them
#include<iostream.h>
#include<conio.h>
class Time
{
private:
int hr,min,sec;
public:
void getdetails();
void display();
void addTime(Time ,Time );
};
void Time::getdetails()
{
cout<<"Enter the hr :";
cin>>hr;
cout<<"Enter the min :";
cin>>min;
cout<<"Enter the sec :";
cin>>sec;
}
void Time::display()
{
cout<<hr<<"-hr "<<min<<"-min "<<sec<<"-sec"<<endl;
}
void Time:: addTime(Time t1,Time t2)
{
sec=t1.sec+t2.sec;
min=sec/60;
sec=sec%60;
min=min+t1.min+t2.min;
hr=min/60;
min=min%60;
hr=hr+t1.hr+t2.hr;
}
void main()
{
Time T1,T2,T3;
cout<<"Enter the first time :"<<endl;
T1.getdetails();
cout<<endl;
cout<<"Enter the second time :"<<endl;
T2.getdetails();
cout<<endl;
cout<<"The first time is:"<<endl;
T1.display();
cout<<endl;
cout<<"the second time is:"<<endl;
T2.display();
cout<<endl;
T3.addTime(T1,T2);
cout<<"The resultant time after addition is :"<<endl;
T3.display();
getch();
}
Output:
vi. Write a program to define a class named complex with data members real and imaginary.
Use appropriate member function of the class which accepts
two objects of class and add them(add real and imaginary part)
#include<conio.h>
class Complex
{
private:
int real,img;
public:
void getdetails();
void display();
void addComplex(Complex C1,Complex C2);
};
void Complex::getdetails()
{
cout<<endl;
cout<<"Real Part :";
cin>>real;
cout<<"Imaginary part :";
cin>>img;
}
void Complex::display()
{
if(img>0)
{
cout<<real<<"+j"<<img;
}
else
{
cout<<real<<"-j"<<(-1)*img;
}
}
void Complex ::addComplex(Complex C1,Complex C2)
{
real=C1.real+C2.real;
img=C1.img+C2.img;
}
void main()
{
Complex first,second,result;
cout<<endl;
cout<<"enter the first complex number:"<<endl;
first.getdetails();
cout<<endl;
cout<<"enter the second complex number:"<<endl;
second.getdetails();
cout<<endl;
cout<<"First complex number is :";
cout<<endl;
first.display();
cout<<endl;
cout<<"Second complex number is :";
cout<<endl;
second.display();
cout<<endl<<"The sum is :";
cout<<endl;
result.addComplex(first,second);
result.display();
getch();
}
FOR N NUMBER
vi.Create a class Employee that have name,id,age,salary and gender for n number of employee.
Define the member function outside the class body.
#include<iostream>
#include<conio.h>
class Employe
{
private:
char name[100];
int id;
int age;
int salary;
public:
void getDetails();
void display();
};
void Employe::getDetails()
{
cout<<endl;
cout << "Enter name: " ;
cin >> name;
cout << "Enter ID number: ";
cin >> id;
cout << "Enter age: ";
cin >> age;
cout<<"ENter salary: ";
cin>>salary;
}
void Employe::display()
{
cout<<endl;
cout << "Employe details:"<<endl<<endl;
cout << "Name:"<< name <<endl;
cout<< ",ID Number:" << id <<endl;
cout<< "Age:" <<age<<endl;
cout<<"Salary:"<<salary<<endl;
}
void main()
{
Employe E[100];
int n,i;
cout << "Enter total number of employe:";
cin >> n;
cout<<endl;
for(i=0;i< n; i++)
{
cout<<endl;
cout << "Enter details of employe" << (i+1) << ":\n";
E[i].getDetails();
}
cout << endl;
for(i=0;i< n; i++)
{ cout<<endl;
cout << "Details of employe " << (i+1) << ":\n";
E[i].display();
}
getch();
}
Output:
![]() |
This is output 1 |
Comments
Post a Comment