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. This is done using an overloaded implementation of
- + which is a member function of Height
- */
- #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 void input();
- inline void display();
- inline Height operator +(Height&);
- };
- 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);
- }
- void Height::input(){
- cout << "Meter = ";
- cin >> meter;
- cout << "Centi Meter = ";
- cin >> centi_meter;
- }
- void Height::display(){
- cout << meter << "m " << centi_meter << "cm" << endl;
- }
- int main() {
- system("cls");
- Height obj1, obj2;
- cout << "Enter the first height" << endl;
- obj1.input();
- cout << "Enter the second height" << endl;
- obj2.input();
- Height result = obj1 + obj2;
- cout << " ";
- obj1.display();
- cout << "+";
- obj2.display();
- cout << "=";
- result.display();
- cin.ignore();
- cin.get();
- return 0;
- }
Add Comment
Please, Sign In to add comment