Previous
Previous Product Image

Easy Notes Of Object oriented programming unit-3 @Computer Diploma

Original price was: ₹99.99.Current price is: ₹19.99.
Next

Easy Notes Of Object oriented programming unit-5 @Computer Diploma

Original price was: ₹99.99.Current price is: ₹19.99.
Next Product Image

Easy Notes Of Object oriented programming unit-4 @Computer Diploma

Original price was: ₹99.99.Current price is: ₹19.99.

Unit – IV Pointers and Polymorphism in C++

4.1 Concept of Pointer: Pointer declaration, Pointer operator, Address operator, Pointer
arithmetic
4.2 Pointer to Object: Pointer to object, ‘this’ pointer, Pointer to derived class
4.3 Introduction of Polymorphism, Types of polymorphism
4.4 Compile time Polymorphism: Function overloading, Revision of constructor overloading,
Operator overloading: Rules for operator overloading, Overloading of unary and binary
operators
4.5 Run time polymorphism: Virtual function, Rules for virtual function, Pure virtual function

Hurry Up!
Add to Wishlist
Add to Wishlist

Description

4.1 Concept of Pointers

A pointer is a variable that stores the memory address of another variable. It “points” to a location in memory.

  • Pointer Declaration: A pointer variable is declared with an asterisk (*) before its name.
    • int *ptr; declares a pointer ptr that can hold the address of an integer.
  • Pointer Operator (*): Also known as the dereference operator, it is used to access the value at the memory address stored in the pointer.
    • *ptr = 20; assigns the value 20 to the variable that ptr is pointing to.
  • Address Operator (&): Returns the memory address of a variable.
    • ptr = &x; stores the memory address of the variable x into the pointer ptr.
  • Pointer Arithmetic: Pointers can be used in arithmetic operations like addition and subtraction. When you increment or decrement a pointer, it moves to the next memory location of its data type. For example, incrementing an int pointer moves it by 4 bytes (on most systems).

 

4.2 Pointers to Objects

Pointer to Object A pointer can be used to store the address of an object. This allows you to access an object’s members using the -> (arrow) operator.

  • MyClass *obj_ptr; declares a pointer to a MyClass object.
  • obj_ptr = new MyClass(); dynamically allocates an object and stores its address in the pointer.
  • obj_ptr->memberFunction(); calls a member function of the object.

‘this’ Pointer The ‘this’ pointer is a special, implicit pointer available inside every non-static member function. It holds the address of the object on which the function is called. It is used to refer to the current object.

  • It’s useful for returning the current object from a member function or for distinguishing between a member variable and a local variable with the same name.

Pointer to Derived Class A pointer of a base class type can point to an object of a derived class. This is a crucial concept for polymorphism. While a base class pointer can point to a derived class object, it can only access the members that are inherited from the base class. To access derived class-specific members, you need to cast the pointer or use virtual functions.

 

4.3 Introduction to Polymorphism

Polymorphism means “many forms.” In OOP, it’s the ability of a single interface to be used for a general class of actions. It allows an object to take on different forms depending on the context. This is achieved through function overloading, operator overloading, and virtual functions.

Types of Polymorphism

  • Compile-time Polymorphism (or Static Polymorphism): The compiler resolves which function to call at compile time. This is achieved through function overloading and operator overloading.
  • Run-time Polymorphism (or Dynamic Polymorphism): The function call is resolved at runtime. This is achieved through virtual functions.

 

4.4 Compile-Time Polymorphism

Function Overloading Function overloading is the ability to define multiple functions in the same scope with the same name but with different parameter lists (different number or types of arguments). The compiler selects the correct function based on the arguments provided in the call.

Constructor Overloading This is a specific form of function overloading where a class can have multiple constructors with different parameter lists, allowing for different ways to initialize an object.

Operator Overloading Operator overloading allows you to redefine how an operator (like +, -, *) works for user-defined data types (classes).

Rules for Operator Overloading:

  • You cannot overload operators for built-in data types.
  • The arity (number of operands) of the operator cannot be changed.
  • Certain operators like . (member access) and :: (scope resolution) cannot be overloaded.
  • Overloaded operators can be implemented as either a member function or a non-member (friend) function.

Overloading of Unary and Binary Operators:

  • Unary operators (e.g., ++, --, -) work on a single operand. When overloaded as a member function, they take no arguments.
  • Binary operators (e.g., +, -, *) work on two operands. When overloaded as a member function, they take one argument.

 

4.5 Run-Time Polymorphism

Virtual Function A virtual function is a member function in a base class that you expect to be redefined in a derived class. When you call a virtual function through a base class pointer or reference, the program determines which version of the function (base or derived) to call at runtime. This dynamic dispatch is the core of run-time polymorphism. The virtual keyword is used to declare a virtual function in the base class.

Rules for Virtual Functions:

  • They must be member functions of a class.
  • They cannot be static or friend functions.
  • They are inherited by derived classes.
  • The function prototypes (return type and parameters) in the base and derived classes must match.

Pure Virtual Function A pure virtual function is a virtual function in the base class that has no definition. It is declared with = 0;.

  • virtual void show() = 0;
  • A class containing at least one pure virtual function is called an abstract class. You cannot create objects of an abstract class.
  • Any derived class must provide an implementation for all pure virtual functions of the base class; otherwise, it also becomes an abstract class.

Keywords of this Topic: pointer, * (dereference), & (address), -> (arrow), this, polymorphism, virtual, pure virtual, override, final.

Reviews

There are no reviews yet.

Be the first to review “Easy Notes Of Object oriented programming unit-4 @Computer Diploma”

Your email address will not be published. Required fields are marked *

Shopping cart

0
image/svg+xml

No products in the cart.

Continue Shopping