🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Object Oriented Programming, Lecture 08.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Document Details

ImprovedEnglishHorn

Uploaded by ImprovedEnglishHorn

Zagazig University

2023

Tags

object-oriented programming C++ inheritance programming concepts

Full Transcript

Object Oriented Programming Ch.9: Operator Overloading and Inheritance Prepared By Dr. Ibrahim Attiya © 2023 Zagazig University Ch.9: Outline ❑ Operator Overloading ❑ Invoking an Overloaded Operator ❑ What Is Inheritance? ❑ Protected Members and Cla...

Object Oriented Programming Ch.9: Operator Overloading and Inheritance Prepared By Dr. Ibrahim Attiya © 2023 Zagazig University Ch.9: Outline ❑ Operator Overloading ❑ Invoking an Overloaded Operator ❑ What Is Inheritance? ❑ Protected Members and Class Access ❑ Inheritance vs. Access ❑ Multiple Inheritance Operator Overloading Operator Overloading C++ allows you to redefine how standard operators work when used with class objects. The term operator overloading refers to giving the normal C++ operators, such as +, *, =, +=, ++ and −−, additional meanings when they are applied to user-defined data types. Normally a = b + c; works only with basic types such as int and float, and attempting to apply it when a, b, and c are objects of a user-defined class will cause complaints from the compiler. Using overloading, you can make this statement legal even when a, b, and c are user-defined types. Overloading the = Operator Operator function prototyping: void operator=(const SomeClass &rval) return function parameter for type name object on right side of operator Operator is called via object on left side. In the above statement, the parameter named rval illustrates that it references the object on the right side of the operator. Invoking an Overloaded Operator C++ allows operator functions to be called with regular function call notation, or by using the operator symbol. Operator can be invoked as a member function: object1.operator=(object2); It can also be used in more conventional manner: object1 = object2; Example: // Call operator= function student2 = student1; student2.operator=(student1); Program Ⅰ Returning a Value Overloaded operator can return a value. class Point2d { public: double operator-(const point2d &right) { return sqrt(pow((x-right.x),2) + pow((y-right.y),2)); }... private: int x, y; }; Point2d point1(2,2), point2(4,4); // Compute and display distance between 2 points. cout += − = * = / = % = ^ = & = |= > >> = = && || ++ −− ->* , -> [] () new delete Cannot overload the following operators: ?:..* :: sizeof Overloading Types of Operators Unary operators, such as ++ and --, are overloaded differently for prefix vs. postfix notation. In addition to the assignment and math operators, relational operators may be overloaded. The only difference is that a relational operator function should always return a bool value (i.e., a true or false). What Is Inheritance? What Is Inheritance? Provides a way to create a new class from an existing class. The new class inherits all the member variables and functions (except the constructors and destructor) of the class it is based on. Inheritance involves a base class and a derived class. ▪ The base class is the general class and the derived class is the specialized class. ▪ The derived class is based on, or derived from, the base class. The "is a" Relationship Inheritance establishes an "is a" relationship between classes. – A car is a vehicle – A grasshopper is an insect. – A flower is a plant – A football player is an athlete Inheritance – Terminology and Notation The derived class inherits the member variables and member functions of the base class without any of them being rewritten. Notation: class Student // base class {... }; class Undergraduate : public Student { // derived class... }; Back to the ‘is a’ Relationship An object of a derived class 'is a(n)' object of the base class. Example: – an Undergraduate is a Student – a Grasshopper is an Insect A derived object has all of the characteristics of the base class. Hence, the members of the base class object become members of the derived class object. What Does a Child Class Have? An object of the derived class has: ▪ all members defined in child class ▪ all members declared in parent class An object of the derived class can use: ▪ all public members defined in child class ▪ all public members defined in parent class Finally, inheritance does not work in reverse. It is not possible for a base class to call a member function of a derived class. Program Ⅱ Protected Members and Class Access Protected Members and Class Access C++ provides a third access specification, protected. Protected members of a base class are like private members, but they may be accessed by derived classes. Class access specification: determines how private, protected, and public members of base class are inherited by the derived class. Class Access Specifiers 1) public – object of derived class can be treated as object of base class (not vice-versa). 2) protected – more restrictive than public, but allows derived classes to know details of parents. 3) private – prevents objects of derived class from being treated as objects of base class. Inheritance vs. Access How inherited base class members Base class members appear in derived class private: x private x is inaccessible protected: y base class private: y public: z private: z private: x protected x is inaccessible protected: y base class protected: y public: z protected: z private: x public x is inaccessible protected: y base class protected: y public: z public: z More Inheritance vs. Access class Grade class Test : public Grade private members: private members: char letter; int numQuestions; float score; float pointsEach; void calcGrade(); int numMissed; public members: public members: void setScore(float); Test(int, int); float getScore(); char getLetter(); private members: int numQuestions: When Test class inherits float pointsEach; from Grade class using int numMissed; public class access, it public members: Test(int, int); looks like this: void setScore(float); float getScore(); float getLetter(); More Inheritance vs. Access (2) class Grade class Test : protected Grade private members: private members: char letter; int numQuestions; float score; float pointsEach; void calcGrade(); int numMissed; public members: public members: void setScore(float); Test(int, int); float getScore(); char getLetter(); private members: int numQuestions: When Test class inherits float pointsEach; from Grade class using int numMissed; protected class access, it public members: Test(int, int); looks like this: protected members: void setScore(float); float getScore(); float getLetter(); More Inheritance vs. Access (3) class Grade class Test : private Grade private members: private members: char letter; int numQuestions; float score; float pointsEach; void calcGrade(); int numMissed; public members: public members: void setScore(float); Test(int, int); float getScore(); char getLetter(); private members: int numQuestions: When Test class inherits float pointsEach; from Grade class using int numMissed; private class access, it void setScore(float); float getScore(); looks like this: char getLetter(); public members: Test(int, int); Inheritance vs. Access Multiple Inheritance Multiple Inheritance Multiple inheritance is when a derived class has two or more base classes. A derived class can have more than one base class. Each base class can have its own access specification in derived class's definition: class cube : public square, public rectSolid; class class square rectSolid class cube Multiple Inheritance The syntax for multiple inheritance is similar to that for single inheritance. class A // base class A { }; class B // base class B { }; // C is derived from A and B class C : public A, public B { }; Multiple Inheritance Arguments can be passed to both base classes' constructors: cube::cube(int side) : square(side), rectSolid(side, side, side); Base class constructors are called in order given in class declaration, not in order used in class constructor. DateTime::DateTime(int dy, int mon, int yr, int hr, int mt, int sc) : Date(dy, mon, yr), Time(hr, mt, sc); Multiple Inheritance Problem: what if base classes have member variables/functions with the same name? Solutions: ▪ Derived class redefines the multiply-defined function ▪ Derived class invokes member function in a particular base class using scope resolution operator :: Compiler errors occur if derived class uses base class function without one of these solutions. Any Questions?

Use Quizgecko on...
Browser
Browser