Object Oriented Programming Practical 5
Question 1
Write a program to find area of circle such that the class circle must have three functions namely:
a)read() to accept the radius from user.
b)compute() for calculating the area
c)display() for displaying the result.(Use Scope resolution operator)
circle.cpp
C
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class circle
{
public:
int radius;
float area;
void read();
void compute();
void display();
};
void circle::read()
{
cout<<"Enter radius of circle: ";
cin>>radius;
}
void circle::compute()
{
area=3.14*radius*radius;
}
void circle::display()
{
cout<<"Area of circle is: "<<area<<endl;
}
void main()
{
clrscr();
circle c;
c.read();
c.compute();
c.display();
getch();
}
Output

Question 2
Define a class complex with data members real and imaginary, member function read() and write(). Write a program to perform the addition of two complex number and display the result.
complex.cpp
C
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class complex
{
public:
int real,img;
void read()
{
cout<<"\nEnter real part: ";
cin>>real;
cout<<"\nEnter imaginary part: ";
cin>>img;
}
void write()
{
cout<<"\nReal part: "<<real<<endl;
cout<<"\nImaginary part: "<<img<<endl;
}
};
void main()
{
clrscr();
complex c1,c2,c3;
cout<<"Enter complex number 1:";
c1.read();
cout<<"\nEnter complex number 2:";
c2.read();
c3.real=c1.real+c2.real;
c3.img=c1.img+c2.img;
cout<<"\nAddition of two complex numbers is:"<<endl;
c3.write();
getch();
}
Output
