Base/Derived class pointers

I have no idea what is going on here, is there a non-confusing explanation to this mess, or a good write up somewhere.


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
#ifndef BASEDERIVED_H
#define BASEDERIVED_H
#include<string>
using namespace std;

class Base
{
public:
    int m_nValue;

    Base(){cout << "Base Class Constructed" << endl;};
    Base(int nValue=0)
        : m_nValue(nValue*2)
    {
        cout << "Base Class Constructed" << endl;
    }

    virtual const string GetName() { return "Base"; }
    int GetValue() { return m_nValue; }
};

class Derived: public Base
{
public:
    double m_dValue;

    Derived(int nValue=0, double dValue=0.0)
        :  Base(nValue), m_dValue(dValue)
    {
        cout << "Derived Class Constructed " << endl;
    }

    virtual const string GetName() { return "Derived"; }
    int GetValueDoubled() { return m_dValue * 2; }
};
#endif 


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
#ifndef BASEDERIVED2_H
#define BASEDERIVED2_H

class A
{
public:
    A()
    {
        cout << "A" << endl;
    }
};

class B: public A
{
public:
    B()
    {
        cout << "B" << endl;
    }
};

class C: public B
{
public:
    C()
    {
        cout << "C" << endl;
    }
};

class D: public C
{
public:
    D()
    {
        cout << "D" << endl;
    }
};
#endif


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
#ifndef BASEDERIVED3_H
#define BASEDERIVED3_H

class Alpha
{
    public:
        Alpha()
        {
            cout << "Creating Alpha " << endl;
        }
        ~Alpha()
        {
            cout << "Destroying Alpha " << endl;
        }
};

class Beta
{
    public:
        Beta()
        {
            cout << "Creating Beta " << endl;
        }
        ~Beta()
        {
            cout << "Destroying Beta " << endl;
        }
};

class Gamma : public Alpha
{
    public:
        Gamma()
        {
            cout << "Creating Gamma " << endl;
        }
        ~Gamma()
        {
            cout << "Destroying Gamma " << endl;
        }
    private:
        Beta b;
};

#endif


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
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include<vector>
#include "BaseDerived.h"
#include "BaseDerived2.h"
#include "BaseDerived3.h"
using namespace std;

int main()
{
    /*Base is a non-derived class because it does not inherit from anybody.
    C++ allocates memory for Base, then calls Base’s default constructor
    to do the initialization. */
    Base cBase(3);
    cout<<cBase.m_nValue << endl;
    cout << cBase.m_nValue << endl;
    cout << cBase.GetName() << endl;
//    cout << "pBase is a " << pBase->GetName() << " and has value " << pBase->GetValue() << endl;
    /* Derived is really two parts: a Base part, and a Derived part.
    When C++ constructs derived objects, it does so in pieces,
    starting with the base portion of the class.
    Once that is complete, it then walks through the inheritance tree and
    constructs each derived portion of the class.*/
/*    Derived cDerived(1,2.5);
    cout << cDerived.m_nValue << endl;
    cout << cDerived.m_dValue << endl;
    cout << cDerived.GetName() << endl;

    cout << "Constructing A: " << endl;
    A cA;

    cout << "Constructing B: " << endl;
    B cB;

    cout << "Constructing C: " << endl;
    C cC;

    //cout << "Constructing D: " << endl;*/
    //D cD;

//    Gamma c;

    Derived cDerived(1,5.0);
    // These are both legal!
    Base& rBase = cDerived;
    Base* pBase = &cDerived;
    Derived* pDerived = &cDerived;

    cout << "cDerived is a " << cDerived.GetName() << " and has value " << cDerived.GetValueDoubled() << endl;
    cout << "rBase is a " << rBase.GetName() << " and has value " << rBase.GetValue() << endl;
    cout << "pBase is a " << pBase->GetName() << " and has value " << pBase->GetValue() << endl;
    cout << "pDerived is a " << pDerived->GetName() << " and has value " << pDerived->GetValueDoubled() << endl;

    /*vector<Base> vBase;
    vBase.reserve(5);
    for(int i=0;i<5;i++)
        vBase.push_back(Base(i*2.0));
    cout << vBase[2].GetName()<< " and has value " << vBase[2].GetValue() << endl;*/

    return 0;
}


can you beat my lecturer and explain it in less than 4 slides :p

when I try uncommenting line 17 in main it doesn't like it.

I found the learncpp.com thing so no need to gtfm :p

Guess I need to stew on it, too late for my brain maybe, zzzzzz.
Last edited on
Line 17 looks like it was copied from like 50 - not sure why you would put line 50 on line 17 though.

Have you read the tutorials on this site?
http://www.cplusplus.com/doc/tutorial/polymorphism/
Thanks, yes I missed that copy of line 17

I also found another explanation in Lippman book, guess it pays to rest.

No I hadn't read that tutorial, thanks for pointing it out.
Topic archived. No new replies allowed.