0% found this document useful (0 votes)
25 views5 pages

20011598-136.. Quiz 02 Oop

Uploaded by

Muhammad Bilal
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)
25 views5 pages

20011598-136.. Quiz 02 Oop

Uploaded by

Muhammad Bilal
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/ 5

Quiz OOP

MUHAMMAD BILAL 2001158-


136

BSSE SEC-2C

Instructor: Miss Qamar un nisa

Question
Write a program by concluding and elaborating the concept of
operator overloading, you have studied.

Code
#include<iostream>

#include<stdio.h> using

namespace std; class

complex

{ int real;

float image;

public:

void getdata()

cout<<"\n Enter the real part of the complex :: ";


cin>>real; cout<<"\n Enter the imaginary part

of the complex :: "; cin>>image;

void operator + (complex);

void operator - (complex);

};

void complex :: operator + (complex c1)

complex temp;

temp.real=real+c1.real;

temp.image=image+c1.image; if

(temp.image>=0)

cout<<"\n complex no. after addition :: ";

cout<<temp.real<<"+"<<temp.image<<"i\n";

else

cout<<"\n complex no. after addition :: ";

cout<<temp.real<<temp.image<<"i\n";
}

void complex ::operator-(complex c1)

complex temp; temp.real

= real-c1.image; temp.image=

image-c1.image; if

(temp.image>=0)

cout<<"\n complex no. after subtraction :: ";

cout<<"\n temp.real"<<"+"<<temp.image<<"i\n";

else

cout<<"\n complex no. after subtraction :: ";

cout<<temp.real<<temp.image<<"i\n";

int main()

{
complex c1, c2;

int n;

do

cout<<"\n 1. Input data for complex no. ";

cout<<"\n 2. Addition of complex no. ";

cout<<"\n 3. Subtraction of complex no. ";

cout<<"\n 4. Quit"; cout<<"\n Enter your

choice :: ";

cin>>n;

switch(n)

case 1:

cout<<endl<<"\n Enter the data for First Complex Number";

c1.getdata();

cout<<endl<<"\n Enter the data for seconds Complex Number";

c2.getdata();

break;
case

2:

c1+c2;

break;

case

3: c1-

c2;

break;

case

4:

exit(1);

break;

} while (n!=4);

return 0;

You might also like