0% found this document useful (0 votes)
9 views

Ex5 (Oops)

.

Uploaded by

suyashchavan301
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Ex5 (Oops)

.

Uploaded by

suyashchavan301
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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).

#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();

cout << "Enter the first complex number:" << endl;


c1.read();

cout << "Enter the second complex number:" << endl;


c2.read();

sum = c1.add(c2);

cout << "Sum of the complex numbers: ";


sum.write();

getch(); // Wait for a key press before exiting

return 0;
}
OUTPUT :-

You might also like