Last active
June 29, 2020 09:00
-
-
Save namhokim/c0a76d858b34f490d8a8e45a2267acf6 to your computer and use it in GitHub Desktop.
Class pointer usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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