You are on page 1of 76

LAB MANUAL

On

Object Oriented Programming


(ETCS112A)
Bsc cyber security -
II Semester
nd

Submitted By: Submitted To:


Devansh mittal Mr. Shamim Ahmad
(2201830016)
INDEX

S.NO TOPIC
OOPS (Object Oriented Programming) Programs

Program-1 // Write a program to print “Hello World” -------->

#include<iostream>

using namespace std;

int main () {

cout<<"Hello World” ;

return 0 ;

Output—

Program-2// Write a program to accept your name from user and print hello with
your name----->

#include <iostream>

using namespace std;

int main(){

char name[26];

cout<<"enter your name: "<<endl;


cin>>name;

cout<<"HELLO "<<name;

Output—

Program-3// Write a program to print “hello” with your full name--

#include <iostream>

using namespace std;

int main(){

string name;

cout<<" ENTER YOUR NAME ";

getline(cin, name);

cout<<"HELLO "<<name;

return 0;
}

Output—

Program-4// Write a program to print and calculate the electricity bill----->

#include <iostream>

using namespace std;

int main(){

float unit , bill;

cout<<"Number of units consumed"<<endl;

cin>>unit;

if ((unit>0) && (unit<201)){

bill=unit*0;

cout<<"the electricity bill is"<<bill<<endl;}

else if ((unit>200) && (unit<401)){

bill=unit*5;

cout<<"the electricity bill is"<<bill<<endl;}

else if ((unit>400) && (unit<601)){


bill=unit*8;

cout<< "the electricity bill is"<<bill<<endl;}

else if ((unit>600 && unit<1001)){

bill=unit*10;

cout<< "the electricity bill is"<<bill<<endl;}

else if (unit>1000){

bill=unit*15;

cout<< "the electricity bill is"<<bill<<endl;}

return 0;

Output—

Program-5// Write a program to print “good morning” or “hello” using


conditional statements--- >

#include <iostream>
using namespace std;

int main() {

int a=5,b=7,c=3;

if ((a>b)&&(a>c))

cout<<"hello";

if ((a>b)||(a>c))

cout<<"good morning";

if (!a)

cout<<"bye";

return 0;

Output—

Program-6 // Write a program to print natural numbers from 1 to 10 using for


loop---- >

#include <iostream>

using namespace std;


int main(){

for(int i=1; i<11;i++)

cout<<i<<endl;

Output—

Program-7// Write a program to print even numbers from 1 to 20 using for loop--
-- >

#include <iostream>

using namespace std;

int main(){

int i;

for(i=2; i<=20; i=i+2

cout<<i<<endl

}
Output--

Program-8// Write a program to print the table of five using for loop--- >

#include <iostream>

using namespace std;

int main(){

int i;

for(i=1;i<=10;++i)

cout<<"5 * "<<i<<" = "<<i*5<<endl;

Output—
Program-9// Write a program to print the squares of first 10 natural numbers
using for loop, while loop and do-while loop--- >

#include <iostream>

using namespace std;

int main(){

//***FOR LOOP

int i;

for(i=1;i<=10;i++){

cout<<i*i<<endl;

//****WHILE LOOP

int i=1;

while(i<=10){

cout<<i*i<<endl;

i++;
}

//****DO WHILE LOOP

int i=1;

do{

cout<<i*i<<endl;

i++;

}while(i<=10);

Output—

Program-10// Write a program to print the factorial of a number using for , while
and do- while loop--- >
#include <iostream>

using namespace std;

int main()

//*****FOR LOOP

int i, x = 1, num;

cout << "Enter any Number: ";

cin >> num;

for(i=1;i<=num;i++){

x=x*i;

cout<<"Factorial of " <<num<<" is: "<<x<<endl;

//*****WHILE LOOP

i=1;

while(i<=num){

x*=i;

i++;

cout<<"the factorial is "<<x<<endl;

//******DO WHILE

i = 1;

do
{

x = x * i;

i++;

} while (i <= num);

cout << "the factorial of " << num << " is " << x << endl;

Output—

Program-11//Write a program to find whether a number is prime or not using for,


while and do-while loop--- >

#include <iostream>

using namespace std;

int main(){

int x, i;

//bool a = true;

cout << "Enter any number: ";


cin >> x;

//FOR LOOP**********

for (i = 2; i < x; ++i){

if (x % i == 0){

a = false;

break;

if (a == true)

cout << x << " is a prime number.";

else

cout << x << " is not a prime number.";

//WHILE LOOP******

int f = 0;

i = 2;

while(i <= x/2)

if(x%i == 0)

f=1;
break;

i++;

if(f == 0)

cout<<x<<" is a Prime Number"<<endl;

else

cout<<x<<" is Not a Prime Number"<<endl;

//DO WHILE***********

do{

if(x%i==0)

f=1;

break;

}i++;

}while(i<=x/2);

if(f==0)

cout<<x<<" is a prime number"<<endl;

else

cout<<x<< "is not a prime number"<<endl;


return 0;

Output—

Program-12//Write a program to accept the day number and display the


corresponding date and date-------->

#include <iostream>

using namespace std;

int main(){

int n;

cout<<"Enter a day number: ";


cin>>n;

switch(n)

case 1: cout<<"Monday"<<endl;

break;

case 2: cout<<"Tuesday"<<endl;

break;

case 3: cout<<"wednesday"<<endl;

break;

case 4: cout<<"Thursday"<<endl;

break;

case 5: cout<<"Friday"<<endl;

break;

case 6: cout<<"Saturday"<<endl;

break;

case 7: cout<<"Sunday"<<endl;

break;

default: cout<<"Enter a valid input";

Output—
Program-13//write a program to accept a character from keyboard check whether
it is a vowel or consonant------->

#include <iostream>

using namespace std;

int main(){

char n;

cout<<"Enter a character: ";

cin>>n;

switch(n)

case 'A': cout<<"VOWEL"<<endl;

break;

case 'a': cout<<"VOWEL"<<endl;

break;

case 'E': cout<<"VOWEL"<<endl;

break;

case 'e': cout<<"VOWEL"<<endl;


break;

case 'I': cout<<"VOWEL"<<endl;

break;

case 'i': cout<<"VOWEL"<<endl;

break;

case 'O' : cout<<"VOWEL"<<endl;

break;

case 'o' : cout<<"VOWEL"<<endl;

break;

case 'U': cout<<"VOWEL"<<endl;

break;

case 'u': cout<<"VOWEL"<<endl;

break;

default: cout<<"CHARACTER IS CONSONANT";

Output—
Program-14//write a program to accept a number from 1 to 5 1 namaste 2 hello 3
good morning 4 good night 5 exit----->

#include <iostream>

using namespace std;

int main(){

char choice='y';

do{

cout<<"\n\n------WELCOME TO GREETINGS------->"<<endl;

cout<<"press 1 for namaste"<<endl;

cout<<"press 2 for hello"<<endl;

cout<<"press 3 for good morning"<<endl;

cout<<"press 4 for good night"<<endl;

int n;
cout<<"Enter a number: ";

cin>>n;

switch(n)

case 1: cout<<"NAMASTE"<<endl;

break;

case 2: cout<<"HELLO"<<endl;

break;

case 3: cout<<"GOOD MORNING"<<endl;

break;

case 4: cout<<"GOOD NIGHT"<<endl;

break;

default: cout<<"enter a valid menu number";

cout<<"\n\nDo you want to continue ";

cin>>choice;

}while(choice=='y');

}
Output—

Program-15//write a menu based program to print sum of digits of a number in


case 1 ..reverse digits in case 2.... check whether palidrone or not in case 3------->

#include <iostream>

using namespace std;

int main()

int n;

char choice = 'y';

do

string ad;

ad = "<<---------------------------------------------------------------->>";

cout << " " << endl;

cout << ad << endl;

cout << "| <<======WELCOME TO MENU BASED


PROGRAM======>> |" << endl;
cout << ad << endl;

cout << "| Press 1 for sum of digits |" << endl;

cout << "| Press 2 for reverse the digits |" << endl;

cout << "| Press 3 for check whether a number is palidrone or not |" <<
endl;

cout << ad << endl;

cout << "\nplease Enter your choice ";

cin >> n;

switch (n)

case 1:

int digit1, digit2, sum;

cout << "Enter 1st digit: " << endl;

cin >> digit1;

cout << "Enter 2nd digit: " << endl;

cin >> digit2;

sum = digit1 + digit2;

cout << "The sum of digits are " << sum << endl;

break;

case 2:

int i, reverse, rem;


reverse = 0;

cout << "Enter a number: ";

cin >> i;

while (i != 0)

rem = i % 10;

reverse = reverse * 10 + rem;

i = i / 10;

cout << "Reversed Number: " << reverse << endl;

break;

case 3:

int a, r, num, temp;

num = 0;

cout << "Enter the Number=";

cin >> a;

temp = a;

while (a > 0)

r = a % 10;

num = (num * 10) + r;

a = a / 10;
}

if (temp == num)

cout << "Number is Palindrome." << endl;

else

cout << "Number is not Palindrome." << endl;

break;

default:

cout << "ENTER A VALID INPUT" << endl;

break;

cout << "\n\nDO YOU WANT TO CONTINUE (y/n)";

cin >> choice;

} while (choice == 'y');

cout << "\nTHANKYOU...have a nice day";

Output—
Program-16// write a program to add the elements of an array---->

#include <iostream>

using namespace std;

int main(){

int a[5]={7,9,3,4,5};

int i;

int sum=0;

for(i=0;i<5;i++){

sum=sum+a[i];

cout<<"the sum of elements are "<<sum<<endl;

return 0;

}
Output—

Program-17//write a program in c++ to calcultae and print sum of firt N natual


numbers

#include <iostream>

using namespace std;

int mai(){

int num, sum;

sum=0;

cout << "Enter the number ";

cin >> num;


for (int i = 1; i <= num; ++i) {

sum += i;

cout<<sum;

return 0;

Output—

Program-18//Write a program to calculate and print circumference of a circle and


area of circle using function in c++

#include<iostream>

using namespace std;

float area(float);

float circum(float);
int main()

int radius;

cout<<"\n Enter Radius of Circle: ";

cin>>radius;

cout<<"\n Area of Circle : "<<area(radius);

cout<<"\n Circumference of Circle : "<<circum(radius);

float area(float radius)

return (3.14 * radius * radius);

float circum(float radius)

return(2 * 3.14 * radius);

Output—
Program-19//write a program to print a 2D array in matrix

#include <iostream>

using namespace std;

int main()

const int i = 3, j = 3;

int arr[i][j] = {{1, 2, 3},

{4, 5, 6},

{7, 8, 9}};

for(int a = 0; a < 3; a++)

for(int b = 0; b < 3; b++)

cout << arr[a][b] << " ";

cout << endl;


}

return 0;

Output—

Program-20//write a program to print and calculate the factorial of a number


using function--->

#include <iostream>

using namespace std;

int factorial(int);

int main(){

int n;

cout<<"Enter any number "<<endl;

cin>>n;

cout<<"The number is "<<n<<endl<<"factorial is "<<factorial(n);

int factorial(int n)

{
if (n>1)

return n* factorial(n-1);

else

return 1;

Output—

Program-21//Write a program to check whether a number is prime or not using


function--->

#include <iostream>

using namespace std;

bool check_prime(int);

int main(){

int p;

cout<<"Enter the number"<<endl;

cin>>p;
if (check_prime(p))

cout<<"The number is prime"<<endl;

else

cout<<"The number is not prime"<<endl;

bool check_prime(int p){

bool is_prime=true;

if(p==0 || p==1){

is_prime=false;

for( int i=2.; i<=p/2; i++){

if (p%i==0){

is_prime=false;

break;

return is_prime;

Output—
Program-22//Write a program to display multiple statements using fuctions

#include <iostream>

using namespace std;

void message_1() {

cout << "Hello" << endl;

void message_2() {

cout << "Namaste" << endl;

void message_3() {

cout << "Good Morning" << endl;

int main() {

message_1();

message_2();

message_3();
return 0;

Output—

Program-23//write a program to print four cities greeting statements using


function-->>

#include <iostream>

using namespace std;

void dehli() {

cout << "Welcome to DEHLI" << endl;

void kolkata() {

cout << "Welcome to KOLKATA" << endl;

void chennai() {

cout << "Welcome to CHENNAI" << endl;

void Agra() {

cout << "Welcome to AGRA" << endl;


}

int main() {

dehli();

kolkata();

chennai();

Agra();

return 0;

Output—

Program-24//Write a program to c++ to implement a basic calculator using


functions with the following functions ADD , SUBTRACT, MULTIPLY, DIVIDE-->>

#include<iostream>

using namespace std;


int sum(int,int);

int sub(int,int);

int mul(int,int);

float result=0;

float di(int a, int b)

result=a/b;

return(result);

int main()

float a,b,m,su,s,d;

cout<<"Enter 1st number ";

cin>>a;

cout<<"Enter 2nd number ";

cin>>b;

s=sum(a,b);

su=sub(a,b);
m=mul(a,b);

d=di(a,b);

cout<<"\nSOLUTION IS -:"<<endl;

cout<<"\nAddition of two Numbers = "<<s<<endl;

cout<<"Subtraction of two Numbers = "<<su<<endl;

cout<<"Multiplication of two Numbers= "<<m<<endl;

cout<<"Division of two Numbers= "<<d<<endl;

return 0;

sum(int a,int b)

result=a+b;

return(result);

sub(int a,int b)

result=a-b;

return(result);

}
mul(int a,int b)

result=a*b;

return(result);

Output—

Program-25//Write a program to print fabonacii series-->>

#include <iostream>

using namespace std;

int main() {

int n, t1 = 0, t2 = 1, nextTerm = 0;

cout << "Enter the number of terms: ";

cin >> n;
cout << "Fibonacci Series: ";

for (int i = 1; i <= n; ++i) {

// Prints the first two terms.

if(i == 1) {

cout << t1 << ", ";

continue;

if(i == 2) {

cout << t2 << ", ";

continue;

nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;

cout << nextTerm << ", ";

return 0;

Output—
Program-26//Write a program in c++to implement a class rectangle-- >>

#include <iostream>

using namespace std;

class rectangle{

private :

int l,b;

public :

void input(int lenth, int breath){

l=lenth;

b=breath;

int area(){

return l*b;

}
};

int main(){

rectangle r1;

r1.input(20,5);

cout<<"Area of rectangle is "<<r1.area()<<endl;

Output—

Program-27//write a program in c++ to make a claculator using classes and


object-- >>

#include <iostream>

using namespace std;

class calculator{

int x ,y,z,su,mu,di;

public:

void inputdata(){

cout<<"Enter the numbers"<<endl;

cin>>x>>y;
}

void sum(){

z=x+y;

void sub(){

su=x-y;

void mul(){

mu=x*y;

void div(){

di=x/y;

void outputsum(){

cout<<"Sum of two numbers is "<<z<<endl;

void outputsub(){

cout<<"difference of two numbers is "<<su<<endl;

void outputmul(){

cout<<"Product of two numbers is "<<mu<<endl;

void outputdiv(){
cout<<"Division of two numbers is "<<di<<endl;

};

int main(){

calculator cal;

cal.inputdata();

cal.sum();

cal.sub();

cal.mul();

cal.div();

cal.outputsum();

cal.outputsub()

cal.outputmul();

cal.outputdiv()

Output—

Program-28//Write a program to show the data of the employee using class


employee-->>
#include <iostream>

using namespace std;

class employee

int id;

char na[30];

int sr;

public:

void getData();

void showData();

};

void employee :: getData()

cout<<"Enter employee id: ";

cin>>id;

cout<<"Enter employee name: ";

cin>>na;

cout<<"Enter employee salary: ";

cin>>sr;

void employee :: showData()

cout<<"<<-------------------------------------------------------->>"<<endl;
cout<<" Employee ID:"<<" "<<id<<" "<<endl;

cout<<" Name: "<<" "<<na<<" "<<endl;

cout<<" Salary: "<<" "<<sr<<" "<<endl;

cout<<"<<--------------------------------------------------------->> "<<endl;

int main()

employee e1;

e1.getData();

e1.showData();

return 0;

Output—

Program-29//Write a program in c++ to implement class box ..data members


length, bredth, height..member fuctions
#include <iostream>

using namespace std;

class box{

int l , b, h, sa,vol;

public:

void inputdata(){

cout<<"Enter the dimensions "<<endl;

cin>>l>>b>>h;

void surface(){

sa=2*l*b + 2*b*h +2*h*l;

void volume(){

vol=l*b*h;

void outputsur(){

cout<<"Surface Area of cuboid is "<<sa<<endl;

void outputvo(){
cout<<"Volume of cuboid is "<<vol<<endl;

};

int main(){

box savo;

savo.inputdata();

savo.surface();

savo.outputsur();

savo.volume();

savo.outputvo();

Output—

Program-30////Write a program to implement id , name and salary of a employee


with class employee using constructors
// Default constructor------ >

#include <iostream>

using namespace std;

class employee{

public:

int id;

string name;

float salary;

employee(){

id = 105;

name = "Aman";

salary = 200000;

void showdata(){

cout<<id<<" "<<name<<" "<<salary<<endl;

};

int main(){

employee e;

e.showdata();
return 0;

// Parametrized constructor------ >>

#include <iostream>

using namespace std;

class employee{

int id;

string name;

float salary;

public:

employee(int i, string n, float s){

i=id;

name=n;

salary=s;

void showdata(){

cout<<id<<" "<<name<<" "<<salary<<endl;

}
};

int main(){

employee e(105, "Aman",2000);

e.showdata();

return 0;

Output—

Program-31 //Write a program to make a calculator using constructor

#include <iostream>

using namespace std;

class calculator{

public:

int a,b,sum,sub,mul,div;

calculator(){

a=6;

b=2;

void showsum(){

cout<<"sum is "<<a+b<<endl;

}
void showsub(){

cout<<"difference is "<<a-b<<endl;

void showmul(){

cout<<"product is "<<a*b<<endl;

void showdiv(){

cout<<"ivision is "<<a/b<<endl;

};

int main(){

calculator c;

c.showsum();

c.showsub();

c.showmul();

c.showdiv();

Output—

//Parametrized constructor
#include <iostream>

using namespace std;

class calculator{

int a, b;

public:

calculator(int x, int y){

a=x;

b=y;

void showsum(){

cout<<"sum of two numbers are "<<a+b<<endl;

void showsub(){

cout<<"Differnce of two numbers are "<<a-b<<endl;

void showmul(){

cout<<"Poduct of two numbers are "<<a*b<<endl;

void showdiv(){

cout<<"division of two numbers are "<<a/b<<endl;

};
int main(){

calculator c(8,2);

c.showsum();

c.showsub();

c.showmul();

c.showdiv();

Output—

Program-33// //Write a program to implement addition of two weights using class


weight

#include <iostream>

using namespace std;

class weight{

public:

int kg1, kg2;

int g1, g2;

int sum;
weight(){

cout<<"Enter weight 1 "<<endl;

cin>>kg1>>g1;

cout<<"Enter weight 2 "<<endl;

cin>>kg2>>g2;

void showdata(){

cout<<"the sum of two weights is "<<kg1+kg2<<" kilograms "<<"and


"<<g1+g2<<" grams"<<endl;

};

int main(){

weight w;

w.showdata();

return 0;

Output—
Program-34//Write a program to find the product of two numbers using single
inheritence

#include <iostream>

using namespace std;

class base

public:

int x;

void getdata()

cout << "Enter the value of x = "; cin >> x;

};

class derive : public base

private:

int y;

public:

void readdata()

cout << "Enter the value of y = "; cin >> y;

void product()
{

cout << "Product = " << x * y;

};

int main()

derive a;

a.getdata();

a.readdata();

a.product();

return 0;

Output—

Program-35//W rite a program using multiple inheritence to find the sum of two
numbers

#include <iostream>

using namespace std;


class A

public:

int x;

void getx()

cout << "enter value of x: ;

cin >> x;

};

class B

public:

int y;

void gety()

cout << "enter value of y: ;

cin >> y;

};

class C : public A, public B

public:
void sum()

cout << "Sum = " << x + y;

};

int main()

C obj1; //object of derived class C

obj1.getx();

obj1.gety();

obj1.sum();

return 0;

Output—

Program-36//Write a program to print the product of three numbers using


multilevel inheritence

#include <iostream>

using namespace std;

class base

{
public:

int x;

void getdata()

cout << "Enter value of x= ";

cin >> x;

};

class derive1 : public base

public:

int y;

void readdata()

cout << "\nEnter value of y= "; cin >> y;

};

class derive2 : public derive1

private:

int z;

public:

void indata()
{

cout << "\nEnter value of z= "; cin >> z;

void product()

cout << "\nProduct= " << x * y * z;

};

int main(){

derive2 a;

a.getdata();

a.readdata();

a.indata();

a.product();

return 0;

Output—

Program-37//Write a program to add and subtract two complex numbers using


class complex
#include <iostream>

using namespace std;

class Complex {

private:

double real;

double imaginary;

public:

// Constructor

Complex(double real = 0.0, double imaginary = 0.0) {

this->real = real;

this->imaginary = imaginary;

double getReal() const {

return real;

double getImaginary() const {

return imaginary;

void setReal(double real) {


this->real = real;

void setImaginary(double imaginary) {

this->imaginary = imaginary;

// Addition of two complex numbers

Complex add(const Complex& other) const {

double newReal = real + other.real;

double newImaginary = imaginary + other.imaginary;

return Complex(newReal, newImaginary);

// Subtraction of two complex numbers

Complex subtract(const Complex& other) const {

double newReal = real - other.real;

double newImaginary = imaginary - other.imaginary;

return Complex(newReal, newImaginary);

void display() const {

cout << real << " + " << imaginary << "i" << std::endl;

}
};

int main() {

// Create complex numbers

Complex c1(2.0, 3.0);

Complex c2(4.0, 1.5);

// Display the complex numbers

cout << "Complex Number 1: ";

c1.display();

cout << "Complex Number 2: ";

c2.display();

// Add two complex numbers

Complex sum = c1.add(c2);

cout << "Sum: ";

sum.display();

// Subtract two complex numbers

Complex diff = c1.subtract(c2);

cout << "Difference: ";

diff.display();
return 0;

Output—

Program-38//Write a program to add two distance using class distance

#include <iostream>

using namespace std;

class Distance {

private:

int feet;

float inches;

public:

Distance(int ft = 0, float in = 0.0) {

feet = ft;
inches = in;

void setFeet(int ft) {

feet = ft;

void setInches(float in) {

inches = in;

int getFeet() const {

return feet;

float getInches() const {

return inches;

Distance operator+(const Distance& other) const {

int totalFeet = feet + other.feet;

float totalInches = inches + other.inches;

if (totalInches >= 12.0) {

totalInches -= 12.0;

totalFeet++;

return Distance(totalFeet, totalInches);


}

void display() const {

cout << "Feet: " << feet << " Inches: " << inches << endl;

};

int main() {

Distance d1(5, 10.5);

Distance d2(3, 7.2);

Distance sum = d1 + d2;

cout << "d1: ";

d1.display();

cout << "d2: ";

d2.display();

cout << "Sum: ";

sum.display();

return 0;

Output—
Program-39//Write a program to print the sum of series using function
overloading

#include <iostream>

using namespace std;

void SumNum(int A, int B);

void SumNum(int A, int B, int C);

void SumNum(int A, int B, int C, int D);

int main()

SumNum(1,2);

SumNum(1,2,3);

SumNum(1,2,3,4);

return 0;

void SumNum(int A, int B)

cout<< endl << "SUMNUM is : "<< A+B;


}

void SumNum(int A, int B, int C)

cout<< endl << "SUMNUM is : "<< A+B+C;

void SumNum(int A, int B, int C, int D)

cout<< endl << "SUMNUM is : "<< A+B+C+D;

Output—

Program-40////Write a program to print the sum of two and three digits using
function overloading

#include <iostream>

using namespace std;

class Addition

public:
int sum(int a,int b)

return a+b;

int sum(int a,int b, int c)

return a+b+c;

};

int main(void)

Addition obj;

cout<<obj.sum(20, 15)<<endl;

cout<<obj.sum(81, 100, 10);

return 0;

Output—

Program-41//Write a program to display the count using operator overloading


#include <iostream>

using namespace std;

class Count {

private:

int value;

public:

Count() : value(5) {}

void operator ++ () {

++value;

void operator ++ (int) {

value++;

void display() {

cout << "Count: " << value << endl;

};

int main() {

Count count1;
count1++;

count1.display();

++count1;

count1.display();

return 0;

Output—

Program-42//Write a program to overload binary operator or add two complex


numbers using operator overloading

#include <iostream>

using namespace std;

class Complex {

private:

float real;

float imag;

public:
// Constructor to initialize real and imag to 0

Complex() : real(0), imag(0) {}

void input() {

cout << "Enter real and imaginary parts respectively: ";

cin >> real;

cin >> imag;

Complex operator + (const Complex& obj) {

Complex temp;

temp.real = real + obj.real;

temp.imag = imag + obj.imag;

return temp;

void output() {

if (imag < 0)

cout << "Output Complex number: " << real << imag << "i";

else

cout << "Output Complex number: " << real << "+" << imag << "i";

};
int main() {

Complex complex1, complex2, result;

cout << "Enter first complex number:\n";

complex1.input();

cout << "Enter second complex number:\n";

complex2.input();

result = complex1 + complex2;

result.output();

return 0;

Output—

You might also like