I am a C++ newbie and I just started with class inheritance. I have previously had a bit of C++ experience with pointers and variables but don't remember much.
Here is my problem.
So I have 2 Classes, one base and one derivied
Base.h
#ifndef BASE_H
#define BASE_H
class Base {
protected:
int* value_;
public:
int* GetValue() { return value_; }
void SetValue(int* value) { value_ = value; }
Base() {
int tmp = 5;
SetValue(&tmp);
}
};
#endif
Derived.h
#ifndef DERIVED_H
#define DERIVED_H
#include "Base.h"
class Derived : public Base {
public:
Derived() { Base(); }
};
#endif
main.cpp
#include <iostream>
#include "base.h"
#include "derived.h"
int main() {
Derived derived = new Derived();
std::cout << derived->GetValue() << std::endl;
return 0;
}
Console output
0
What am I doing wrong?
The console output should be 5
, right?