Base Pointer pointer Derived to access

class Base
{
public:
int base_int;
};

class Derived : public Base
{
public:
int derived_int;
};

Base* basepointer = new Derived(); // what purpose we use for ??,
Derived* derivedpointer = new Derived(); //

i know that "Base* basepointer" only access (Base) which is part of Derived object, and Derived pointer point Derived then access all which is inherit Base.
Im am newbie, so What purpose when we use "Base* basepointer = new Derived;", Instead Why we dont use "Derived *derivedpointer = new Derived" for all.
i know that "Base* basepointer" only access (Base) which is part of Derived object,
Not quite true.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

class Base
{
  public:
  int base_int;
  virtual void say_something() {std::cout << "I am  base";}
};

class Derived : public Base
{
  public:
  int derived_int;
  void say_something() {std::cout << "I am  Derived";}
};

int main()
{
    Base* basepointer = new Derived(); 
    basepointer->say_something();

   basepointer = new Base();
    basepointer->say_something();
}


Run this code. We use a Base* to access a function in the Derived object!

Then, we use the SAME Base*, and we make it point at a different object, and call the SAME function, and now it uses the function in a Base object!

So one reason to use Base* is that we can make it point at a Base object or ANY derived object. Maybe we don't know what kind of object we're going to have; maybe it gets decided at runtime. Maybe it will be changed many times, and it will point at a Base, then a Derived, then something else that inherits from Base, and then something that inherits from Derived. Over and over.

In your example, yes, might as well use Derived* , because your example is so simple and not doing anything at all complicated.




thank you so much, this answer so good, looking forward.

it use for Virtual function. However since i search onto google, s.one say that "Using a base class pointer or reference allows you to write functions that accept all derived classes of the base generically so you can call common non-virtual functions on them. Otherwise, you'd have to write the same function multiple times for the different derived classes, or use templates." so can you explain give me a little bit about this.
one time again, thank so much.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

struct Base
{
  virtual void print()
  {
    std::cout << "Base print";
  }

  void nonVirtualPrint()
  {
    std::cout << "Base print NON virtual";
  }
};

struct Derived : public Base
{
  void print()
  {
      std::cout<< "Derived print";
  }
};

struct DerivedTwo : public Base
{
  void print()
  {
      std::cout<< "DerivedTwo print";
  }
};

void printer(Base& object)
{
  object.print();
  std::cout << '\n';
   object.nonVirtualPrint();
  std::cout << '\n';
}

int main()
{
  Base objectOne;
  Derived objectTwo;
  DerivedTwo objectThree;

  printer(objectTwo);
  printer(objectOne);
  printer(objectThree);

}




We had one function, printer, and we passed to it THREE different kinds of object! I used the same function for three different types, calling both virtual and non-virtual functions.
Last edited on
Topic archived. No new replies allowed.