定义一个基类Shape,在次基础上派生出Rectangle和Circle。
要求:定义一个基类Shape,在次基础上派生出Rectangle和Circle。,二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Sq...
要求:
定义一个基类Shape,在次基础上派生出Rectangle和Circle。,二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square.
情况:我只能做到下面这些了,不知道怎么用Rectangle类创建一个派生类。。希望高手们帮我完成一下这个程序。只有两天时间了。
#include<iostream.h>
#include<math.h>
#define pi 3.14
class shape
{
public:
virtual float area()=0;
};
class circle:public shape
{
private:
float r;
public:
circle(float r1)
{
r=r1;
}
float area()
{
return (float)pi*r*r;
}
};
class rectangle:public shape
{
private:
float width,height;
public:
rectangle(float w1,float h1)
{
width=w1;height=h1;
}float area()
{
return width*height;
}
};
float total(shape*s[],int n)
{
float sum=0.0;
for(int i=0;i<n;i++)
sum+=s[i]->area();
return sum;
}
void main()
{
shape* s[2];
s[0]=new circle(1);
cout<<s[0]->area()<<endl;
s[1]=new rectangle(2,4);
cout<<s[1]->area()<<endl;
cout<<total(s,2)<<endl;
} 展开
定义一个基类Shape,在次基础上派生出Rectangle和Circle。,二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square.
情况:我只能做到下面这些了,不知道怎么用Rectangle类创建一个派生类。。希望高手们帮我完成一下这个程序。只有两天时间了。
#include<iostream.h>
#include<math.h>
#define pi 3.14
class shape
{
public:
virtual float area()=0;
};
class circle:public shape
{
private:
float r;
public:
circle(float r1)
{
r=r1;
}
float area()
{
return (float)pi*r*r;
}
};
class rectangle:public shape
{
private:
float width,height;
public:
rectangle(float w1,float h1)
{
width=w1;height=h1;
}float area()
{
return width*height;
}
};
float total(shape*s[],int n)
{
float sum=0.0;
for(int i=0;i<n;i++)
sum+=s[i]->area();
return sum;
}
void main()
{
shape* s[2];
s[0]=new circle(1);
cout<<s[0]->area()<<endl;
s[1]=new rectangle(2,4);
cout<<s[1]->area()<<endl;
cout<<total(s,2)<<endl;
} 展开
1个回答
展开全部
#include <iostream>//
#include <cmath>//
#define pi 3.14
using namespace std;
class shape
{
public:
virtual float area()=0;
};
class circle:public shape
{
private:
float r;
public:
circle(float r1)
{
r=r1;
}
float area()
{
return (float)pi*r*r;
}
};
class rectangle:public shape
{
private:
float width,height;
public:
rectangle(float w1,float h1)
{
width=w1;height=h1;
}
float area()
{
return width*height;
}
};
class square : public rectangle//其实是一样的,把rectangle当基类
{
public:
square(float len):rectangle(len,len){};
~square(){};
float area(float len)
{
return len * len;
};
};
float total(shape*s[],int n)
{
float sum=0.0;
for(int i=0;i<n;i++)
sum+=s[i]->area();
return sum;
}
int main()
{
shape* s[2];
s[0]=new circle(1);
cout<<s[0]->area()<<endl;
s[1]=new rectangle(2,4);
cout<<s[1]->area()<<endl;
s[ 2 ] = new square( 3 );//类似照写
cout << s[2]->area() << endl;//
cout << total( s, 3 ) << endl;//改为3
for( int i = 0; i < 3; i++ )//释放
{//
delete [] s[ i ];//
}//
system( "pause" );
return 0;
}
#include <cmath>//
#define pi 3.14
using namespace std;
class shape
{
public:
virtual float area()=0;
};
class circle:public shape
{
private:
float r;
public:
circle(float r1)
{
r=r1;
}
float area()
{
return (float)pi*r*r;
}
};
class rectangle:public shape
{
private:
float width,height;
public:
rectangle(float w1,float h1)
{
width=w1;height=h1;
}
float area()
{
return width*height;
}
};
class square : public rectangle//其实是一样的,把rectangle当基类
{
public:
square(float len):rectangle(len,len){};
~square(){};
float area(float len)
{
return len * len;
};
};
float total(shape*s[],int n)
{
float sum=0.0;
for(int i=0;i<n;i++)
sum+=s[i]->area();
return sum;
}
int main()
{
shape* s[2];
s[0]=new circle(1);
cout<<s[0]->area()<<endl;
s[1]=new rectangle(2,4);
cout<<s[1]->area()<<endl;
s[ 2 ] = new square( 3 );//类似照写
cout << s[2]->area() << endl;//
cout << total( s, 3 ) << endl;//改为3
for( int i = 0; i < 3; i++ )//释放
{//
delete [] s[ i ];//
}//
system( "pause" );
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询