【C++】实验十三

题目:

1、声明Point(点)类,由Point类派生出Circle(圆)类,再由Circle类派生出Cylinder(圆柱)类。将类的定义部分分别作为3个头文件。对它们的成员函数的声明部分分别作为3个源文件(.cpp文件),在主函数中用#include命令把它们包含进来。形成一个完整的程序,并上机运行。

2、教材第12章例12.3的基础上作以下修改。

*把构造函数修改为带参数的函数,在建立对象时初始化。

*先不要析构函数声明virtual,在main函数中另设一个指向Circle类对象的指针变量。使它指向gradl。运行程序,分析结果。

*不作第2点的修改而将析构函数声明为virtual,运行程序,分析结果。

3、声明抽象基类shape, 由它派生出3个派生类:Circle(圆形)、Rectangle(矩形)、Triangle(三角形),用一个函数printArea分别输出以上三者的面积,3个图形的数据在定义对象时给定。

参考代码:

1、

#ifndef _Circle_H//Circle.h
#define _Circle_H
#include<iostream.h>
class Circle:public Point{
protected:
    double radius;
public:
       Circle(double =0,double =0,double =0);
friend ostream & operator<<(ostream &,const Circle &);
    void setRadius(double);
    double getRadius()const;
    double area()const;
    };
#endif

#ifndef _Cylinder_H//Cylinder.h
#define _Cylinder_H
#include<iostream.h>
class Cylinder:public Circle{
 private:
    double height;
 public:
     Cylinder(double=0,double=0,double=0,double=0);
  void setHeight(double);
  double getHeight()const;
  double area()const;
  double volume()const;
  friend ostream & operator<<(ostream &,const Cylinder &);

};
#endif

#ifndef _Point_H//Point.h
#define _Point_H
#include<iostream.h>

class Point{
protected:
    double x;
    double y;
public:
    Point(double=0,double=0);
    void setPoint(double,double);
    double getX();
    double getY();
friend ostream & operator<<(ostream &,const Point &);

};
#endif

#include"Point.h"//Circle.cpp
#include"Circle.h"
#include<iostream.h>

Circle::Circle(double a ,double b ,double c ):Point(a,b){
    radius=c;
}

void Circle::setRadius(double a){
  radius = a;
}

double Circle::getRadius()const{
  return radius;
}

double Circle::area()const{
  return 3.14*radius*radius;
}

ostream &operator<<(ostream &output,const Circle &c){
    output<<c.area()<<endl; 

    return output;
}

#include"Point.h"//Cylinder.cpp
#include"Circle.h"
#include"Cylinder.h"
#include<iostream.h>


Cylinder::Cylinder(double a,double b,double c,double d):Circle(a,b,c){
 height = d;  
}

void Cylinder::setHeight(double a){
  height = a;

double Cylinder::getHeight ()const{
  return height;
}

double Cylinder::area()const{
    return 2*Circle::area()+height*2*3.14*radius;
}

double Cylinder::volume()const{
    return Circle::area() *height;
}

 ostream &operator<<(ostream & output,const Cylinder & cy){
  cout<< "修改x,y为[" << cy.x << "," << cy.y << "],r=" << cy.radius <<",h"<<cy.height << "\n" <<"表面积=" <<cy.area () << ",体积=" <<cy.volume () <<endl;
  return output;
}

#include"Point.h"//Point.cpp
#include<iostream.h>


Point::Point(double a,double b){
  x = a;
  y = b;
}

void Point::setPoint(double a,double b){
  x = a;
  y = b;
}

double Point::getX(){
  return x;
}

double Point::getY(){
  return y;
}

ostream & operator<<(ostream &output,const Point &p){
    output<<"["<<p.x<<","<<p.y<<"]"<<endl;
    return output;
}

#include"Point.h"//Main.cpp
#include"Circle.h"
#include"Cylinder.h"
#include<iostream.h>

int main(){
  Cylinder cy1(3.5,6.4,5.2,10);
  cout<< "圆柱:\nx=" << cy1.getX() << ",y=" << cy1.getY() << ",r=" << cy1.getRadius() << ",h=" << cy1.getHeight () << "\n" << "表面积=" << cy1.area() << ",体积=" << cy1.volume () << endl;
  cy1.setHeight (15);
  cy1.setRadius (7.5);
  cy1.setPoint(5,5);
  cout<<endl;
  cout<< "新圆柱:\n" <<cy1;
  Point &pRef = cy1;
  cout<< "\n"<<"x,y为" <<pRef;
  Circle &cRef = cy1;
  cout<< "\n"<<"圆的面积为" <<cRef;
  return 0;
}

2、

#include<iostream>
using namespace std;

class Point{
public:
    Point(){}
    virtual~Point(){
      cout<<"execyting Point destructor" <<endl;
    }
};

class Cricle:public Point{
public:
    Cricle(int = 0);
    virtual~Cricle(){
      cout<< "exceuting Circle destructor" << endl;
    }
private:
    int radus;
};

Cricle::Cricle(int a){
        radus = a;
    }

int main(){
  Point *p=new Cricle(33);
 // Cricle *c=new Cricle(33);  //第二小问
 // delete c;                  //第二小问
  delete p;
  return 0;
}

3、

#include<iostream>
using namespace std;

class shape{
public:
    virtual double area()=0;
    
};

class Circle:public shape{
public:
    Circle(double a){
      r=a;
    }
    virtual double area(){
      return r*r*3.14;
    }
protected:
    double r;
};

class Rectangle:public shape{
public:
    Rectangle(double a,double b){
      x=a;y=b;
    }
    virtual double area(){
      return x*y;
    }
protected:
  double x,y;
};

class Triangle:public shape{
public:
    Triangle(double a,double b){
      d=a;
      g=b;
    }
    virtual double area(){
      return d*g/2;
    }
protected:
    double d;
    double g;
};

 void printArea(Circle &c1,Rectangle &r1,Triangle &t1){
  cout<<"圆的面积:"<<c1.area()<<'\n'<<"矩形面积:"<<r1.area() <<'\n'<<"三角形面积:"<<t1.area ()<<endl;
}

int main(){
  void printArea(Circle &,Rectangle &,Triangle &);
    Circle c1(3.0);
  Rectangle r1(2.1,3.0);
  Triangle t1(2.2,3.3);
  printArea(c1,r1,t1);
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值