8 C Programming CLASS This pointer Static Class

  • Slides: 25
Download presentation
8. C++ Programming • CLASS • This pointer • Static Class • Friend Class

8. C++ Programming • CLASS • This pointer • Static Class • Friend Class • Operator Overloading • Inheritance

this-pointer this returns always a reference to the current object. Example: Circle: : Circle(double

this-pointer this returns always a reference to the current object. Example: Circle: : Circle(double radius) { this -> radius = radius; } radius of the current (new) object constructor’s argument 2

this-pointer class AAA{ private: int a; char c; double d; void set(AAA *pa) {

this-pointer class AAA{ private: int a; char c; double d; void set(AAA *pa) { cin >> pa -> a; c; d; class AAA{ private: int a; char c; double d; void set() //void set(AAA *this) { cin >> this -> a; cin >> this -> c; cin >> this -> d; 3

[ practice 1 this pointer ] 4

[ practice 1 this pointer ] 4

[ practice 1 this pointer ] 5

[ practice 1 this pointer ] 5

[ practice 2 Static class ] 6

[ practice 2 Static class ] 6

Friends A Class can declare other classes as "friend classes". A Class can declare

Friends A Class can declare other classes as "friend classes". A Class can declare external functions as "friends". friends can access private members and methods. 7

Friend Declaration class foo { private: int i, j; … friend class fee; friend

Friend Declaration class foo { private: int i, j; … friend class fee; friend int printfoo( foo &f 1); }; 8

[ practice 3 Friend ] 9

[ practice 3 Friend ] 9

[ practice 3 Friend ] 10

[ practice 3 Friend ] 10

Operator overloading for “standard” functions Like a normal function definition but with operator-binding syntax

Operator overloading for “standard” functions Like a normal function definition but with operator-binding syntax with the function name. E. g. + operator: <datatype> operator+ … } (double a, class. A b) { 11

[ practice 4 Operator overloading ] 12

[ practice 4 Operator overloading ] 12

Inheritance You can create a new class that inherits from an existing class. The

Inheritance You can create a new class that inherits from an existing class. The new class is a specialization of the existing class (called subclass). You can add new members and methods. You can replace methods. Motivation: Reuse of existing code 13

Inheritance Example: You have a class that represents “circles". Create a new class that

Inheritance Example: You have a class that represents “circles". Create a new class that represents “cylinders". Most of the behavior and attributes are the same, but a few are different/new (height of cylinder, different area computation, volume) 14

Inheritance Example A Cylinder is a extended (specialized) form of a Circle. height Circle

Inheritance Example A Cylinder is a extended (specialized) form of a Circle. height Circle radius Cylinder 15

[ practice 5 inheritance ] 16

[ practice 5 inheritance ] 16

[ practice 5 inheritance…continue ] 17

[ practice 5 inheritance…continue ] 17

[ practice 5 inheritance ] 18

[ practice 5 inheritance ] 18

Overloading vs Overriding • Overloading • - allows creating several methods with the same

Overloading vs Overriding • Overloading • - allows creating several methods with the same name which differ from each other in the type of the input and the output of the function. Polymorphism way. Overriding - allows a child class to provide a specific implementation of a method that is already provided by one of its parent classes Overloading Overriding method name same type or number of parameter different same return type same or different same • class C • { • public : • void test(int a); • void test(char b); • } • class D : public C • { • public : • void test(int a); • int test(int a); • } 19

[ practice 6 method overriding (1/2) ] 20

[ practice 6 method overriding (1/2) ] 20

[ practice 6 method overriding (2/2) ] 21

[ practice 6 method overriding (2/2) ] 21

[ practice 6 method overriding ] 22

[ practice 6 method overriding ] 22

[ practice 7 Overriding & Overloading ] 23

[ practice 7 Overriding & Overloading ] 23

[ explain 7 Overriding & Overloading ] 24

[ explain 7 Overriding & Overloading ] 24

Thank you

Thank you