Understanding Polymorphism in Object-Oriented Programming

eee0116 chapter 24 object oriented programming n.w
1 / 17
Embed
Share

Explore the concept of polymorphism in object-oriented programming, enabling different objects in the same class hierarchy to be treated as objects of the base class. Learn how to invoke base-class functions from derived-class objects and the importance of virtual functions. Discover the relationships among objects in an inheritance hierarchy and how polymorphism allows for flexible and scalable programming.

  • Polymorphism
  • OOP
  • Inheritance
  • Virtual Functions
  • Object Hierarchy

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. EEE0116 Chapter 24: Object- Oriented Programming: Polymorphism C How to Program Deitel & Deitel 1

  2. 2 Outline Introduction Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class Functions from Derived-Class Objects Virtual Functions Abstract Classes and Pure virtual Functions

  3. 3 Introduction Polymorphism process objects of classes that are part of the same hierarchy as if they are all objects of the base class. Different actions for different objects New classes can be embedded without important changes

  4. 4 Introduction Example: Animal Hierarchy Base class is Animal class. Each drived class has its move function. Different animal objects can be accessed using base class Animal pointers. When move function is called, each animal object gets its own move generically. Polymorphism takes place when a program calls avirtual function using base class pointers or references.

  5. 5 Relationships Among Objects in an Inheritance Hierarchy Polymorphism key issue: An object of derived class con be considered as an object of its base class Invoke base-class functions using derived class objects Using derived-class pointers on base-class objects Using base-class pointers to claa derived-class member functions Polymorphism using virtual functions: Base-class pointers aimed at derived-class objects

  6. 6 Invoking Base-Class Functions from Derived-Class Objects Use base-class pointer on base-class object Base-class functionality Use derived-class pointer on derived-class object Derived-class functionality Use base-class pointer on derived-class object Invokes base-class functionality However using virtual functions makes it possible to invoke derived-class functionality (Polymorphism)

  7. 7 Virtual Functions Handle (pointer or reference) determines which class s (base or derived) functions to call. With virtual functions, not handle, type of the object determines which class s function to be called. Dynamic binding

  8. 8 Virtual Functions Keyword virtual is used to declare a function in base class as virtual. Derived classes can overwrite virtual functions. Once a function is declared as virtual, it remains virtual in hierarchy. Static binding Dynamic binding

  9. 9 Abstract Classes and Pure virtual Functions Abstract classes You cannot create an object from abstract class. They are incomplete and too generic to define objects They are usually base classes and therefore are called abstract base classes. Classes esed to create objects are considered as concrete classes.

  10. 10 Abstract Classes and Pure virtual Functions Pure virtual functions A class is made abstract vy defining one or more of its virtual functions as pure. Example: virtual void print() const = 0; = 0 is called as pure specifier. Pure virtual functions do not have any implementations. If the derived class do not overwrite the pure virtual function, it will also be abstract.

  11. 11 Abstract Classes and Pure virtual Functions 1 // Fig. // Fig. 24 2 // Employee abstract base class. // Employee abstract base class. 3 #ifndef #ifndef EMPLOYEE_H EMPLOYEE_H 4 #define #define EMPLOYEE_H EMPLOYEE_H 5 6 #include #include <string> <string> // C++ standard string class // C++ standard string class 7 using using std::string; std::string; 8 9 class class Employee Employee 10 { { 11 public public: : 12 Employee( Employee( co const nst string &, string &, const 13 14 void void setFirstName( setFirstName( const const string & ); 15 string getFirstName() string getFirstName() const 16 17 void void setLastName( setLastName( const const string & ); 18 string string getLastName() getLastName() const 19 20 void void setSocialSecurityNumber( setSocialSecurityNumber( const 21 string getSocialSecurityNumber() string getSocialSecurityNumber() const 24.13: Employee.h .13: Employee.h const string &, string &, const const string & ); string & ); string & ); // set first name // set first name const; ; // return first name // return first name string & ); // set last name // set last name const; ; // return last name // return last name const string & ); string & ); // set SSN // set SSN const; ; // return SSN // return SSN

  12. 12 Abstract Classes and Pure virtual Functions 22 23 // pure virtual function makes Employee abstract base class // pure virtual function makes Employee abstract base class 24 virtual virtual double double earnings() earnings() const const = 25 virtual virtual void void print() print() const const; ; // virtual 26 private private: : 27 string firstName; string firstName; 28 string lastName; string lastName; 29 string socialSecurityNumber; string socialSecurityNumber; 30 }; }; // end class Employee // end class Employee 31 32 #endif #endif // EMPLOYEE_H // EMPLOYEE_H ; // pure virtual // pure virtual // virtual = 0 0;

  13. 13 Abstract Classes and Pure virtual Functions 1 // Fig. // Fig. 24 2 // Abstract // Abstract- -base 3 // Note: No definitions are given for pure virtual functions. // Note: No definitions are given for pure virtual functions. 4 #include #include <iostream> <iostream> 5 using std::cout; using std::cout; 6 7 #include #include "Employee.h" "Employee.h" // Employee class // Employee class definition 8 9 // constructor // constructor 10 Employee::Employee( Employee::Employee( const const string &first, string &first, const 11 const const string &ssn ) string &ssn ) 12 : firstName( first ), lastName( last ), socialSecurityNumber( ssn ) : firstName( first ), lastName( last ), socialSecurityNumber( ssn ) 13 { { 14 // empty body // empty body 15 } } // end Employe // end Employee constructor e constructor 16 17 // set first name // set first name 18 void void Employee::setFirstName( Employee::setFirstName( const const string &first ) string &first ) 19 { { 20 firstName = first; firstName = first; 21 } } // end function setFirstName // end function setFirstName 22 23 // return first name // return first name 24 string Employee::getFirstName() string Employee::getFirstName() const const 25 { { 26 return return firstName; firstName; 27 } } // end function getFirstName // end function getFirstName 28 24.14: Employee.cpp .14: Employee.cpp base- -class Employee member class Employee member- -function definitions. function definitions. definition const string &last, string &last,

  14. 14 Abstract Classes and Pure virtual Functions 29 // set last name // set last name 30 void void Employee::setLastName( Employee::setLastName( const 31 { { 32 lastName = last; lastName = last; 33 } } // end function setLastName // end function setLastName 34 35 // return last name // return last name 36 string Employee::getLastName() string Employee::getLastName() const 37 { { 38 return return lastName; lastName; 39 } } // end function getLastName // end function getLastName 40 41 // set social security number // set social security number 42 void void Employee::setSocialSecurityNumber( Employee::setSocialSecurityNumber( const 43 { { 44 socialSecurityNumber = ssn; socialSecurityNumber = ssn; // should validate 45 } } // end function setSocialSecurityNumber // end function setSocialSecurityNumber 46 47 // return social security number // return social security number 48 string Employee::getSocialSecurityNumber() string Employee::getSocialSecurityNumber() const 49 { { 50 return return socialSecurityNumber; socialSecurityNumber; 51 } } // end function getSocialSecurityNumber // end function getSocialSecurityNumber 52 53 // print Employee's information (virtual, but not pure virtual // print Employee's information (virtual, but not pure virtual) ) 54 void void Employee::print() Employee::print() const const 55 { { 56 cout << getFirstName() << cout << getFirstName() << ' ' ' ' << getLastName() 57 << << " "\ \nsocial security number: " nsocial security number: " << getSocialSecurityNumber(); 58 } } // end function print // end function print const string &last ) string &last ) const const string &ssn ) string &ssn ) // should validate const << getLastName() << getSocialSecurityNumber();

  15. 15 Abstract Classes and Pure virtual Functions 1 // Fig. // Fig. 24 2 // SalariedEmployee class derived from Employee. // SalariedEmployee class derived from Employee. 3 #ifndef #ifndef SALARIED_H SALARIED_H 4 #define #define SALARIED_H SALARIED_H 5 6 #include #include "Employee.h" "Employee.h" // Employee class definition // Employee class definition 7 8 class class SalariedEmployee : SalariedEmployee : public 9 { { 10 public public: : 11 SalariedEmployee( SalariedEmployee( const const string &, 12 const const string &, string &, double double = 13 14 void void setWeeklySalary( setWeeklySalary( double 15 double double getWeeklySalary() getWeeklySalary() const 16 17 // keyword virtual signals intent to override // keyword virtual signals intent to override 18 virtual virtual double double earnings() earnings() const const; 19 virtual virtual void void print() print() const const; ; // print SalariedEmployee object 20 private private: : 21 double double weeklySalary; weeklySalary; // salary per // salary per week 22 }; }; // end class SalariedEmployee // end class SalariedEmployee 23 24 #endif #endif // SALARIED_H // SALARIED_H 24.15: SalariedEmployee.h .15: SalariedEmployee.h public Employee Employee string &, const const string &, string &, = 0.0 0.0 ); ); double ); ); // set weekly salary // set weekly salary const; ; // return weekly salary // return weekly salary ; // calculate earnings // calculate earnings // print SalariedEmployee object week

  16. 16 Abstract Classes and Pure virtual Functions 1 // Fig. // Fig. 24 2 // SalariedEmployee class member // SalariedEmployee class member- -function definitions. 3 #include #include <iostream> <iostream> 4 using using std::cout; std::cout; 5 6 #include #include "SalariedEmployee.h" "SalariedEmployee.h" // SalariedEmployee class definition // SalariedEmployee class definition 7 8 // constructor // constructor 9 Salaried SalariedEmployee::SalariedEmployee( Employee::SalariedEmployee( const 10 const const string &last, string &last, const const string &ssn, string &ssn, double 11 : Employee( first, last, ssn ) : Employee( first, last, ssn ) 12 { { 13 setWeeklySalary( salary ); setWeeklySalary( salary ); 14 } } // end SalariedEmployee constructor // end SalariedEmployee constructor 15 16 // set // set salary salary 17 void void SalariedEmployee::setWeeklySalary( SalariedEmployee::setWeeklySalary( double 18 { { 19 weeklySalary = ( salary < weeklySalary = ( salary < 0.0 0.0 ) ? ) ? 0.0 20 } } // end function setWeeklySalary // end function setWeeklySalary 21 22 // return salary // return salary 23 double double SalariedEmployee::getWeeklySalary() SalariedEmployee::getWeeklySalary() const 24 { { 25 return return weeklySalary; weeklySalary; 26 } } // end function getWeeklySalary // end function getWeeklySalary 24.16: SalariedEmployee.cpp .16: SalariedEmployee.cpp function definitions. const string &first, string &first, double salary ) salary ) double salary ) salary ) 0.0 : salary; : salary; const

  17. 17 Abstract Classes and Pure virtual Functions 27 28 // calculate earnings; // calculate earnings; 29 // override pure virtual function earnings in Employee // override pure virtual function earnings in Employee 30 double double SalariedEmployee::earnings() SalariedEmployee::earnings() const 31 { { 32 return return getWeeklySalary(); getWeeklySalary(); 33 } } // end function earnings // end function earnings 34 35 // print SalariedEmployee's // print SalariedEmployee's information 36 void void SalariedEmployee::print() SalariedEmployee::print() const 37 { { 38 cout << cout << "salaried employee: " "salaried employee: "; ; 39 Employee::print(); Employee::print(); // reuse abstract base // reuse abstract base- -class print function 40 cout << cout << " "\ \nweekly salary: " nweekly salary: " << getWeeklySalary(); << getWeeklySalary(); 41 } } // end function pri // end function print nt const information const class print function

Related


More Related Content