Ex5 (Oops)
Ex5 (Oops)
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).
#include <iostream.h>
#include <cmath.h>
class Circle {
public:
void read() {
cout << "Enter the radius of the circle: ";
cin >> radius;
}
void compute() {
area = M_PI * pow(radius, 2);
}
void display() {
cout << "Area of the circle: " << area << endl;
}
private:
double radius;
double area;
};
int main() {
Circle circle;
circle.read();
circle.compute();
circle.display();
return 0;
}
OUTPUT :-
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.
#include <iostream.h>
#include <conio.h>
class Complex {
public:
void read() {
cout << "Enter the real part: ";
cin >> real;
cout << "Enter the imaginary part: ";
cin >> imaginary;
}
void write() {
cout << real << " + " << imaginary << "i" << endl;
}
Complex add(Complex c) {
Complex result;
result.real = real + c.real;
result.imaginary = imaginary + c.imaginary;
return result;
}
private:
double real;
double imaginary;
};
int main() {
Complex c1, c2, sum;
clrscr();
sum = c1.add(c2);
return 0;
}
OUTPUT :-