Oopass 5
Oopass 5
Implement the
following operations:
a. Constructor (including a default constructor which creates the complex number 0+0i).
d. Overloaded << and >> to print and read Complex Numbers.(Operator Overloading)
input
#include<iostream>
class Complex {
public:
Complex () {
real = 0;
imag = 0;
void operator>>(Complex c) {
void operator<<(Complex c) {
cout<<real<<" + "<<imag<<"i"<<endl;
Complex operator+(Complex c) {
Complex temp;
return temp;
Complex operator*(Complex c) {
Complex temp;
return temp;
};
int main() {
c1 >> c1;
c2 >> c2;
c3 = c1 + c2;
c3 << c3;
c3 = c1 * c2;
c3 << c3;
return 0;
output
Enter real part: 1
input
#include <iostream>
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
12
23
12
32
25.43
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>
class Shape {
protected:
float side;
public:
};
public:
void read() {
};
float width;
public:
void read() {
float height;
public:
void read() {
float area() {
float perimeter() {
cout << "Perimeter calculation requires all three sides." << endl;
return 0;
};
int main() {
Circle c;
Rectangle r;
Triangle t;
c.read();
r.read();
t.read();
cout << "Circle: Area = " << c.area() << ", Perimeter = " << c.perimeter() << endl;
cout << "Rectangle: Area = " << r.area() << ", Perimeter = " << r.perimeter() << endl;
return 0;
}
output
Enter details for Circle:
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>
str1 += str2;
str += to_string(a);
int main()
concatenate(str1, str2);
reverseString(str1);
concatenate(10, str1);
return 0;
output
Concatenated string: Hello World!
olleH