0% found this document useful (0 votes)
20 views

Assignment #3 PDF Solved

The document defines a C++ class named "number" that overloads operators for two integer members to allow arithmetic and comparison operations on "number" objects. The class defines unary operators like increment/decrement, binary arithmetic operators like addition/subtraction, and relational operators like greater-than. The main() function demonstrates creating "number" objects and using the overloaded operators to perform operations like addition, comparison, input/output.

Uploaded by

Muhammad Faizan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Assignment #3 PDF Solved

The document defines a C++ class named "number" that overloads operators for two integer members to allow arithmetic and comparison operations on "number" objects. The class defines unary operators like increment/decrement, binary arithmetic operators like addition/subtraction, and relational operators like greater-than. The main() function demonstrates creating "number" objects and using the overloaded operators to perform operations like addition, comparison, input/output.

Uploaded by

Muhammad Faizan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<iostream>

#include<string.h>
#include<math.h>
using namespace std;
class number{
int m1,m2;
public:
number(int x=0, int y=0):m1(x),m2(y){
}
void show(){
cout<<"m1="<<m1<<endl;
cout<<"m2="<<m2<<endl;
}
////uniary operator/////
number operator ++(){ //prefixe increament
return number(++m1,++m2);

}
number operator ++(int){ // postfixe increament
return number(m1++,m2++);
}
number operator --(){ //prefixe decreament
return number (--m1,--m2);
}
number operator --(int){ //postfixe decreament
return number (m1--,m2--);
}
////binary operator////

number operator +(number & n1){ //sum of two int


return number (m1+n1.m1,m2+n1.m2);

}
number operator -(number & n1){ //subtraction of two int
return number (m1-n1.m1,m2-n1.m2);
}
number operator *(number & n1){ //multiplication of two int
return number (m1*n1.m1,m2*n1.m2);
}
number operator /(number & n1){ //division of two int
return number (m1/n1.m1,m2/n1.m2);
}
number operator %(number & n1){ //modulus of two int
return number (m1%n1.m1,m2%n1.m2);
}
////relational operator////
bool operator >(number n1){ ////we can not use nameless temporary object in relation operator///
if(m1>n1.m1,m1>n1.m2)
return true;
else
return false;

}
bool operator < (number n1){ ////we can not use nameless temporary object in relation operator///
if(m1<n1.m1,m1<n1.m2)
return true;
else
return false;

}
/// compound assignment operator///
bool operator >= (number n1){ ////we can not use nameless temporary object in compound assignm
ent operator///
if(m1>=n1.m1,m1>=n1.m2)
return true;
else
return false;

}
bool operator <= (number n1){ ////we can not use nameless temporary object in compound assignm
ent operator///
if(m1<=n1.m1,m1<=n1.m2)
return true;
else
return false;

}
friend istream & operator>>(istream & in, number & n1);
friend ostream & operator<<(ostream & out,number & n1 );

};
istream& operator>>(istream &in,number &n1){
cout<<"enter values of m1 and m2"<<endl;
in>>n1.m1>>n1.m2;
return in;
}
ostream &operator<<(ostream & out, number & n1){
out<<" value of m1 "<<n1.m1<<" and "<<" value of m2 "<<n1.m2<<endl;
}
int main(){
number obj1(5,10),obj2(15,20),obj3;
++obj1;
obj1.show();
obj2++;
obj2.show();
--obj1;
obj1.show();
obj2--;
obj2.show();
obj3=obj1+obj2;
obj3.show();
obj3=obj1-obj2;
obj3.show();
obj3=obj1*obj2;
obj3.show();
obj3=obj1/obj2;
obj3.show();
obj3=obj1%obj2;
obj3.show();
if(obj1>obj2)
cout<<"obj1 is greter"<<endl;
else
cout<<"obj2 is greater"<<endl;
if(obj1<obj2)
cout<<"obj1 is smaller"<<endl;
else
cout<<"obj2 is smaller"<<endl;
if(obj1>=obj2)
cout<<"obj1 is greater or equal to obj2"<<endl;
else
cout<<"obj1 is smaller or equall to obj1"<<endl;
if(obj1<=obj2)
cout<<"obj1 is smaller or equal to obj2"<<endl;
else
cout<<"obj1 is greater or equall to obj1"<<endl;
//we can input values of any object//
cin>>obj1;
cout<<obj1;

You might also like