Skip to content

Instantly share code, notes, and snippets.

@namhokim
Last active June 29, 2020 09:00
Show Gist options
  • Save namhokim/c0a76d858b34f490d8a8e45a2267acf6 to your computer and use it in GitHub Desktop.
Save namhokim/c0a76d858b34f490d8a8e45a2267acf6 to your computer and use it in GitHub Desktop.
Class pointer usage
#include <iostream>
class A {
private:
int valueA;
public:
A():valueA(1) {
}
int getValueA() {
return this->valueA;
}
};
class B : public A {
private:
int valueB;
public:
B():valueB(2) {
}
int getValueB() {
return this->valueB;
}
};
class C : public B {
private:
int valueC;
public:
C():valueC(3) {
}
int getValueC() {
return this->valueC;
}
};
int main(int argc, const char * argv[]) {
A *a = new A;
B *b = new B;
C *c = new C;
std::cout << a->getValueA() << std::endl; // 1
std::cout << b->getValueA() << std::endl; // 1
std::cout << c->getValueA() << std::endl; // 1
A aa;
B bb;
C cc;
std::cout << aa.getValueA() << std::endl; // 1
std::cout << bb.getValueA() << std::endl; // 1
std::cout << cc.getValueA() << std::endl; // 1
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment