一道C++编程题?
你这个目录有点多,我没有写全,所以运行结果如下图,你可以自己在代码里补全。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Title{//标题的基类
protected:
string no;//序号
string name;//名称
public:
Title(string no1,string name1){
no=no1;
name=name1;
}
virtual void Print()=0;//打印本级标题 纯虚函数
};
class FirstTitle:public Title{//一级标题
public:
FirstTitle(string no1,string name1):Title(no1,name1){}
void Print(){
cout<<no<<" "<<name;
}
};
class SecondTitle:public Title{//二级标题
public:
SecondTitle(string no1,string name1):Title(no1,name1){}
void Print(){
cout<<" "<<no<<" "<<name;
}
};
class ThirdTitle:public Title{//三级标题
public:
ThirdTitle(string no1,string name1):Title(no1,name1){}
void Print(){
cout<<" "<<no<<" "<<name;
}
};
int main(int argc, const char * argv[]) {
vector<Title*> titles;
Title *two=new FirstTitle("2","数据类型及表达式");
Title *two_one=new SecondTitle("2.1","C++的数据类型"),*two_two=new SecondTitle("2.2","常量"),*two_three=new SecondTitle("2.3","变量"),*two_four=new SecondTitle("2.4","C++的运算符");
Title *two_two_one=new ThirdTitle("2.2.1","什么是常量"),*two_two_two=new ThirdTitle("2.2.2","数值常量");
//....
titles.push_back(two);
titles.push_back(two_one);
titles.push_back(two_two);titles.push_back(two_two_one);titles.push_back(two_two_two);
titles.push_back(two_three);
titles.push_back(two_four);
//....
for(int i=0;i<titles.size();i++){
titles[i]->Print();
cout<<endl;
}
return 0;
}