0% found this document useful (0 votes)
8 views9 pages

Oopass 5

The document describes the implementation of multiple C++ classes demonstrating operator overloading and virtual functions. It includes a Complex class for complex numbers, a Distance class for measuring distances, a Shape class with derived classes for different geometric shapes, and a StringOperations class for string manipulations. Each section provides code examples and expected outputs for various operations.

Uploaded by

chaitanya14244
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)
8 views9 pages

Oopass 5

The document describes the implementation of multiple C++ classes demonstrating operator overloading and virtual functions. It includes a Complex class for complex numbers, a Distance class for measuring distances, a Shape class with derived classes for different geometric shapes, and a StringOperations class for string manipulations. Each section provides code examples and expected outputs for various operations.

Uploaded by

chaitanya14244
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/ 9

1. Implement a class Complex, which represents the Complex Number data type.

Implement the
following operations:

a. Constructor (including a default constructor which creates the complex number 0+0i).

b. Overloaded operator + to add two complex numbers.

c. Overloaded operator * to multiply two complex numbers.

d. Overloaded << and >> to print and read Complex Numbers.(Operator Overloading)

input
#include<iostream>

using namespace std;

class Complex {

int real, imag;

public:

Complex () {

real = 0;

imag = 0;

void operator>>(Complex c) {

cout << "Enter real part: ";

cin >> real;

cout << "Enter imaginary part: ";

cin >> imag;

void operator<<(Complex c) {

cout<<real<<" + "<<imag<<"i"<<endl;

Complex operator+(Complex c) {

Complex temp;

temp.real = real + c.real;

temp.imag = imag + c.imag;

return temp;

Complex operator*(Complex c) {

Complex temp;

temp.real = real * c.real - imag * c.imag;

temp.imag = real * c.imag + imag * c.real;

return temp;

};
int main() {

Complex c1, c2, c3;

c1 >> c1;

c2 >> c2;

cout << "Addition of two complex numbers: ";

c3 = c1 + c2;

c3 << c3;

cout << "Multiplication of two complex numbers: ";

c3 = c1 * c2;

c3 << c3;

return 0;

output
Enter real part: 1

Enter imaginary part: 2

Enter real part: 1

Enter imaginary part: 2

Addition of two complex numbers: 2 + 4i

Multiplication of two complex numbers: -3 + 4i


2. Write a program defining class Distance that stores feet and inches. Overload operator + to add two
instances of class Distance and return the result in another instance. Overload comparison operator <
which will compare two instances of class Distance and return true or false. Overload += to add two
distances and store the result in the one of them. Overload = operator to assign one distance to the
other. (Operator Overloading)

input

#include <iostream>

using namespace std;

class Distance{
int feet,inch;
public: Distance(){
feet = 0;
inch = 0;
}
public: void operator>>(Distance C1){
cout<<"Enter the input:"<<endl;
cin>>feet>>inch;
}
public: void operator<<(Distance C1){
cout<<"The output is:"<<endl;
cout<<feet<<"."<<inch<<endl;
}
public: Distance operator+(Distance d){
Distance temp;
temp.feet = feet + d.feet;
temp.inch = inch + d.inch;
if(temp.inch >= 12){
temp.feet = temp.feet + 1;
temp.inch = temp.inch - 12;
}
return temp;
}
public: bool operator<(Distance d){
if(feet >= d.feet){
if(inch < d.inch){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
public: void operator+=(Distance d){
feet = feet + d.feet;
inch = inch + d.inch;
if(inch >= 12){
feet = feet + 1;
inch = inch - 12;
}
}
};

int main() {
Distance C1,C2,C3;
C1<<(C1);
C1>>(C1);
C2>>(C2);
C3 = C1 + C2;
C3<<(C3);

int a;
a =C1<C2;
if(a==true){
cout<<"C1 is less than C2."<<endl;
}
else{
cout<<"C1 is greater than C2"<<endl;
}

C1+=C2;
C1<<(C1);

return 0;
}

output
The output is:

0.0

Enter the input:

12

23

Enter the input:

12

32

The output is:

25.43

C1 is less than C2.

The output is:

25.43
3. Write a C++ program to implement a class called Shape with virtual member functions for
calculating area and perimeter. Derive classes such as Circle, Rectangle, and Triangle from the Shape
class and override virtual functions accordingly. (Virtual Function)

input

#include <iostream>

using namespace std;

class Shape {

protected:

float side;

public:

virtual void read() = 0;

virtual float area() = 0;

virtual float perimeter() = 0;

};

class Circle : public Shape {

public:

void read() {

cout << "Enter the radius of the circle: ";

cin >> side;

float area() { return 3.14 * side * side; }

float perimeter() { return 2 * 3.14 * side; }

};

class Rectangle : public Shape {

float width;

public:

void read() {

cout << "Enter the length of the rectangle: ";

cin >> side;

cout << "Enter the width of the rectangle: ";

cin >> width;

float area() { return side * width; }

float perimeter() { return 2 * (side + width); }


};

class Triangle : public Shape {

float height;

public:

void read() {

cout << "Enter the base of the triangle: ";

cin >> side;

cout << "Enter the height of the triangle: ";

cin >> height;

float area() {

return 0.5 * side * height;

float perimeter() {

cout << "Perimeter calculation requires all three sides." << endl;

return 0;

};

int main() {

Circle c;

Rectangle r;

Triangle t;

cout << "Enter details for Circle:" << endl;

c.read();

cout << "Enter details for Rectangle:" << endl;

r.read();

cout << "Enter details for Triangle:" << endl;

t.read();

cout << "Circle: Area = " << c.area() << ", Perimeter = " << c.perimeter() << endl;

cout << "Rectangle: Area = " << r.area() << ", Perimeter = " << r.perimeter() << endl;

cout << "Triangle: Area = " << t.area() << endl;

return 0;

}
output
Enter details for Circle:

Enter the radius of the circle: 3

Enter details for Rectangle:

Enter the length of the rectangle: 4

Enter the width of the rectangle: 3

Enter details for Triangle:

Enter the base of the triangle: 4

Enter the height of the triangle: 3

Circle: Area = 28.26, Perimeter = 18.84

Rectangle: Area = 12, Perimeter = 14

Triangle: Area = 6
4. Implement a class StringOperations with overloaded functions to concatenate two strings, to
concatenate a string with an integer, and to reverse a string. Test these functions with various inputs to
demonstrate their versatility. em. Overload = operator to assign one distance to the other. (Operator
Overloading)

input

#include<iostream>

using namespace std;

void concatenate(string str1,string str2)

str1 += str2;

cout << "Concatenated string: " << str1 << endl;

void reverseString(string str)

int len = str.length();

for (int i = len - 1; i >= 0; i--)

cout << str[i];

cout << endl;

void concatenate(int a,string str)

str += to_string(a);

cout << "Concatenated string: " << str << endl;

int main()

string str1 = "Hello ";

string str2 = "World!";

concatenate(str1, str2);

reverseString(str1);

concatenate(10, str1);

return 0;

output
Concatenated string: Hello World!

olleH

Concatenated string: Hello 10

You might also like