如下:
#include <iostream>
using namespace std;
class CShape
{
public:
virtual void printArea() = 0;
void printMe() { cout << "I am CShape" << endl; }
};
class CCircle :public CShape
{
private:
double radius;
public:
CCircle(double r) { radius = r; }
void printArea() { cout <<"面积:"<< 3.1415926 * radius * radius; }
void printMe() { cout << "I am CCircle" << endl; }
};
class CRectangle :public CShape
{
private:
double length, width;
public:
CRectangle(double l, double w) { length = l; width = w; }
void printArea() { cout << "面积:" << length*width; }
void printMe() { cout << "I am CRectangle" << endl; }
};
int main()
{
CCircle c(2);
c.printMe();
c.printArea();
CRectangle r(3, 4);
r.printMe();
r.printArea();
return 0;
}
#include<iostream>
#define PI 3.14
using namespace std;
class CShape
{
public:
virtual void printArea() = 0;
static void printMe()
{
cout << "T am CShape" << endl;
}
};
class CCircle :public CShape
{
public:
CCircle(float n)
{
this->r = n;
}
void printMe()
{
cout << "T am CCircle" << endl;
}
void printArea()
{
cout << this->r*this->r*PI << endl;
}
private:
float r;
};
class CRectangle :public CShape
{
public:
CRectangle(float l, float w)
{
this->lenth = l;
this->width = w;
}
void printMe()
{
cout << "I an CRectangle" << endl;
}
void printArea()
{
cout << this->lenth*this->width << endl;
}
private:
float lenth;
float width;
};
int main()
{
CShape::printMe();
CCircle c(9.9);
c.printMe();
c.printArea();
CRectangle r(3, 4);
r.printMe();
r.printArea();
return 0;
}