Created
November 20, 2017 09:20
-
-
Save icecoobe/782058b33c835d23e7f20dd01ff865b5 to your computer and use it in GitHub Desktop.
cpp class test
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> | |
using namespace std; | |
class Base { | |
// int a; | |
public: | |
int a; | |
Base() { a = 0; } | |
Base(int x) { a = x; } | |
virtual ~Base() { cout << "Delete Base instance." << endl; } | |
void show(void) { cout << "[Base]" << a << endl; } | |
virtual void vshow(void) { cout << "[Base]" << a << endl; } | |
}; | |
class Derv : public Base { | |
public: | |
int a, b; | |
Derv(int x, int y, int z) : Base(x) { a = y; b = z; } | |
~Derv() { cout << "Delete Derv instance." << endl; } | |
void show(void) { cout << "[D]" << a << " " << b << endl; } | |
virtual void vshow(void) { cout << "[D]" << a << " " << b << endl; } | |
}; | |
int main() | |
{ | |
Derv d = Derv(1, 2, 3); | |
d.show(); | |
d.vshow(); | |
cout << "--------------" << endl; | |
Base *pBase = new Derv(1,2,3); | |
pBase->show(); | |
pBase->vshow(); | |
delete pBase; | |
cout << "--------------" << endl; | |
pBase = &d; | |
pBase->show(); | |
pBase->vshow(); | |
cout << "--------------" << endl; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment