Comp 5421 TA Yanal Alahmad Email yanal tggmail

  • Slides: 17
Download presentation
Comp 5421 TA: Yanal Alahmad Email: yanal. tg@gmail. com Date: 12 June, 2018 Reference:

Comp 5421 TA: Yanal Alahmad Email: yanal. tg@gmail. com Date: 12 June, 2018 Reference: [1] C++ How to Program, 10/E Deitel & Deitel

Operator Overloading • Operator is overloaded by writing a non-static member function or non-member

Operator Overloading • Operator is overloaded by writing a non-static member function or non-member function • The function name ‘operator’ followed by the symbol for the operator being overloaded

Overloading Binary Operators as Member Functions class Test{ public: bool operator < (const String&)

Overloading Binary Operators as Member Functions class Test{ public: bool operator < (const String&) const; ……. }; y<z y. operator<(z)

Overloading Binary Operators as Non. Member Functions bool operator < (const String&, const String&)

Overloading Binary Operators as Non. Member Functions bool operator < (const String&, const String&) const; operator<(y, z)

Overloading Binary Stream Insertion and Extraction Operators • Should be overloaded as non-member functions,

Overloading Binary Stream Insertion and Extraction Operators • Should be overloaded as non-member functions, otherwise the awkward statements employee << cout employee >> cin !!! • Overloaded operator functions for binary operators can be member functions only when the left operand is an object of the class in which the function is overloaded • Run: Phone

Overloading Prefix Increment Operators • If its is defined as a member function Date&

Overloading Prefix Increment Operators • If its is defined as a member function Date& operator++( ); d 1. operator++( ) • If its is defined as a non-member function Date& operator++(Date& ); operator++(d 1);

Overloading Postfix Increment Operators • If its is defined as a member function Date&

Overloading Postfix Increment Operators • If its is defined as a member function Date& operator++( int); // dummy int for the compiler to distinguish d 1. operator++( 0) • If its is defined as a non-member function Date& operator++(Date&, int ); operator++(d 1, 0); • Run: Date

Inheritance • Allows a class to absorb an existing class’s capabilities, then customize or

Inheritance • Allows a class to absorb an existing class’s capabilities, then customize or enhance them • You can specify a class that inherits an existing class • The existing class is called base class • The new class is called the derived class • Other OO programming languages such as java refer to the base class as super class, and to the derived class as sub class

Is-a Relationship • It is a relationship represents inheritance • An object of the

Is-a Relationship • It is a relationship represents inheritance • An object of the derived class is-a object of the base class • Example: – Care is-a Vehicle

Is-a Relationship

Is-a Relationship

Has-a Relationship (Composition) • Inheritance is not appropriate for every class relationship. • In

Has-a Relationship (Composition) • Inheritance is not appropriate for every class relationship. • In Some cases, has-a relationship is more appropriate • Example – Employee has-a Birthdate Employee Has-a Birth. Date

Relationship Between Base and Derived Classes ü Run: Commission. Employee ü Run: Base. Plus.

Relationship Between Base and Derived Classes ü Run: Commission. Employee ü Run: Base. Plus. Commission. Employee

Creating Commission. Employee and Base. Plus. Commission. Employee Inheritance ü Run: Base. Plus. Commission.

Creating Commission. Employee and Base. Plus. Commission. Employee Inheritance ü Run: Base. Plus. Commission. Employee. Inh

Using Protected Data • Base class’s public member is accessible within the body and

Using Protected Data • Base class’s public member is accessible within the body and anywhere of an object to the class • Private: in only accessible within the body of the class and the friend functions of the base class • Protected: offers intermediate level of protection between public and private • Protected: is accessible within the body of both the base and derived class

Problems with Protected • Inconsistency of base class data • Coupling

Problems with Protected • Inconsistency of base class data • Coupling

Constructor and Destructor in Derived Class • Constructor of the base class is called

Constructor and Destructor in Derived Class • Constructor of the base class is called first, then the constructor of the derived class is followed • Destructor of the derived class is called first, then the destructor of the base class is followed

Invoking Base-Class Functions from Derived Class Objects ü Run: Commission. Employee. Invoking

Invoking Base-Class Functions from Derived Class Objects ü Run: Commission. Employee. Invoking