1.
#include <iostream.h>
class Point
{public:
Point(float x=0,float y=0);
void setPoint(float,float);
float getX() const
{return x;}
float getY() const
{return y;}
friend ostream & operator<<(ostream &output,Point &);
protected:
float x,y;
};
Point::Point(float a,float b)
{
x=a;y=b;
}
void Point::setPoint(float a,float b)
{
x=a;y=b;
}
ostream & operator<<(ostream &output,Point &p)
{
output<<"["<<p.x<<","<<p.y<<"]"<<endl;
return output;
}
class Circle:public Point
{
public:
Circle(float x,float y,float r):Point(x,y),radius(r){}
void setRadius(float r)
{radius=r;}
float getRadius() const
{return radius;}
double area() const
{
return 3.1415*radius*radius;
}
friend ostream & operator<<(ostream &output,Circle &);
protected:
float radius;
};
ostream & operator<<(ostream &output,Circle &c)
{
output<<"Center =["<<c.x<<","<<c.y<<"],r= "<<c.radius<<",area="<<c.area()<<endl;
return output;
}
class Cylinder:public Circle
{
public:
Cylinder(float x,float y,float r,float h):Circle(x,y,r),height(h){}
void setHeight(float h)
{height=h;}
float getHeight() const
{return height;}
double area() const
{
return 2*Circle::area()+2*3.1415*radius*height;
}
double volume() const
{
return 3.1415*radius*radius*height;
}
friend ostream & operator<<(ostream &output,Cylinder &);
protected:
float height;
};
ostream & operator<<(ostream &output,Cylinder &cy)
{
output<<"Center=["<<cy.x<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height<<"\narea="<<cy.area()<<",volume="<<cy.volume()<<endl;
return output;
}
int main()
{
Cylinder cy1(1.1,6.4,6.1,11);
cout<<"original cylinder:\nx="<<cy1.getX()<<",y="<<cy1.getY()<<",r="<<cy1.getRadius()<<",h="<<cy1.getHeight()<<"\narea="<<cy1.area()<<",volume="<<cy1.volume()<<endl;
cy1.setHeight(10);
cy1.setRadius(7.5);
cy1.setPoint(5,5);
cout<<"\nnew cylinder:\n"<<cy1;
Point &pRef=cy1;
cout<<"\npRef as a point:"<<pRef;
Circle &cRef=cy1;
cout<<"\ncRef as a circle:"<<cRef;
return 0;
}
3.(1)
#include <iostream>
using namespace std;
class Point
{
public:
Point(float a,float b):x(a),y(b){}
~Point(){cout<<"executing Point destructor"<<endl;}
private:
float x;
float y;
};
class Circle:public Point
{
public:
Circle(float a,float b,float r):Point(a,b),radius(r){}
~Circle(){cout<<"executing Circle destructor"<<endl;}
private:
float radius;
};
int main()
{
Point *p=new Circle(1,2,3);
delete p;
return 0;
}
(2)
#include <iostream>
using namespace std;
class Point
{
public:
Point(float a,float b):x(a),y(b){}
~Point(){cout<<"executing Point destructor"<<endl;}
private:
float x;
float y;
};
class Circle:public Point
{
public:
Circle(float a,float b,float r):Point(a,b),radius(r){}
~Circle(){cout<<"executing Circle destructor"<<endl;}
private:
float radius;
};
int main()
{
Point *p=new Circle(1,2,3);
Circle *r=new Circle(1,2,3);
delete r;
return 0;
}
(3)
#include <iostream>
using namespace std;
class Point
{
public:
Point(float a,float b):x(a),y(b){}
virtual ~Point(){cout<<"executing Point destructor"<<endl;}
private:
float x;
float y;
};
class Circle:public Point
{
public:
Circle(float a,float b,float r):Point(a,b),radius(r){}
~Circle(){cout<<"executing Circle destructor"<<endl;}
private:
float radius;
};
int main()
{
Point *p=new Circle(1,2,3);
delete p;
return 0;
}
4.
#include <iostream.h>
class Shape{
public:
virtual double area()const =0;
};
class Circle:public Shape
{
public:
Circle(double r):radius(r){}
virtual double area() const
{
return 3.1415*radius*radius;
}
protected:
double radius;
};
class Rectangle :public Shape{
public:
Rectangle(double l,double w):length(l),width(w){}
virtual double area()const
{
return length*width;
}
protected:
double length;
double width;
};
class Triangle:public Shape
{
public:
Triangle(double d,double h):dibian(d),height(h){}
virtual double area() const
{
return 0.5*dibian*height;
}
protected:
double dibian;
double height;
};
void display(const Shape &s)
{
cout<<s.area()<<endl;
}
int main()
{
Circle c(2);
cout<<"area of c is ";
display(c);
Rectangle r(1,2);
cout<<"area of r is ";
display(r);
Triangle t(1,2);
cout<<"area of t is ";
display(t);
return 0;
}
5.
#include <iostream.h>
class Shape{
public:
virtual double area()const =0;
};
class Circle:public Shape
{
public:
Circle(double r):radius(r){}
virtual double area() const
{
return 3.1415*radius*radius;
}
protected:
double radius;
};
class Rectangle :public Shape{
public:
Rectangle(double l,double w):length(l),width(w){}
virtual double area()const
{
return length*width;
}
protected:
double length;
double width;
};
class Triangle:public Shape
{
public:
Triangle(double d,double h):dibian(d),height(h){}
virtual double area() const
{
return 0.5*dibian*height;
}
protected:
double dibian;
double height;
};
class Square:public Shape{
public:
Square(double s):side(s){}
virtual double area() const
{
return side*side;
}
protected:
double side;
};
class Trapezoid:public Shape
{
public:
Trapezoid(double t,double b,double h):top(t),bottem(b),height(h){}
virtual double area() const
{
return 0.5*(top+bottem)*height;
}
protected:
double top,bottem,height;
};
void display(const Shape &s)
{
cout<<s.area()<<endl;
}
int main()
{
Circle c(2);
Rectangle r(1,2);
Triangle t(1,2);
Square s(2);
Trapezoid tt(1,2,3);
Shape *p[5]={&c,&r,&t,&s,&tt};
double area=0.0;
for(int i=0;i<5;i++)
{
area=area+p[i]->area();
}
cout<<"the sum of area is "<<area<<endl;
return 0;
}