Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Height program has been edited to facilitate
- adding two objects of Height class using operator
- overloading. << and >> have also been overloaded.
- */
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- class Height{
- int meter, centi_meter;
- public:
- inline Height();
- inline Height(int ,int);
- inline Height(const Height&);
- inline ~Height();
- inline Height operator +(Height&);
- friend istream& operator >>(istream&, Height&);
- friend ostream& operator <<(ostream&, Height&);
- };
- istream& operator >>(istream &in, Height &obj){
- cout << "Meter = ";
- in >> obj.meter;
- cout << "Cetimeter = ";
- in >> obj.centi_meter;
- return in;
- }
- ostream& operator <<(ostream &out, Height &obj){
- out << obj.meter << "m " << obj.centi_meter << "cm" << endl;
- return out;
- }
- Height::Height(){
- meter = 0;
- centi_meter = 0;
- }
- Height::Height(int meter, int centi_meter){
- Height::meter = meter;
- Height::centi_meter = centi_meter;
- }
- Height::Height(const Height ©){
- meter = copy.meter;
- centi_meter = copy.centi_meter;
- }
- Height::~Height(){
- }
- Height Height::operator +(Height &height){
- int c_m = (centi_meter + height.centi_meter) % 100;
- int m = (meter + height.meter) + ((centi_meter + height.centi_meter) / 100);
- return Height(m, c_m);
- }
- int main() {
- system("cls");
- Height obj1, obj2;
- cout << "Enter the first height" << endl;
- cin >> obj1;
- cout << "Enter the second height" << endl;
- cin >> obj2;
- Height result = obj1 + obj2;
- cout << " ";
- cout << obj1;
- cout << "+";
- cout << obj2;
- cout << "=";
- cout << result;
- cin.ignore();
- cin.get();
- return 0;
- }
Add Comment
Please, Sign In to add comment