Member Function Inside 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 and display these data.
#include<iostream>
#include<conio.h>
class person
{
private:
char name[100];
int age;
char gender[100];
char tel;
public:
void input()
{
cout<<"Enter Name:";
cin>>name;
cout<<"Enter Age:";
cin>>age;
cout<<"Enter gender:";
cin>>gender;
cout<<"Enter telephone number:";
cin>>tel;
}
void display()
{
cout<<endl;
cout<<"Details are"<<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();
}
ii. Program to represent a Circle that has member
functions to perform following tasks.
a.Calculate area of circle?
b.Calculate perimeter of the circle
Define member function inside the class.
#include<iostream>
#include<conio.h>
#include<math.h>
class circle
{
private:
float r;
public:
int read()
{
cout<<endl;
cout<<"Enter radius"<<endl;
cin>>r;
}
int area()
{
float A;
A=3.14*pow(r,2);
cout<<"Area is:"<<A<<endl;
}
int perimeter()
{
float p;
p=2*3.14*r;
cout<<"Perimeter is:"<<p;
}
};
int main()
{
circle c1,c2;
c1.read();
c1.area();
cout<<endl;
c1.perimeter();
c2.read();
c2.area();
c2.perimeter();
getch();
}
Output:
Comments
Post a Comment