I want to design a parent class
//Parent.h
class Parent {
private:
int currentId;
static int getIdSquare(); //Just random function for simplicity
}
//Parent.cpp
#include "Parent.h"
int Parent::getIdSquare() { return this->currentId * this->currentId };
Of course this won't work! because you cannot access non-static variable in static function but hold on. I want my child class to be like this
//Child.h
#include "Parent.h"
class Child : public Parent {
private:
static int index;
};
//Child.cpp
#include "Child.h"
int Child::index = 5;
So that in main when I call Child::getIdSquare();
I will get 25. And I should not be able to call Parent::getIdSquare()
because its private. How do I go on about creating something like. This is a non-working code just to illustrate the concept. So if I make another child class i can specified the index in its own body. I want to call the method statically.
Please help me figure out this puzzle!