Object oriented programming slides for presentation

abdullahkhann3534 7 views 53 slides May 17, 2025
Slide 1
Slide 1 of 53
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53

About This Presentation

Presentation


Slide Content

Object Oriented Programming W7 - L7 02/08/2024

Pointers Overview: Variable & MEMORY Computer memory is a collection of different memory locations. These memory locations are numbered sequentially. Each variable is created at a unique location in memory known as its address. A program may declare many variables. A variable declaration reserves specific amount of space in memory for a particular variable. The variable name is used to refer to that memory location. It allows the users to access a value in the memory. The computer refers to the memory using address. 02/08/2024

When we declare a variable three things are associated with that variable Variable name Variable type Variable address 02/08/2024

Example: Int main() { Int a; Cout << “The address of a is: ” << &a; Return 0; } 02/08/2024

Can we store address of variable in another variable? 02/08/2024 Int main() { Int a; Int b; B = &a Cout << “The address of a is: ” << &a; Return 0; }

Pointer A pointer is a variable that is used to store a memory address. WHY we need Memory address when we can use variable with their names? Pointers can access and manipulate data in computer memory directly 02/08/2024

Advantages of Pointer: It can access the memory address directly. It can save memory It runs faster as it doesnot duplicate data. 02/08/2024

Example: Int main() { Int n ; Int * ptr ; Cout << “enter an integer” ; Cin >> n ; Ptr = &n; Cout << “the value of n is” << n ; Cout << “the address of n” << ptr ; Return 0; } 02/08/2024

Void pointer( A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typecasted to any type ) // find output Int main() { Int a = 10; Float b = 10.5; Char c = ‘a’ ; Int * ptr ; Ptr = &a; Cout << “address stored in pointer is : ” << ptr ; } 02/08/2024

Find Error?? 02/08/2024 Int main() { Int a = 10; Float b = 10.5; Char c = ‘a’ ; Int * ptr ; Ptr = &b; Cout << “address stored in pointer is : ” << ptr ; }

Void Pointer 02/08/2024 Int main() { Int a = 10; Float b = 10.5; Char c = ‘a’ ; void * ptr ; Ptr = &a; Cout <<“value of a is:” << a; Cout << “address stored in pointer is : ” << ptr ; } Ptr = &b; Cout <<“value of b is :” << b; Cout << “address stored in pointer is : ” << ptr ; Ptr = &c; Cout <<“value of c is :” << c; Cout << “address stored in pointer is : ” << ptr ;

Output: 02/08/2024

Dereference Operator: ( * ) In computer programming, a dereference operator, also known as an indirection operator, operates on a pointer variable. It returns the location value, in memory pointed to by the variable's value . 02/08/2024

Example: Int main() { Int a = 20 ; Int * ptr ; Ptr = &a; Cout << the value stored in pointer is: “ << * ptr ; } 02/08/2024

Output : the value stored in pointer is: 20 02/08/2024

Data manipulation using Pointers Int main () { Int a , b , s , *p1, *p2 ; P1 = &a ; P2 = &b; Cout << Enter an Integer : “ ; Cin >> *p1; Cout << Enter an Integer : “ ; Cin >> * p2; 02/08/2024 S = *p1 + *p2 ; Cout << “sum is : ” << s; }

This pointer To know the address of current object , we use this pointer. Used to distinguished our data members and local variables when both are declared with the same name. We identify the data member using this pointer 02/08/2024

Example: Class person { Int age ; String name ; Person( int age , string name) { Age = age ; //compiler confusion Name = name ; } 02/08/2024 Void printvalue () { Cout << “age” << age ; Cout << “name” << name ; } };

Int main () { Person a(45 , “ABC”); a.printvalue (); } 02/08/2024

Output: 02/08/2024 Garbage value

Solution : this Pointer Class person { Int age ; String name ; Person( int age , string name) { This ->Age = age ; This ->Name = name ; } } 02/08/2024 Void printvalue () { Cout << “age” << age ; Cout << “name” << name ; } };

Int main () { Person a(35 , “ abc ”); a.printvalue (); } 02/08/2024

Output : 3 5 abc 02/08/2024

Task: we have two data members num and ch. In member function setMyValues () we have two local variables having same name as data members name. Create display function to show both values. In such case if you want to assign the local variable value to the data members then you won’t be able to do until unless you use this pointer, because the compiler won’t know that you are referring to object’s data members unless you use this pointer. 02/08/2024

Solution: 02/08/2024 #include < iostream > using namespace std ; class Demo { private : int num ; char ch ; public: void setMyValues ( int num , char ch ) { this-> num = num ; this-> ch = ch ; } void displayMyValues () { cout << num << endl ; cout << ch ; } }; int main() { Demo obj ; obj.setMyValues (100, 'A'); obj.displayMyValues (); return 0; }

Polymorphism One name multiple forms Poly means many and morphism means forms The behaviour of the object can be implemented at rum time. Compile time polymorphism Function overloading / method overloading / constructor overloading / operator overloading 2. Run time polymorphism virtual function Pointer to objects: pointer can also refer to an object of a class. The member of an object can be accessed through pointers by using the symbol ->(member access operator) Ptr ->member 02/08/2024

Overview: // early binding compiler already knows which function to call Class A { Public: void show() { Cout << “base class” ; } }; Derived Class 02/08/2024 Class B: public A { Public: void show() { Cout << “Derived class” ; } }; Int main() { B obj ; Obj.show (); } Output:

Overview: // early binding Class A { Public: void show() { Cout << “base class” ; } }; Derived Class Base class 02/08/2024 Class B: public A { Public: void show() { Cout << “Derived class” ; } }; Int main() { B obj ; Obj.show (); Obj.A.show (); } Output:

Overview: // late Binding Class A { Public: void show() { Cout << “base class” ; } }; base class 02/08/2024 Class B: public A { Public: void show() { Cout << “Derived class” ; } }; Int main() { A * ptr ; A obj ; Ptr = & obj ; Ptr - > show() ; } Output:

Virtual Base class / Virtual Function : Key word virtual Virtual function is re defined in derived class. When a virtual function is defined in base class, then the pointer to base class is created. Now on the basis of type of object assigned the respective class function is called. This is also called run time polymorphism. 02/08/2024

Virtual Function A virtual function (also known as virtual methods) is a member function that is declared within a base class and is re-defined (overridden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the method. 02/08/2024

Example: Class B : public A { Public: Void show() { Cout << “Child Class B” ; } }; 02/08/2024 Class A { Public: Void show() { Cout << “parent Class A” ; } }; Class C : public A { Public: Void show() { Cout << “Child Class C” ; } };

Int main () { A obj1 ; B obj2 ; C obj3; A * ptr ; Ptr = &obj1 ; Ptr -> show() ; 02/08/2024 Ptr = &obj2 ; Ptr ->show(); Ptr = &obj3; Ptr ->show(); } Pointer can store address of child class , but with the same pointer if we want to access child class member that also exist in parent class , this will always call member function of parent class.

Output: 02/08/2024

Using virtual keyword: One function is providing multiple functionalities depends on which type of address , pointer is holding. 02/08/2024

Example: Class B : public A { Public: Void show() { Cout << “Child Class B” ; } }; 02/08/2024 Class A { Public: Virtual Void show() { Cout << “parent Class A” ; } }; Class C : public A { Public: Void show() { Cout << “Child Class C” ; } };

Int main () { A obj1 ; B obj2 ; C obj3; A * ptr ; Ptr = &obj1 ; Ptr -> show() ; 02/08/2024 Ptr = &obj2 ; Ptr ->show(); Ptr = &obj3; Ptr ->show(); }

Output: 02/08/2024

Working: When a member function is declared as a virtual in parent class and is called with pointer , the compiler checks the type of object referred by the pointer. It executes the member function according to the type of object not by type of pointer. The type of pointer in each call is different so the compiler executes different function each time. 02/08/2024

Binding: Binding refers to the process of converting identifiers (such as variable and performance names into addresses.) Binding is done for each variable and functions. For functions, it means that matching the call with the right function definition call by the compiler. Binding can be performed in two ways: Early binding/compile time binding/ static binding(As the name indicates compiler directly associate an address to the function call. In early binding, compiler knows at the time of compilation which block of code is to be executed upon a function call.) Late binding/ Dynamic binding(in late binding, compiler does not know at the time of compilation that which block of code is to be executed upon a function call. Late binding is attained by using virtual functions.) 02/08/2024

Early Binding Is also called compile time binding or static binding As the name indicates, compiler directly associate an address to the function call. In early binding , compiler knows at the time of compilation which block of code is to be executed upon a function call. 02/08/2024

Example: Early Binding 02/08/2024 Class A { Public: Void show() { Cout << “parent Class A” ; } }; Class B : Class A { Public: Void show() { Cout << “Derived Class B” ; } }; Int main() { B obj ; Obj.show (); Obj.A.show (); }

Late Binding In late binding compiler doesn’t know at the time of compilation that which block of code is to be executed upon a function call. Late binding is attained using virtual function . 02/08/2024

Example : Class parent { Public: Virtual void show() { Cout << “parent class” ; } }; 02/08/2024 Class child1: public parent { Public: void show() { Cout << “child1 class” ; } };

02/08/2024 Class child2: public parent { Public: void show() { Cout << “child2 class” ; } };

Int main() { Parent * ptr [5] ; // array of pointers //This pointer can store address of child class address . Int op , i ; // for object creation Cout << “Enter 1 for parent , 2 for child1 & 3 for child2” ; } 02/08/2024

For (I = 0 ; I < 5 ; i ++) { Cout << “which object to create ?” ; Cin >> op ; If (op == 1) Ptr [ i ] = new parent ; // create new object of parent Else if (op ==2) Ptr [ i ] == new child1; Else (op ==3) Ptr [ i ] == new child2; } } 02/08/2024

For (I = o ; I <5 ; i ++) { Ptr [ i ] -> show(); It will call that function whose class object address is stored in pointer array. } 02/08/2024

02/08/2024

What if we remove virtual keyword? What will be the output ?? 02/08/2024

Abstract class(Model class) and pure Virtual function No definition of virtual function assigned with zero. We can’t create the object of parent class having pure virtual function If we don’t override the function in child class that was present in abstract class then the child class will also become abstract class. 02/08/2024

Pure virtual function 02/08/2024

02/08/2024 Output: Child1 class… Child2 class…