0% found this document useful (0 votes)
17 views11 pages

Pointers in C++

Pointers in C++ are variables that store memory addresses of other variables, enabling direct memory manipulation and dynamic memory allocation. They are crucial for efficient memory management, polymorphism, and handling objects in Object-Oriented Programming. Pointers allow for dynamic creation of objects, runtime function selection, and support complex relationships in class hierarchies.

Uploaded by

teracod883
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views11 pages

Pointers in C++

Pointers in C++ are variables that store memory addresses of other variables, enabling direct memory manipulation and dynamic memory allocation. They are crucial for efficient memory management, polymorphism, and handling objects in Object-Oriented Programming. Pointers allow for dynamic creation of objects, runtime function selection, and support complex relationships in class hierarchies.

Uploaded by

teracod883
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

What are Pointers?

A pointer is a variable in C++ that stores the memory address of


another variable. Instead of holding a data value, a pointer holds
the location in memory where the data is stored.

Pointers are powerful tools in C++ because they allow you to


manipulate memory directly, allocate memory dynamically, and
handle objects and arrays in a more efficient manner.

Why use pointers?

Pointers are useful in situations like:

● Dynamic memory allocation: Objects or arrays can be


created at runtime.
● Efficient memory management: Reducing memory usage by
using references.
● Polymorphism: Allowing the same function call to behave
differently based on the object type, even when using a base
class pointer.

( The phrase means that the behavior of the function (what the
function does when it is called) will change depending on the
actual type of the object that the base class pointer is pointing
to.)
Pointer Basics
In C++, pointers allow us to work with memory directly by storing
memory addresses.

● Declaring a pointer:
To declare a pointer, use the * symbol.

int* ptr; // Pointer to an integer

Getting the address of a variable:

The & operator is used to get the address of a variable.

int x = 10;

int* ptr = &x; // Pointer stores the address of x

Dereferencing a pointer:

Dereferencing refers to accessing the value stored at the


address the pointer holds. This is done using the * operator.

int value = *ptr; // Dereferencing to get the value stored at ptr


EXAMPLE:

#include <iostream>

using namespace std;

int main() {

int x = 10;

int* ptr = &x;

// Pointer to x

cout << "Address of x: " << ptr << endl;

// Printing address of x

cout << "Value of x: " << *ptr << endl;

// Dereferencing pointer to get value

return 0;

OUTPUT:

Address of x: 0x7ffd23d2b2f8 // Example memory address

Value of x: 10
Pointers in Object-Oriented Programming

Pointers are crucial in OOP, where they are used to:

● Dynamically allocate memory for objects.


● Implement polymorphism (where a base class pointer can
point to objects of derived classes).
● Enable dynamic dispatch for method calls.
● Handle dynamic arrays of objects.

Dynamic Memory Allocation for Objects

In OOP, we use pointers to create objects dynamically on the


heap using the new operator. This is useful when you don't
know how many objects you need at compile-time.

Example: Dynamically Creating an Object

CODE:

#include <iostream>

using namespace std;

class Box {

public:

double length;

double width;

double height;
Box(double l, double w, double h) : length(l), width(w), height(h)
{}

double volume() {

return length * width * height;

};

int main() {

Box* ptr = new Box(5.0, 3.0, 2.0); // Create a Box object


dynamically

cout << "Volume of the box: " << ptr->volume() << endl;

delete ptr; // Free the dynamically allocated memory

return 0;

Explanation:

● Box* ptr = new Box(5.0, 3.0, 2.0); dynamically


allocates memory for a Box object.
● ptr->volume() calls the volume() method on the
dynamically created object.
● delete ptr; deallocates the memory, preventing memory
leaks.

OUTPUT:
Volume of the box: 30

Polymorphism and Pointers:

In OOP, polymorphism allows a base class pointer to point to


objects of derived classes, and the correct method is called
based on the object type at runtime. This is achieved using
virtual functions and base class pointers.

Example: Polymorphism Using Base Class Pointer

CODE:

#include <iostream>

using namespace std;

class Animal {

public:

virtual void speak() { // Virtual function

cout << "Animal speaks!" << endl;

};

class Dog : public Animal {

public:

void speak() override {

cout << "Dog barks!" << endl;

}
};

class Cat : public Animal {

public:

void speak() override {

cout << "Cat meows!" << endl;

};

int main() {

Animal* ptr; // Pointer to base class

Dog dog;

Cat cat;

ptr = &dog; // Base class pointer pointing to derived class


object

ptr->speak(); // Calls Dog's speak method

ptr = &cat;

ptr->speak(); // Calls Cat's speak method

return 0;
}

Explanation:

● The speak() function is virtual in the base class, so the


correct function is determined at runtime based on the object
type.
● The base class pointer (ptr) can point to objects of derived
classes (Dog and Cat), demonstrating polymorphism.

OUTPUT:

Dog barks!

Cat meows!

Inheritance and Pointers

Inheritance is a fundamental concept in OOP. When you inherit a


class, the derived class has access to the base class’s members.
Pointers allow base class objects to refer to derived class objects.

Example: Inheritance with Pointers:

CODE:

#include <iostream>

using namespace std;

class Vehicle {

public:

virtual void display() {


cout << "This is a vehicle." << endl;

};

class Car : public Vehicle {

public:

void display() override {

cout << "This is a car." << endl;

};

int main() {

Vehicle* vehiclePtr; // Base class pointer

Car car;

vehiclePtr = &car;

vehiclePtr->display(); // Calls Car's display() method due to


polymorphism

return 0;

}
Explanation:

● A base class pointer (vehiclePtr) can point to a derived


class object (car).
● The virtual function in the base class enables runtime
polymorphism, calling the correct method (Car::display)
even though the pointer is of type Vehicle*.

OUTPUT:

This is a car.

Pointers are essential in C++ as they give us more control over


memory and enable dynamic memory management, which is
crucial for writing efficient, high-performance programs. In the
context of Object-Oriented Programming (OOP):

● Pointers allow dynamic allocation of objects.


● They facilitate polymorphism, enabling runtime function
selection.
● They allow efficient management of arrays of objects and
other dynamic structures.
● They support complex relationships in inheritance and class
hierarchies.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy