8 C Programming CLASS This pointer Static Class



![[ practice 1 this pointer ] 4 [ practice 1 this pointer ] 4](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-4.jpg)
![[ practice 1 this pointer ] 5 [ practice 1 this pointer ] 5](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-5.jpg)
![[ practice 2 Static class ] 6 [ practice 2 Static class ] 6](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-6.jpg)


![[ practice 3 Friend ] 9 [ practice 3 Friend ] 9](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-9.jpg)
![[ practice 3 Friend ] 10 [ practice 3 Friend ] 10](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-10.jpg)

![[ practice 4 Operator overloading ] 12 [ practice 4 Operator overloading ] 12](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-12.jpg)



![[ practice 5 inheritance ] 16 [ practice 5 inheritance ] 16](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-16.jpg)
![[ practice 5 inheritance…continue ] 17 [ practice 5 inheritance…continue ] 17](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-17.jpg)
![[ practice 5 inheritance ] 18 [ practice 5 inheritance ] 18](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-18.jpg)

![[ practice 6 method overriding (1/2) ] 20 [ practice 6 method overriding (1/2) ] 20](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-20.jpg)
![[ practice 6 method overriding (2/2) ] 21 [ practice 6 method overriding (2/2) ] 21](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-21.jpg)
![[ practice 6 method overriding ] 22 [ practice 6 method overriding ] 22](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-22.jpg)
![[ practice 7 Overriding & Overloading ] 23 [ practice 7 Overriding & Overloading ] 23](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-23.jpg)
![[ explain 7 Overriding & Overloading ] 24 [ explain 7 Overriding & Overloading ] 24](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-24.jpg)

- Slides: 25

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 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) { 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](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-4.jpg)
[ practice 1 this pointer ] 4
![practice 1 this pointer 5 [ practice 1 this pointer ] 5](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-5.jpg)
[ practice 1 this pointer ] 5
![practice 2 Static class 6 [ practice 2 Static class ] 6](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-6.jpg)
[ practice 2 Static class ] 6

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 int printfoo( foo &f 1); }; 8
![practice 3 Friend 9 [ practice 3 Friend ] 9](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-9.jpg)
[ practice 3 Friend ] 9
![practice 3 Friend 10 [ practice 3 Friend ] 10](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-10.jpg)
[ practice 3 Friend ] 10

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](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-12.jpg)
[ practice 4 Operator overloading ] 12

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 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 radius Cylinder 15
![practice 5 inheritance 16 [ practice 5 inheritance ] 16](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-16.jpg)
[ practice 5 inheritance ] 16
![practice 5 inheritancecontinue 17 [ practice 5 inheritance…continue ] 17](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-17.jpg)
[ practice 5 inheritance…continue ] 17
![practice 5 inheritance 18 [ practice 5 inheritance ] 18](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-18.jpg)
[ practice 5 inheritance ] 18

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 12 20 [ practice 6 method overriding (1/2) ] 20](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-20.jpg)
[ practice 6 method overriding (1/2) ] 20
![practice 6 method overriding 22 21 [ practice 6 method overriding (2/2) ] 21](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-21.jpg)
[ practice 6 method overriding (2/2) ] 21
![practice 6 method overriding 22 [ practice 6 method overriding ] 22](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-22.jpg)
[ practice 6 method overriding ] 22
![practice 7 Overriding Overloading 23 [ practice 7 Overriding & Overloading ] 23](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-23.jpg)
[ practice 7 Overriding & Overloading ] 23
![explain 7 Overriding Overloading 24 [ explain 7 Overriding & Overloading ] 24](https://slidetodoc.com/presentation_image_h2/1e51dbb5627a0632d80119cd4991ef88/image-24.jpg)
[ explain 7 Overriding & Overloading ] 24

Thank you