c 2b 2b pointer to base class

Solutions on MaxInterview for c 2b 2b pointer to base class by the best coders in the world

showing results for - "c 2b 2b pointer to base class"
Léonie
04 May 2019
1#include <cstdlib>
2#include <ctime>
3#include <iostream>
4#include <vector>
5
6class Parent {
7public:
8    virtual void sayHi()
9    {
10        std::cout << "Parent here!" << std::endl;
11    }
12};
13
14class Child : public Parent {
15public:
16    void sayHi()
17    {
18        std::cout << "Child here!" << std::endl;
19    }
20};
21
22class DifferentChild : public Parent {
23public:
24    void sayHi()
25    {
26        std::cout << "DifferentChild here!" << std::endl;
27    }
28};
29
30int main()
31{
32    std::vector<Parent*> parents;
33
34    // Add 10 random children
35    srand(time(NULL));
36    for (int i = 0; i < 10; ++i) {
37        int child = rand() % 2; // random number 0-1
38        if (child) // 1
39            parents.push_back(new Child);
40        else
41            parents.push_back(new DifferentChild);
42    }
43
44    // Call sayHi() for each type! (example of polymorphism)
45    for (const auto& child : parents) {
46        child->sayHi();
47    }
48
49    return 0;
50}
queries leading to this page
bae class pointerhow you can determine whether a given base class reference is actually referring to a derived type or not 3f illustrate with code snippets3c 2b 2b pass pointer to base class on constructorc 2b 2b pointer to base class call derived methodc 2b 2b member class pointer to parentduring 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 reference to parent class c 2b 2bmake pointer of base classc 2b 2b pointers to base classvirtual property c 2b 2bc 2b 2b type of derived object in base classderived class pointer to base class c 2b 2bbase pointer objectsc 2b 2b pointer to parent classwrite and execute a simple program in c 2b 2b to prove that a base class pointer can be associated with its derived object 28for unified way of accessing all the derived objects 29 3freference derived class in baseclassc 2b 2b call base class using this pointerreference derived class in base classcast base class pointer to derived c 2b 2bcpp create pointer to objecthow to get reference to base class c 2b 2bc 2b 2b inheritance reference member of the base classsimple program in c 2b 2b to prove that a base class pointer can be associated with its derived object 28for unified way of accessing all the derived objects 29 3fc 2b 2b function pointer to base class functiondifferece betweeen pointer to object and pointer to derived classbase class pointebase class pointer c 2b 2binheritance and pointers c 2b 2bc 2b 2b cast base class pointer to subclassc 2b 2b pointer to base classwhat is base class pointer in c 2b 2bc 2b 2b base pointer to derived classpointer to a base class c 2b 2bconvert base class to derived class pointer c 2b 2bc 2b 2b set parent class to reference of passed classc 2b 2b pass pointer to base classc 2b 2b pointer to base class