1.class Car:public Vehicle
{
private:
int zai,zhong;
public:
Car(string NO,int z,int l):Vehicle(NO),zai(z),zhong(l)
{}
int fee()
{
return zai*8+zhong*2;
}
};
class Truck:public Vehicle
{
private:
int zhong;
public:
Truck(string NO,int l):Vehicle(NO),zhong(l)
{}
int fee()
{
return zhong*5;
}
};
class Bus:public Vehicle
{
private:
int zai;
public:
Bus(string NO,int z):Vehicle(NO),zai(z)
{}
int fee()
{
return zai*3;
}
};
2.class Shape
{
public:
Shape()
{
}
virtual float area()=0;
virtual ~Shape()
{
}
};
class Circle:public Shape
{
private:
int x,y;
float radius;
public:
Circle(int X,int Y,float r):x(X),y(Y),radius(r)
{}
float area()
{
return 3.1415*radius*radius;
}
~Circle()
{
}
};
class Rectangle:public Shape
{
private:
int width,height;
public:
Rectangle(int w,int h):width(w),height(h)
{}
float area()
{
return width*height;
}
~Rectangle()
{
}
};
3.#include <iostream>
using namespace std;
class Person
{
public:
Person()
{
}
virtual void bellRing()=0;
virtual ~Person(){}
};
class Student:public Person
{
public:
Student()
{
}
void bellRing()
{
cout<<"I am a student learning in classroom."<<endl;
}
~Student()
{
cout<<"A student object destroyed."<<endl;
}
};
class Teacher:public Person
{
public:
Teacher()
{
}
void bellRing()
{
cout<<"I am a teacher teaching in classroom."<<endl;
}
~Teacher()
{
cout<<"A teacher object destroyed."<<endl;
}
};
class Principal:public Person
{
public:
Principal()
{
}
void bellRing()
{
cout<<"I am the principal inspecting in campus."<<endl;
}
~Principal()
{
cout<<"A principal object destroyed."<<endl;
}
};
4.
#include<iostream>
using namespace std;
class Shape
{
public:
Shape()
{
}
virtual float area()=0;
};
class Circle:public Shape
{
private:
float radus;
public:
Circle(float r):radus(r)
{}
float area()
{
return 3.1415926*radus*radus;
}
};
class Square:public Shape
{
private:
float bian;
public:
Square(float a):bian(a)
{}
float area()
{
return bian*bian;
}
};
class Rectangle:public Shape
{
private:
float width,length;
public:
Rectangle(float w,float l):width(w),length(l)
{}
float area()
{
return width*length;
}
};
class Trapezoid:public Shape
{
private:
float shang,xia,gao;
public:
Trapezoid(float s,float x,float g):shang(s),xia(x),gao(g)
{}
float area()
{
return (shang+xia)*gao/2;
}
};
class Triangle:public Shape
{
private:
float di,ha;
public:
Triangle(float d,float h):di(d),ha(h)
{}
float area()
{
return di*ha/2;
}
};
int main()
{
float a,b,c,d,e,f,g,h,i;
cin>>a>>b>>c>>d>>e>>f>>g>>h>>i;
Shape *shape[5]={new Circle(a),new Square(b),new Rectangle(c,d),
new Trapezoid(e,f,g),new Triangle(h,i)};
printf("total of all areas = %.3f\n",shape[0]->area()+shape[1]->area()+
shape[2]->area()+shape[3]->area()+shape[4]->area());
return 0;
}
5.
#include<iostream>
using namespace std;
class Shape
{
public:
Shape()
{
}
virtual float area()=0;
};
class Circle:public Shape
{
private:
float ra;
public:
Circle(float r):ra(r)
{}
float area()
{
return 3.14159*ra*ra;
}
};
class Rectangle:public Shape
{
private:
float width,length;
public:
Rectangle(float w,float l):width(w),length(l)
{}
float area()
{
return width*length;
}
};
class Triangle:public Shape
{
private:
float di,gao;
public:
Triangle(float d,float g):di(d),gao(g)
{}
float area()
{
return di*gao/2;
}
};
int main()
{
float a,b,c,d,e;
cin>>a>>b>>c>>d>>e;
Circle c1(a);
Rectangle r(b,c);
Triangle t(d,e);
printf("%.2f\n%.2f\n%.2f\n",c1.area(),r.area(),t.area());
return 0;
}