Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Pet {
- string name;
- int age;
- public:
- Pet(string nm ="Kitty",int ag =1)
- { name = nm;
- age = ag;
- }
- string getName()
- { return name;
- }
- int getAge()
- { return age;
- }
- // Instructions: You are required to complete the overloading code operator function
- Pet operator + (const Pet &); // Overloaded +
- Pet operator - (const Pet &); // Overloaded -
- Pet operator * (const Pet &); // Overloaded *
- bool operator > (const Pet &); // Overloaded >
- bool operator < (const Pet &); // Overloaded <
- bool operator == (constPet &); // Overloaded ==
- };
- // Main function for the program
- int main() {
- // declare instances of pets
- Pet pet1("Poppy",2),pet2("Boy",5), pet3;
- pet3 = pet1 + pet2; // pet1.operator+(pet2)
- cout << " Pet 1 name : " << pet1.getName() << endl;
- cout << " Pet 1 age : " << pet1.getAge() << endl;
- cout << " Pet 2 name : " << pet2.getName() << endl;
- cout << " Pet 2 age : " << pet2.getAge() << endl;
- cout << " Pet 3 name : " << pet3.getName() << endl;
- cout << " Pet 3 age : " << pet3.getAge() << endl;
- if( pet1 < pet2 ) { //pet1.operator<(pet2);
- cout << pet1.getName() << " is younger than " << pet2.getName();
- if( pet1 == pet2)
- cout << "The pets have same page"<< endl;
- else
- cout << "Thay are not same age" << endl;
- // Provide suitable if statement for operator >
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement