What is pointer
In C++, a pointer is a variable that stores the memory address of another variable. Pointers are powerful tools that allow you to work directly with memory, access data structures dynamically, and create more efficient code in certain situations. Understanding pointers is fundamental to mastering C++.
1.Declaring a pointer: To declare a pointer in C++, you use an asterisk (*) before the variable name. For example
int* ptr; // Declares a pointer to an integer
2. Initializing a pointer: A pointer should be initialized with the address of the variable it is intended to point to. You can obtain the address of a variable using the “address-of” operator (&). For example
int x = 50; int* ptr = &x; // ptr now points to the memory address of x
Pointer to derived class
- In object-oriented programming, Pointer can be used for objects of base classes as well as objects of derived classes.
- A pointer to a derived class refers to a pointer that points to an object of a derived class, which is a subclass that inherits from a base class.
- The concept is based on inheritance, where a derived class inherits the properties and behaviors (data members and member functions) of the base class.
- When you have a pointer to a derived class, you can use it to access both the member functions and data members of the base class and the additional ones defined in the derived class.
- This is one of the fundamental features of polymorphism, as you can treat objects of the derived class as objects of the base class, allowing for code reusability and flexibility.
- The process of assigning a derived class object’s address to a base class pointer is called “upcasting.” It’s called “upcasting” because you are going up the class hierarchy from the derived class to the base class.
Example-
#include <iostream> // Base class class Shape { public: virtual void draw() { std::cout << "Drawing a shape." << std::endl; } }; // Derived class 1 class Circle : public Shape { public: void draw() override { std::cout << "Drawing a circle." << std::endl; } }; // Derived class 2 class Rectangle : public Shape { public: void draw() override { std::cout << "Drawing a rectangle." << std::endl; } }; int main() { // Create objects of the derived classes Circle circle; Rectangle rectangle; // Create pointers to the base class and assign the derived objects Shape* shapePtr1 = &circle; Shape* shapePtr2 = &rectangle; // Call the draw() function using the pointers shapePtr1->draw(); // This will call the draw() function of the Circle class. shapePtr2->draw(); // This will call the draw() function of the Rectangle class. return 0; }
In this example, we use a base class pointer Shape* to point to objects of derived classes Circle and Rectangle. When we call the draw() function using these pointers, the appropriate draw() function from the derived class is invoked due to polymorphism. This is known as “runtime polymorphism” because the function call is resolved at runtime based on the actual type of the object being pointed to.
FAQs
Q1. What is the purpose of pointer in C++?
A: Pointers in C++ allow you to directly manipulate memory addresses and indirectly access and modify data in memory. They are useful for dynamic memory allocation, creating data structures, and passing arguments by reference.
Q2. Can a derived class have multiple base classes in C++?
A: Yes, C++ supports multiple inheritance, where a derived class can inherit from multiple base classes.
Q3. Are pointers to derived classes type-safe?
A: Yes, pointers to derived classes are type-safe, ensuring that only compatible types can be assigned to the pointer.
Q4. Can a base class pointer point to an object of a derived class?
A: Yes, a base class pointer can point to an object of a derived class, allowing access to the derived class’s members through the pointer.
more related content on Object Oriented Programming