Is it possible to declare a method in base class and define in derived class? I'm struggling..
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
|
//base.h
class Base{
protected:
int value;
public:
void setValue(int);
int getValue();
};
//derived.h
#include <string>
#include "base.h"
class Derived: public Base{
public:
std::string name;
};
//derived.cpp
#include "derived.h"
void Derived::setValue(int v){
value = v;
}
int Derived::getValue(){
return value;
}
//main.cpp
#include <iostream>
#include "derived.h"
int main(){
Derived d1;
d1.setValue(1);
std::cout << d1.getValue << std::endl;
}
|
Last edited on