代码:
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
class TDshape{
public:
virtual void area() = 0;
virtual void printName() = 0;
};
class Triangle:public TDshape{
public:
void getWidth(){
cout<<"三角形的底是:"<<this->width<<"三角形的高是:"<<this->height<<endl;
}
void setWidth(double wid, double hei){
this->width = wid;
this->height = hei;
}
void area(){
cout<<"三角形面积是:"<<this->width*this->height/2<<endl;
}
void printName(){
cout<<"Trangle"<<endl;
}
double width,height;
};
class Rectangle:public TDshape{
public:
void getWidth(){
cout<<"长方形的长是:"<<this->width<<"长方形的高是:"<<this->height<<endl;
}
void setWidth(int wid,int hei){
this->width = wid;
this->height = hei;
}
void area(){
cout<<"长方形的面积是:"<<this->width*this->height<<endl;
}
void printName(){
cout<<"Rectangle"<<endl;
}
double width,height;
};
int main(){
Triangle t;
t.setWidth(20,10);
t.area();
t.getWidth();
cout<<"----------------" <<endl;
Rectangle r;
r.setWidth(20,20);
r.area();
r.getWidth();
return 0;
}