C++ pointer to base class

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>

class Parent {
public:
    virtual void sayHi()
    {
        std::cout << "Parent here!" << std::endl;
    }
};

class Child : public Parent {
public:
    void sayHi()
    {
        std::cout << "Child here!" << std::endl;
    }
};

class DifferentChild : public Parent {
public:
    void sayHi()
    {
        std::cout << "DifferentChild here!" << std::endl;
    }
};

int main()
{
    std::vector<Parent*> parents;

    // Add 10 random children
    srand(time(NULL));
    for (int i = 0; i < 10; ++i) {
        int child = rand() % 2; // random number 0-1
        if (child) // 1
            parents.push_back(new Child);
        else
            parents.push_back(new DifferentChild);
    }

    // Call sayHi() for each type! (example of polymorphism)
    for (const auto& child : parents) {
        child->sayHi();
    }

    return 0;
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
cast base class pointer to derived c++ c++ pass pointer to base class on constructor c++ pass pointer to base class c++ call base class using this pointer what is base class pointer in c++ convert base class to derived class pointer C++ c++ function pointer to base class function cpp create pointer to object pointer to a base class c++ derived class pointer to base class c++ c++ cast base class pointer to subclass c++ member class pointer to parent During multiple inheritance base class pointer object can point any reference of derived class object also derived class pointer object can point any reference of base class object. simple program in C++ to prove that a base class pointer can be associated with its derived object (for unified way of accessing all the derived objects)? Write and execute a simple program in C++ to prove that a base class pointer can be associated with its derived object (for unified way of accessing all the derived objects)? c++ pointer to parent class how to get reference to base class c++ differece betweeen pointer to object and pointer to derived class base class pointe virtual property c++ how you can determine whether a given base class reference is actually referring to a derived type or not? Illustrate with code snippets3 base pointer objects reference derived class in Base Class reference derived class in BaseClass C++ set parent class to reference of passed class base class pointer c++ bae class pointer c++ type of derived object in base class make pointer of base class inheritance and pointers c++ c++ inheritance reference member of the base class reference to parent class c++ c++ pointers to base class C++ pointer to base class
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source