自考新教材-p255

使用多态处理图形示例

源程序:

#include <iostream>

#include <cmath>

using namespace std;

class CShape

{

protected:

  double acreage;

public:

  CShape()

   {

    //cout<<"基类构造函数"<<endl;

  };

   virtual ~CShape()

   {

    //cout<<"基类析构函数"<<endl;

  }

  virtual double CalAcr()

  {

    return 0;

   };

   virtual void setAcreage(double acre){};

   virtual void PrintInfo(){};

};

class CRectangle:public CShape

{

   double width,high;

public:

   CRectangle(double w,double h)

  {

     //cout<<"矩形带参构造函数"<<endl;

     width = w;

     high = h;

  };

  CRectangle()

  {

    //cout<<"矩形无参构造函数"<<endl;

    width = 0;

     high = 0;

   };

   ~CRectangle()

  {

    //cout<<"矩形析构函数"<<endl;

  }

  virtual double CalAcr();

  virtual void PrintInfo();

  virtual void setAcreage(double);

};

class CCircle:public CShape

{

  double radius;

public:

  CCircle(double r)

  {

  //cout<<"圆形带参构造函数"<<endl;

  radius = r;

  };

  CCircle()

  {

    //cout<<"圆形无参构造函数"<<endl;

    radius = 0;

  };

  ~CCircle()

  {

    //cout<<"圆形析构函数"<<endl;

  }

  virtual double CalAcr();

  virtual void PrintInfo();

  virtual void setAcreage(double);

};

class CTriangle:public CShape

{

  double a,b,c;

public:

  CTriangle(double a,double b,double c)

   {

    //cout<<"三角形带参构造函数"<<endl;

    this->a=a;

    this->b=b;

    this->c=c;

  };

  CTriangle()

  {

    //cout<<"三角形无参构造函数"<<endl;

    a=0;

    b=0;

    c=0;

  };

  ~CTriangle()

   {

    //cout<<"三角形析构函数"<<endl;

  };

   virtual double CalAcr();

   virtual void PrintInfo();

  virtual void setAcreage(double);

};

double CRectangle::CalAcr()

{

   return width*high;

}

void CRectangle::PrintInfo()

{

   cout<<"矩形.\t 宽度="<<this->width<<",高度 ="<<this->high<<", 面积"<<this->acreage<<endl;

}

void CRectangle::setAcreage(double acre)

{

  acreage = acre;

}

double CCircle::CalAcr()

{

  return 3.14*radius*radius;

}

void CCircle::PrintInfo()

{

  cout<<"圆.\t 半径="<<this->radius<<",面积"<<this->acreage<<endl;

}

void CCircle::setAcreage(double acre)

{

  acreage = acre;

}

double CTriangle::CalAcr()

{

  double p = (a+b+c)/2.0;

  return sqrt(p*(p-a)*(p-b)*(p-c));

}

void CTriangle::PrintInfo()

{

  cout<<"三角形.三条边分别是: "<<this->a<<","<<this->b<<","<<this->c<<",面积="<<this->acreage<<endl;

}

void CTriangle::setAcreage(double acre)

{

  acreage = acre;

}

CShape * pShapes[100];

int main()

{

  int i,n;

   double temp1,temp2,temp3;

   CRectangle *pr;CCircle *pc;CTriangle *pt;

   cin>>n;

   for(i=0;i<n;++i)

   {

    char c;

     cin>>c;

    switch(c)

     {

    case‘R‘:case‘r‘:

      cin>>temp1>>temp2;

      pr=new CRectangle(temp1,temp2);

      pr->setAcreage(pr->CalAcr());

      pShapes[i]=pr;

      break;

    case‘C‘:case‘c‘:

      cin>>temp1;

      pc=new CCircle(temp1);

      pc->setAcreage(pc->CalAcr());

      pShapes[i]=pc;

       break;

    case‘T‘:case‘t‘:

      cin>>temp1>>temp2>>temp3;

      pt=new CTriangle(temp1,temp2,temp3);

      pt->setAcreage(pt->CalAcr());

      pShapes[i]=pt;

      break;

    }

  }

  if(n==1)

    cout<<"共有 "<<n<<"种图形,它是"<<endl;

  else

    cout<<"共有 "<<n<<"种图形,分别是"<<endl;

  for(i=0;i<n;++i)

   {

    //cout<<"1"<<endl;

    pShapes[i]->PrintInfo();

    delete pShapes[i];

   }

   system("pause");

  return 0;

}

运行结果:

原文地址:https://www.cnblogs.com/duanqibo/p/12183280.html

时间: 2024-10-21 00:38:03

自考新教材-p255的相关文章

自考新教材-p161

源程序: #include<iostream>using namespace std; class pointer{public: int a; int *p; //指向整型数的指针 pointer() { a=100; p=new int(10); } pointer(const pointer &tempp) //复制构造函数 { if(this != &tempp) { a=tempp.a; p=new int(); *p=*tempp.p; } } ~pointer()

自考新教材--p40

源程序:   各种数据类型的转换 #include <iostream> using namespace std; int main() { const int cInt = 30; int oneInt = 50; int &ref = oneInt; const int &rc1 = cInt; const int &rc2 = oneInt; const int &rc3 = ref; int dInt = ref; int eInt = cInt; in

自考新教材--p39

源程序: using namespace std; int main() { int oneInt = 1; int &ref = oneInt; const int &refc = oneInt;  //定义常引用 ref = 2;  //修改ref也即修改了oneInt cout << "oneInt=" << oneInt << "," << "ref=" << r

自考新教材--p49

源程序: #include <iostream> #define N 5 using namespace std; void insert_sort(int a[], int n)   //直接插入排序 { int i, j, temp; for (i = 1; i < n; i++) { temp = a[i]; j = i - 1; while (j >= 0 && temp < a[j]) { a[j + 1] = a[j]; j--; } a[j +

自考新教材--p179

源程序: #include <iostream> using namespace std; class BaseClass { public: int v1, v2; BaseClass() { v1 = 1; v2 = 1; } int temp1() {} }; class DerivedClass :public BaseClass { int v1; int temp1() {} public: DerivedClass() { v1 = 10; } void printv() { c

自考新教材-p250

用基类指针访问基类对象及派生类对象 源程序: #include <iostream> #include <string> using namespace std; class A { public: void put_name(string s) { name = s; } virtual void print_name() const { cout << "A::" << name << "\n"; }

自考新教材-p252

基类引用实现多态 源程序: #include <iostream> using namespace std; class A { public: virtual void Print() { cout << "A::Print" << endl; } }; class B :public A { public: virtual void Print() { cout << "B::print" << end

自考新教材-p253

多态机制下对象存储空间的大小 源程序: #include <iostream> using namespace std; class A { public: int i; virtual void func(); virtual void func2(); }; class B :public A { int j; void func() {} }; int main() { cout << sizeof(A) << "," << siz

自考新教材-p228

互包含的类 源程序: #include<iostream> #include<string> using namespace std; class B; class A { public: int aInt; B *bPoint=NULL; void SetValue(int v) { aInt=v; } }; class B { public: A aCla; int bInt; void SetValue(int v) { bInt=v; } }; int main() { A