Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism

Pointers to Derived Classes and Base Classes

C++ Object Oriented Programming

1) Pointer to Derived Class 2) Pointer to Base Class

Pointers to Derived Classes and Base Classes

 

1) Pointer to Derived Class

We can declared a pointer

#include <iostream>

using namespace std;

class Base {

public:

void showBase(){

        cout<<"\n Base class Function!";

}

};

class Derived:public Base {

public:

void showDerived(){

        cout<<"\n Derived class Function!";

}

};

int main()

{

Derived obj;

Derived *ptr = &obj;

 ptr‒>showBase();

return 0;

}

Output

Base class Function!

Derived class Function!

Code Explantion : Note that in above program, the type of pointer variable ptr is Derived *. Hence it is a pointer to derived class.

 

2) Pointer to Base Class

•  A pointer of type base class can point to both the base class object as well as a  derived class object. But by default it can only access base class members. For

example ‒

#include <iostream>

using namespace std;

class Base {

public:

void showBase(){

       cout<<"\n Base class Function!";

}

};

class Derived:public Base {

public:

void showDerived(){

      cout<<"\n Derived class Function!";

}

};

int main(){

Derived obj;

Base *ptr = &obj;

ptr‒>showBase();

ptr‒>showDerived();   //Error as base pointer does not know about derived class function

return 0;

}

Code Explanation: In above code we have defined a base class pointer using a variable ptr. Since pointer is of type base class it can not access the function defined in the derived class. Hence we get error on execution of above program

 

Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Pointers to Derived Classes and Base Classes


Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation