初步瞭解C++命名空間(加了繼承)

本篇只在上篇基礎上加了繼承機制,進一步看看命名空間的運行機制。如有不當,還請來者指正。

//file4.h

namespace A{
class One{
public:
virtual int fun();
};
}
namespace B{
class Two{
public:
void fun();
};
}
class Three{
public:
int fun();
};
class Four:public A::One{
public:
int fun();
};

//file4.cpp

#include <iostream>
#include "file4.h"
using namespace std;
int A::One::fun(){
return 1;
}
void B::Two::fun(){
cout<<"2"<<endl;
}
int Three::fun(){
return 3;
}
int Four::fun(){
return 4;
}

//main.cpp

#include <iostream>
#include "file4.h"
using namespace std;
using namespace A;
using namespace B;
int main(){
One one;
Two two;
Three three;
Four four;
cout<<one.fun()<<endl;
two.fun();
cout<<three.fun()<<endl;
cout<<four.fun()<<endl;
system("pause");
return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章