// C++_test1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<string>
#include<cctype>
#include<fstream>
// using namespace std;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
//基类
class BaseClass {
public:
BaseClass() {}
void f(char*s = "unknown") {
cout << "Function f() in BaseClass called from " << s << endl;
h();
}
protected:
void g(char*s = "unknown") {
cout << "Function g() in BaseClass is called from " << s << endl;
}
private:
void h() {
cout << "Function h() in BaseClass\n";
}
};
//派生类
class Derived1Level1 :public virtual BaseClass {
public:
void f(char*s = "unknown") {
cout << "Function f() in Derived1Level1 is called from " << s << endl;
g("Derived1Level1");
h("Derived1Level1");
}
void h(char*s = "unknown") {
cout << "Function h() in Derived1Level1 is called from " << s << endl;
}
};
//派生类
class Derived2Level1 :public virtual BaseClass {
public:
void f(char*s = "unknown") {
cout << "Function f() in Derived2Level1 is called from " << s << endl;
g("Derived2Level1");
}
};
class Derived2Level2 :public Derived1Level1, public Derived2Level1 {
public:
void f(char*s = "unknown") {
cout << "Function f() in Derived2Level2 is called from " << s << endl;
g("Derived2Level2");
Derived1Level1::h("Derived2Level2");
BaseClass::f("Derived2Level2");
}
};
int main() {
BaseClass bc;
Derived1Level1 d1ll;
Derived2Level1 d2ll;
Derived2Level2 d22;
bc.f("main(1)");
d1ll.f("main(2)");
d1ll.h("main(3)");
d2ll.f("main(4)");
d22.f("main(5)");
d22.h();
//暂停,供用户按下enter
// system("pause");
cin.ignore();
return 0;
}