# C++抽象类（看代码哪里出错了、多谢！）

www.MyException.Cn  网友分享于：2013-04-27  浏览：5次
C++抽象类（看代码哪里出错了、谢谢！）
#include "stdafx.h"
#include <iostream.h>

class Shape
{ public:

virtual double Area( int ) const=0;
virtual int Get_N(void) { return 0;}
virtual ~Shape() { cout<<"desturctor of Shape is called!\n";}
};
//------------------------
class Polygon:public Shape
{
public:
virtual double Area(int) const=0;
};
//--------------------------
class Triangle:public Polygon

public:
Triangle(int n);
double Area(int i) const {return pB[i]*pH[i]*0.5;}
int Get_N(void) {return nT;}
~Triangle()
{
delete []pB;
delete []pH;
cout<<"destructor of Trangle is called!\n";
}
private:
double *pB,*pH;
int nT;
};
//--------------------------
Triangle::Triangle(int n)
{ int i;
nT=n;
pB=new double[n];
pH=new double[n];
cout<<"input bottom and high.\n";
for (i=0;i<n;i++)
cin>>pB[i]>>pH[i];
}

class Rectangle:public Polygon
{ public:
Rectangle( int n);
double Area( int i) const { return pH[i]*pW[i];}
int Get_N(void ) { return nR;}
~Rectangle()
{ delete []pW;
delete []pH;
cout<<"destructor of Rectangle is called !\n";
}
private:
double *pW,*pH;
int nR;
};
//-------------------------
Rectangle::Rectangle(int n)
{ int i;
nR=n;
pH=new double[n];
pW=new double[n];
cout<<"input length and wide.\n";
for (i=0;i<n;i++)
cin>>pH[i]>>pW[i];
}

class Circle:public Shape
{ public:
Circle::Cricle(int n);
double Area(int i) const {return pC[i]*pC[i]*3.14;}
int Get_N(void ) {return nC;}
~Circle()
{ delete[] pC;
cout<<"destructor of Circle is called!\n";
}
private:
double *pC;
int nC;
};
//-----------------------------------
Circle::Circle( int n)
{ int i;
nC=n;
pC=new double[n];
for (i=0;i<n;i++)
cin>>pC[i];
}
class Application
{ public:
double Compute(Shape *s[],int n) const ;
};
//------------------------------------------
double Application::Compute(Shape *s[],int n) const
{ double sum=0;
int n1=0;
for (int i=0;i<n;i++)
{ n1=s[i]->Get_N();
for(int k=0;k=n1;k++)
{
sum+=s[i]->Area(k);
cout<<"sum="<<sum<<"\tarea="<<s[i]->Area(k)<<endl;
}
}
return sum;
}

class Myprogarm:public Application
{ public:
Myprogarm();
~Myprogarm();
int Run();
private:
Shape **s;

};
//-------------------------------------------
Myprogarm::Myprogarm()
{ s=new Shape *[3];
int num=0;

cout<<"input the number of triangles.\n";
cin>>num;
s[0]=new Triangle(num);

cout<<"input the number of rectangles.\n";
cin>>num;

s[1]=new Rectangle(num);
cout<<" input the number of circles .\n";
cin>>num;

s[2]=new Circle(num);
}
int Myprogram::Run()
{ double sum=Compute(s,3);
cout<<sum<<endl;
return 0;
}
//--------------------------------------------
Myprogram::~Myprogram()
{ int i;
for(i=0;i<3;i++)
delete s[i];
delete []s;
}
//------------------------------------------
void main()
{

return Myprogram().Run();

}
//---------------------------------------------

------解决方案--------------------

C/C++ code
```
#include "stdafx.h"
#include <iostream.h>

class Shape
{ public:
virtual double Area( int ) const=0;
virtual int Get_N(void) { return 0;}
virtual ~Shape() { cout<<"desturctor of Shape is called!\n";}
};
//------------------------
class Polygon:public Shape
{
public:
virtual double Area(int) const=0;
};
//--------------------------
class Triangle:public Polygon
{
public:
Triangle(int n);
double Area(int i) const {return pB[i]*pH[i]*0.5;}
int Get_N(void) {return nT;}
~Triangle()
{
delete []pB;
delete []pH;
cout<<"destructor of Trangle is called!\n";
}
private:
double *pB,*pH;
int nT;
};
//--------------------------
Triangle::Triangle(int n)
{
int i;
nT=n;
pB=new double[n];
pH=new double[n];
cout<<"input bottom and high.\n";
for (i=0;i<n;i++)
cin>>pB[i]>>pH[i];
}

class Rectangle:public Polygon
{
public:
Rectangle( int n);
double Area( int i) const { return pH[i]*pW[i];}
int Get_N(void ) { return nR;}
~Rectangle()
{
delete []pW;
delete []pH;
cout<<"destructor of Rectangle is called !\n";
}
private:
double *pW,*pH;
int nR;
};
//-------------------------
Rectangle::Rectangle(int n)
{
int i;
nR=n;
pH=new double[n];
pW=new double[n];
cout<<"input length and wide.\n";
for (i=0;i<n;i++)
cin>>pH[i]>>pW[i];
}

class Circle:public Shape
{
public:
Circle(int n);
double Area(int i) const {return pC[i]*pC[i]*3.14;}
int Get_N(void ) {return nC;}
~Circle()
{
delete[] pC;
cout<<"destructor of Circle is called!\n";
}
private:
double *pC;
int nC;
};
//-----------------------------------
Circle::Circle( int n)
{
int i;
nC=n;
pC=new double[n];
for (i=0;i<n;i++)
cin>>pC[i];
}
class Application
{
public:
double Compute(Shape *s[],int n) const ;
};
//------------------------------------------
double Application::Compute(Shape *s[],int n) const
{
double sum=0;
int n1=0;
for (int i=0;i<n;i++)
{
n1=s[i]->Get_N();
for(int k=0;k=n1;k++)
{
sum+=s[i]->Area(k);
cout<<"sum="<<sum<<"\tarea="<<s[i]->Area(k)<<endl;
}
}
return sum;
}

class Myprogarm:public Application
{
public:
Myprogarm();
~Myprogarm();
int Run();
private:
Shape **s;
};
//-------------------------------------------
Myprogarm::Myprogarm()
{
s=new Shape *[3];
int num=0;

cout<<"input the number of triangles.\n";
cin>>num;
s[0]=new Triangle(num);

cout<<"input the number of rectangles.\n";
cin>>num;

s[1]=new Rectangle(num);
cout<<" input the number of circles .\n";
cin>>num;

s[2]=new Circle(num);
}

int Myprogarm::Run()
{
double sum=Compute(s,3);
cout<<sum<<endl;
return 0;
}
//--------------------------------------------
Myprogarm::~Myprogarm()
{
int i;
for(i=0;i<3;i++)
delete s[i];
delete []s;
}
//------------------------------------------
void main()
{

Myprogarm().Run();

}```