C++代码运行中出现无法解析的外部命令?

代码的作用是定义一个点类Point,派生出圆类和圆柱体类Circle和Cylinder
功能是输出圆的周长(通过定义的Cirfer()函数)、面积(定义有Area()函数)和圆柱体的表面积和体积

图片说明

//Shape.h
#ifndef SHAPE_H
#define SHAPE_H
#include<iostream>
#define PI 3.1415926
using namespace std;

class Point
{
public:
    Point(double X, double Y);   //构造函数
    void Set(double X, double Y);   //设置点值
    double Getx()const;    //获取横坐标
    double Gety()const;    //获取纵坐标
    virtual void Output(ostream &out) const;   //输出函数
    virtual void Input(istream &in);    //输入函数
protected:
    double x, y;
};

class Circle :public Point
{
public:
    Circle(const Point &p, double R);   
    void Set(double X, double Y, double R);
    void Set(const Point &p, double R);
    double Getr()const;
    double Area();   //计算圆的面积
    double Cirfer();   //计算圆的周长
    void Output(ostream &out)const;
    void Input(istream &in);
protected:
    double r;
};

class Cylinder :public Circle
{
public:
    Cylinder(const Circle& c, double H);
    void Set(const Circle& c, double H);
    double Geth()const;
    double Area();    //计算圆柱体的表面积
    double Volume();    //计算圆柱体体积
    void Output(ostream& out)const;
    void Input(istream& in);
protected:
    double h;
};

ostream& operator<<(ostream& out, const Point& p);  //输出运算符重载
istream& operator>>(istream& in, const Point& p);    //输入运算符重载
#endif

//Shape.cpp
#include<iostream>
#include"Shape.h"
using namespace std;

ostream & operator<<(ostream &out, const Point &p)   //输出运算符重载
{
    p.Output(out);
    return out;
}
istream & operator>>(istream &in, Point &p)    //输入运算符重载
{
    p.Input(in);
    return in;
}

//Point类成员函数
Point::Point(double X, double Y) :x(X), y(Y) {}
void Point::Set(double X, double Y)
{
    x = X, y = Y;
}
double Point::Getx()const
{
    return x;
}
double Point::Gety()const
{
    return y;
}
void Point::Output(ostream &out)const
{
    out << '(' << x << "," << y << ')';
}
void Point::Input(istream& in)
{
    char str[80];
    in.getline(str, 80, '(');
    in.getline(str, 80, ','); x = atof(str);   //atof()是字符从ASCII转为float型数据
    in.getline(str, 80, ')'); y = atof(str);
}

//Circle类成员函数
Circle::Circle(const Point& p, double R) : Point(p), r(R) {}
void Circle::Set(double X, double Y, double R)
{
    x = X, y = Y, r = R;
}
void Circle::Set(const Point& p, double R)
{
    Point::Set(p.Getx(), p.Gety());
    r = R;
}
double Circle::Getr()const
{
    return r;
}
double Circle::Area()   //圆面积
{
    return PI * r * r;
}
double Circle::Cirfer()    //圆周长
{
    return 2 * PI * r;
}
void Circle::Output(ostream& out)const
{
    out << '[' << '(' << x << "," << y << ')' << "," << r << ']';
}
void Circle::Input(istream& in)
{
    char str[80];
    in.getline(str, 80, '[');
    Point::Input(in);
    in.getline(str, 80, ',');
    in.getline(str, 80, ']'); r = atof(str);
}

//Cylinder类成员函数
Cylinder::Cylinder(const Circle& c, double H) :Circle(c), h(H) {}
void Cylinder::Set(const Circle& c, double H)
{
    Circle::Set(Getx(),Gety(),Getr());
    h = H;
}
double Cylinder::Geth()const
{
    return h;
}
double Cylinder::Area()   //圆柱体表面积
{
    return PI*r*r* 2+2*PI*r*h;
}
double Cylinder::Volume()    //圆柱体体积
{
    return PI*r*r* h;
}
void Cylinder::Output(ostream& out)const
{
    out << '{';
    Circle::Output(out);
    out << "," << h << '}';
}
void Cylinder::Input(istream& in)
{
    char str[80];
    in.getline(str, 80, '{');
    Circle::Input(in);
    in.getline(str, 80, ',');
    in.getline(str, 80, '}'); h = atof(str);
}

//test.cpp
#include<iostream>
#include"Shape.h"

int main()
{
    Point p(5, 8);   //初始化各个类的数据成员
    Circle c(p, 10);
    Cylinder y(c, 8);

    cout << c << "\tArea:" << c.Area() << "\t\tCircumference" << c.Cirfer() << '\n'
        << y << "\tArea:" << y.Area() << "\tVolume:" << y.Volume() << endl;

    cout << "\n请注意键盘输入格式。";
    cout << "\n包括圆括号、方括号、花括号、逗号,";
    cout << "其中可以插入或不插入空格、<Tab>、换行。" << endl;

    cout << "请输入一个点 (x, y) : ";
    cin >> p;
    cout << "结果:" << p << endl;

    cout << "请输入一个圆 [(x, y), r] : ";
    cin >> c;
    cout << "结果:" << c << endl;

    cout << "请输入一个圆柱 {[(x, y), r], h} : ";
    cin >> y;
    cout << "结果:" << y << endl;
    return 0;
}
查看全部
hahastephanie
hahastephanie
2019/10/13 22:49
  • c++
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

3个回复