abstractABC.cpp
#include <iostream> #include <string> using namespace std; class DMA{ private: string label;//为了方便,我直接写出string类型了 int rating; public: DMA(const string l="null",int r=0){//虚基类也是有构造函数的 label=l; rating=r; }virtual void test()const=0{};//只是为了使其成为虚基类 virtual void tese2(){cout<<"test2";};//只有纯虚函数才要求派生类都实现,虚函数不一定要实现 DMA(const DMA &rs) { label=rs.label; rating=rs.rating; } virtual ~DMA()//const=0 {}; /* DMA &operator=(const DMA &rs)//赋值运算符也是不能被继承的 { return DMA(rs);成为了虚基类之后,就不能创建对象了 }*/ string lab(){return label;}; int ra(){return rating;}; friend ostream&operator<<(ostream &os,const DMA &rs)//const=0//友元函数是不能被继承的, { //但是可以通过强制指针转换来调用 os<<"label:"<<rs.label<<" rating:"<<rs.rating<<endl; return os; } virtual void show() { cout<<"label:"<<label<<" rating:"<<rating<<" "<<endl; } }; class baseDMA:public DMA { public: baseDMA(const string l="null",int r=0):DMA(l,r) {} baseDMA(baseDMA &bd):DMA(bd.lab(),bd.ra())//不能用const, { } //baseDMA 赋值我就不写了,原理都是差不多的 virtual void test()const{};//必须实现虚基类的所有虚函数 virtual ~baseDMA(){} friend ostream&operator<<(ostream &os,const baseDMA &bd) { os<<"This is baseDMA: "; os<<(DMA &)bd;//通过强制类型转换调用基类友元函数 return os; } virtual void show() { cout<<"This is baseDMA: "; DMA::show(); cout<<endl; } }; class lacksDMA:public DMA { private: //enum{COL} string color; public: lacksDMA(const string c="blank",const string l="null",int r=0):DMA(l,r) { color=c; } virtual ~lacksDMA(){}; virtual void test()const{};//必须实现虚基类的所有虚函数 friend ostream&operator<<(ostream &os,const lacksDMA &ld) { os<<"This is lacksDMA: "; os<<(DMA &)ld<<" color:"<<ld.color;//通过强制类型转换调用基类友元函数 return os; } virtual void show() { cout<<"This is lacksDMA: "; DMA::show(); cout<<" color:"<<color; cout<<endl; } }; class hasDMA:public DMA { private: string style; public: hasDMA(const string s="none",const string l="null",int r=0):DMA(l,r) { style=s; } virtual ~hasDMA(){}; virtual void test()const{};//必须实现虚基类的所有虚函数 virtual void show() { cout<<"This is hasDMA: "; DMA::show(); cout<<" style:"<<style; cout<<endl; } friend ostream&operator<<(ostream &os,const hasDMA &hd) { os<<"This is hasDMA: "; os<<(DMA &)hd<<" style:"<<hd.style;//通过强制类型转换调用基类友元函数 return os; } };
main133.cpp
#include <iostream> #include "abstractABC.cpp" using namespace std; void main133() { DMA *pd[3];//虚基类不能创建对象,但可以创建指向其的指针 for(int i=0;i<3;i++) { cout<<"\nEnter the label:"; string label; getline(cin,label,'\n'); cout<<"\nEnter the rating:"; int rat; cin>>rat; cout<<"Enter the 1 for baseDMA"<<endl <<"2 for lacksDMA"<<endl <<"3 for hasDMA"<<endl; int temp; cin>>temp; cin.get();//吸收掉回车符,否则下面的geiline会直接读取这一个回车符 if(temp==1) pd[i]=new baseDMA(label,rat);//如果不实现虚基类的所有纯虚函数,将无法创建该类的实例化 else if(temp==2) { cout<<"Enter the color:"; string color; getline(cin,color); pd[i]=new lacksDMA(color,label,rat); } else if(temp==3) { cout<<"Enter the style:"; string style; getline(cin,style); pd[i]=new hasDMA(style,label,rat); } else { cout<<"invalid input! try again!"<<endl; i--; } while(cin.get()!='\n') continue; //getline(label,100,); //cin>>label; } cout<<endl; for(int i=0;i<3;i++) pd[i]->show(); //cout<<*pd[i];这样,都只是调用了DMA的友元函数 system("pause"); }
——————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:https://www.royalchen.com/
author:royalchen
Email:royalchen@royalchen.com
———————————————————————————————————————————————————