作业6.12
#include<iostream>
#include<string>
using namespace std;
class area_c1{
public:
area_c1(double r,double s)
{
height=r;
width=s;
}
virtual double area()=0;
protected:
double height;
double width;
};
class rectangle:public area_c1{
public:
rectangle(double r,double s):area_c1(r,s)
{};
double area()
{
cout<<"矩形面积为:"<<height*width<<endl;
return height*width;
}
};
class isosceles:public area_c1{
public:
isosceles(double r,double s):area_c1(r,s)
{};
double area()
{
cout<<"三角形面积为:"<<height*width*0.5<<endl;
return height*width*0.5;
}
};
int main()
{
area_c1 *p;
rectangle c2(10.0,5.0);
isosceles c3(4.0,6.0);
p=&c2;
p->area();
p=&c3;
p->area();
system("pause");
return 0;
}
作业6.13
#include<iostream>
#include<string>
using namespace std;
class Base{
public:
Base(double i)
{
h=i;
}
virtual void disp()
{
cout<<"显示体积"<<endl;
}
protected:
double h;
};
class Cuboid:public Base{
public:
Cuboid(double i,double w,double l):Base(i)
{
wide=w;
length=l;
}
void disp()
{
cout<<"长方体的体积为:"<<h*wide*length<<endl;
}
private:
double wide,length;
};
class Cylinder:public Base{
public:
Cylinder(double i,double r):Base(i)
{
radius=r;
}
void disp()
{
cout<<"圆柱体的体积为:"<<3.14*radius*radius*h<<endl;
}
private:
double radius;
};
int main()
{
Base *p;
Cuboid cu1(10.0,5.0,2.5);
Cylinder cy1(4.0,6.0);
p=&cu1;
p->disp();
p=&cy1;
p->disp();
system("pause");
return 0;
}
作业6.14
#include<iostream>
#include<string>
using namespace std;
class container{
public:
container(double radius1)
{
radius=radius1;
}
virtual double surface_area()=0;
virtual double volume()=0;
protected:
double radius;
};
class cube:public container{
public:
cube(double radius1):container(radius1)
{};
double surface_area()
{
double s1=radius*radius*6;
cout<<"正方体的表面积为:"<<s1<<endl;
return s1;
}
double volume()
{
double v1=radius*radius*radius;
cout<<"正方体的体积为:"<<v1<<endl;
return v1;
}
};
class sphere:public container{
public:
sphere(double radius1):container(radius1)
{};
double surface_area()
{
double s2=4*3.14*radius*radius;
cout<<"圆体的表面积为:"<<s2<<endl;
return s2;
}
double volume()
{
double v2=(4/3)*3.14*radius*radius*radius;
cout<<"圆体的体积为:"<<v2<<endl;
return v2;
}
};
class cylinder:public container{
public:
cylinder(double radius1,double high1):container(radius1)
{
h=high1;
};
double surface_area()
{
double s3=(3.14*radius*radius*2)+(3.14*2*radius*h);
cout<<"圆柱体的表面积为:"<<s3<<endl;
return s3;
}
double volume()
{
double v3=3.14*radius*radius*h;
cout<<"圆柱体的体积为:"<<v3<<endl;
return v3;
}
private:
double h;
};
int main()
{
container *p;
cube cu1(6.0);
sphere sp1(5.0);
cylinder cy1(5.0,6.0);
p=&cu1;
p->surface_area();
p->volume();
p=&sp1;
p->surface_area();
p->volume();
p=&cy1;
p->surface_area();
p->volume();
system("pause");
return 0;
}