Created
August 29, 2018 13:46
-
-
Save OctoberWu/e07d1332436e4ba7a5c2c6606be919dd to your computer and use it in GitHub Desktop.
#virtual_function #OOP
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
//============================================================================ | |
// Name : virtualFunc.cpp | |
// Author : octoberwu | |
// Version : | |
// Copyright : Your copyright notice | |
// Description : Hello World in C++, Ansi-style | |
//============================================================================ | |
#include <iostream> | |
using namespace std; | |
class A | |
{ | |
public: | |
A(){ pa = new char[1]; cout << "new A" << endl;}; | |
virtual ~A(){delete[] pa; cout << "delete A" << endl;}; | |
virtual void f(){ cout << "A::f()" << endl;}; | |
void g(){ cout << "A::g()" << endl;}; | |
private: | |
char* pa; | |
}; | |
class B : public A | |
{ | |
public: | |
B(){ pb = new char[1]; cout << "new B" << endl;} | |
~B(){delete[] pb; cout << "delete B" << endl;} | |
void f(){ cout << "B::f()" << endl;}; | |
void g(){ cout << "B::g()" << endl;}; | |
private: | |
char* pb; | |
}; | |
int main() { | |
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!! | |
/* | |
A Apple; | |
A.g(); | |
B Baby; | |
Baby.f(); | |
Baby.g(); | |
*/ | |
/* | |
A* p_a = new B; | |
p_a->f(); | |
p_a->g(); | |
delete p_a; | |
*/ | |
B* p_b = new B; | |
p_b->f(); | |
p_b->g(); | |
delete p_b; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment