Professional Documents
Culture Documents
ASSIGNMENT NO.3
MAHNOOR TALIB
Bushra Bashir
Dr.BUSHRA BASHIR
40 MARKS
QUESTION # 1
How many arguments a binary operator overloading function takes, when it’s a member
function of the class?
Binary operator overloading function takes only one explicit argument when it is member
function of the class.
Complex operator + (Complex obj)
CODE:
#include <iostream>
class Complex
int real;
int img;
public:
Complex ()
real=0;
img=0;
real = r;
img = i;
Complex res;
return (res);
void print()
cout << real << " + " << img <<"i"<< endl;
};
int main ()
Complex c3;
c3 = c1 + c2;
cout<<"c1 = ";
c1.print();
cout<<"c2 = ";
c2.print();
cout<<"c3 = ";
c3.print();
system("pause");
return 0;
}
QUESTION # 2
How many arguments a binary operator overloading function takes, when it’s a non-
member function of the class?
All the arguments must be passed in binary operator overloading function i.e. all arguments must
be passed explicitly.
CODE:
#include <iostream>
using namespace std;
class Complex
{
int real;
int img;
public:
Complex ()
{
real=0;
img=0;
}
Complex (int r, int i)
{
real = r;
img = i;
}
Complex operator + (Complex obj)
{
Complex res;
res.real = real + obj.real;
res.img = img + obj.img;
return (res);
}
void print()
{
cout << real << " + " << img <<"i"<< endl;
}
friend Complex operator + (Complex &obj1 ,Complex &obj2)
{
Complex res;
res.real = obj1.real + obj2.real;
res.img = obj1.img + obj2.img;
return (res);
}
void print()
{
cout << real << " + " << img <<"i"<< endl;
}
};
int main ()
{
Complex c1 (10, 5);
Complex c2 (2, 4);
Complex c3;
c3 = c1 + c2;
cout<<"c1 = ";
c1.print();
cout<<"c2 = ";
c2.print();
cout<<"c3 = ";
c3.print();
system("pause");
return 0;
}QUESTION # 3
What happens to the number of arguments for a binary operator overloading friend
functions when they are independent function (not part of any class?)
Function must be preceded with a keyword friend. This function takes 2 arguments while
working and it will act like a function defined within a class.
CODE:
#include "stdafx.h"
#include <iostream>
class Complex
int real;
int img;
public:
Complex()
real=0;
img=0;
real = r;
img = i;
void print()
cout << real << " + i" << img << endl;
};
Complex temp;
return (temp);
int main ()
Complex C3;
C3 = C1 + C2;
C1.print();
C2.print();
C3.print();
system("pause");
return 0;
}
QUESTION # 4
How many arguments a unary operator overloading function takes, when it’s a pre-fix
unary operator?
No arguments a unary operator overloading function takes when it’s a prefix unary operator.
CODE:
#include "stdafx.h"
#include<iostream>
class prefix
private:
int num;
public:
prefix()
num=7;
void operator++()
++num;
void display()
{
cout<<" Counted number : "<<num<<endl;
};
int main()
prefix P1;
++P1;
P1.display();
system("pause");
return 0;
QUESTION # 5
How many arguments a unary operator overloading function takes, when it’s a post-fix
unary operator?
No argument a unary operator overloading function, But we write int when function is declared
in arguments so that program can identify between post-fix and pre-fix .
CODE:
#include "stdafx.h"
#include<iostream>
class prefix
private:
int num;
public:
prefix()
num=7;
void operator++(int )
num++;
void display()
};
int main()
prefix P1;
P1++;
P1.display();
system("pause");
return 0;