test(); } Solution: parent Child"> test(); } Solution: parent Child">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exercises on Polymorphism and Operator Overloading TCP1201: 8.

Similar presentations


Presentation on theme: "Exercises on Polymorphism and Operator Overloading TCP1201: 8."— Presentation transcript:

1 Exercises on Polymorphism and Operator Overloading TCP1201: 8

2 Exercise 1 What is the output of the following program? Is the output desired? If not, what changes should be made? class Parent { public: void example(int a) { cout << "Parent \n"; } }; class Child:public Parent { public: void example(int a) { cout << "Child\n"; } }; int main(){ Parent *p = new Child(); p->example(3); } Solution: Output: Parent Not desirable, should be as the following: class Parent { public: virtual void example(int a) { cout << "Parent \n"; } }; class Child:public Parent { public: virtual void example(int a) { cout << "Child\n"; } }; int main(){ Parent *p = new Child(); p->example(3); }

3 Exercise 2 What is the output of the following program? class Parent { public: virtual void test() { cout << "Parent\n"; } }; class Child : public Parent { public: virtual void test() { cout << "Child\n"; } }; void Test(Child *c) { cout << “child \n"; } void Test(Parent *p) { cout << "parent \n"; } int main() { Parent *val = new Child(); Test(val); val->test(); } Solution: parent Child

4 Exercise 2 Can the following program compile? class Parent { public: void example(int a) { cout << "Parent \n"; } }; class Child:public Parent { public: void example(int a, int b) { cout << "Child \n"; } }; int main(){ Child c; c.example(3); } Solution: No. class Parent { public: void example(int a) { cout << "Parent \n"; } }; class Child:public Parent { public: void example(int a) { Parent:::example(a); } void example(int a, int b) { cout << "Child\n"; } }; int main(){ Child c; c.example(3); }

5 Exercise 3 The program below contains the class Product that stores the detail of a product and it will output the product’s data sorted by the price. In order for the program to produce the expected output specified in the Sample Run, and provide the operator overloading function for three operators: >>, = must be overloaded. The operator overloading function for >> is a friend function of the Product class.  class Product  {  friend istream& operator>>(istream&, Product&);  private:  int ID;  string name;  double price;  public:  int getID() { return ID; }  string getName() { return name; }  double getPrice() { return price; }  };

6 Exercise 3  int main( )  {  Product p1;  Product p2;   cin >> p1;  cin >> p2;  if (p1 >= p2) {  cout << p1;  cout << p2;  }  else {  cout << p2;  cout << p1;  }  return 0;  } 

7 Exercise 3  istream& operator>> (istream& is, Product& p) {  cout << "Enter <Product ID, name, and price :" << endl;  is >> p.ID >> p.name >> p.price;  return is;  }  ostream& operator<< (ostream& os, Product& p) {  os << p.getID() << "\t" << p.getName() << "\t" << p.getPrice() << endl;  return os;  }  bool operator>=(Product p1, Product p2) {  if (p1.getPrice() >= p2.getPrice())  return true;  else  return false;  }


Download ppt "Exercises on Polymorphism and Operator Overloading TCP1201: 8."

Similar presentations


Ads by Google