| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2589 人关注过本帖
标题:C++期末大作业,菜鸟一只,也不知道写的对不对,麻烦帮忙看一下,蟹蟹
取消只看楼主 加入收藏
anhaoAH
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2019-12-18
结帖率:75%
收藏
已结贴  问题点数:10 回复次数:4 
C++期末大作业,菜鸟一只,也不知道写的对不对,麻烦帮忙看一下,蟹蟹
题目要求:a、将三角形、矩形、圆形等形状封装成相应的类;
          b、实现相应图形的构造函数;
          c、将计算相应图形的周长和面积函数封装成类的成员函数;
          d、在main函数中分别输入相应图形的参数构造相应图形的对象,计算相应图形的周长和面积并输出。
#include<iostream>
#include<cmath>
using namespace std;
class Graph
{
public:
    Graph(int a,int b,int c)
    {
        x=a;
        y=b;
        z=c;
    }
    Graph(int a,int b)
    {
        x=a;
        y=b;
    }
    Graph(int a)
    {
        x=a;
    }
    double perimeter(int a,int b,int c)
    {
        return a+b+c;
    }
    double perimeter(int a,int b)
    {
        return 2*a+2*b;
    }
    double perimeter(int a)
    {
        return 3.14*2*a;
    }
    double area(int a,int b,int c)
    {
        double p=(a+b+c)/2;
        return sqrt(p*(p-a)*(p-b)*(p-c));
    }
    double area(int a,int b)
    {
        return a*b;
    }
    double area(int a)
    {
        return 3.14*a*a;
    }
private:
    int x;
    int y;
    int z;
};
int main()
{
    int a,b,c;
    cin>>a>>b>>c;
    Graph A(a,b,c);
    cout<<A.perimeter()<<' '<<A.area()<<endl;
    return 0;
}
编译时红色代码行报错显示error: no matching function for call to Graph: perimeter O 求解答哪里出的问题
另外,题目求不同图形的周长和面积我的想法是函数重载,但是我感觉“cin>>a>>b>>c;”这一句输入参数进行重载好像不太对,但是自己又想不出什么好办法,希望大佬帮忙指正。
搜索更多相关主题的帖子: double 图形 Graph return int 
2020-06-09 18:11
anhaoAH
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2019-12-18
收藏
得分:0 
回复 3楼 rjsp
这个程序在codeblocks编译出错
#include<numbers>    显示fatal error: numbers: No such file or directory    好像没有这个头文件
而且这个程序它没有输入,而我想请教的是像下面说的那样👇👇👇
请问如何实现从键盘输入不确定几个数字(1/2/3)后自动调用相应的函数,额,不太好表达,就是我可能会输入一个数字,也可能是两个或三个,但是无论我输入几个数字,都必须回车后直接调用相应图形的周长和面积函数并输出。
2020-06-10 10:29
anhaoAH
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2019-12-18
收藏
得分:0 
回复 2楼 吕孟伟
请问如何实现从键盘输入不确定几个数字(1/2/3)后自动调用相应的函数,额,不太好表达,就是我可能会输入一个数字,也可能是两个或三个,但是无论我输入几个数字,都必须回车后直接调用相应图形的周长和面积函数并输出。
2020-06-10 10:34
anhaoAH
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2019-12-18
收藏
得分:0 
谢谢大家的回答!我又来了听了大家的建议改成了三个类,三角形的输出也对了,非常接近成功,现在是main函数里不知道该怎么写才能让程序自动调用不同图形的相应函数,求解答。
#include<iostream>
#include<cmath>
using namespace std;
class Graph
{
public:
    Graph(int a,int b,int c)
    {
        x=a;
        y=b;
        z=c;
    }
    Graph(int a,int b)
    {
        x=a;
        y=b;
    }
    Graph(int a)
    {
        x=a;
    }
private:
    int x;
    int y;
    int z;
};
class Triangle
{
public:
    Triangle(int a,int b,int c)
    {
        x=a;
        y=b;
        z=c;
    }
    double perimeter()
    {
        return this->x + this->y + this->z;
    }

    double area()
    {
        double p=(this->x+this->y+this->z)/2;
        return sqrt(p*(p-this->x)*(p-this->y)*(p-this->z));
    }
private:
    int x;
    int y;
    int z;
};
class Rectangle
{
public:
    Rectangle(int a,int b)
    {
        x=a;
        y=b;
    }
    double perimeter()
    {
        return 2*(this->x+this->y);
    }
    double area()
    {
        return this->x*this->y;
    }
private:
    int x;
    int y;
};
class Round
{
public:
    Round(int a)
    {
        x=a;
    }
    double perimeter()
    {
        return 2*3.14*this->x;
    }
    double area()
    {
        return 3.14*this->x*this->x;
    }
private:
    int x;
};
int main()
{
    int a,b,c;
    cin>>a>>b>>c;
    Triangle A(a,b,c);
        就是这一块不知道咋写,目前只能计算特定图形的。
    cout<<A.perimeter()<<' '<<A.area()<<endl;
    return 0;
}
2020-06-10 11:46
anhaoAH
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2019-12-18
收藏
得分:0 
#include<iostream>
#include<cmath>
using namespace std;
class Triangle
{
public:
    Triangle(int a,int b,int c)
    {
        x=a;
        y=b;
        z=c;
    }
    double perimeter()
    {
        return this->x + this->y + this->z;
    }

    double area()
    {
        double p=(this->x+this->y+this->z)/2;
        return sqrt(p*(p-this->x)*(p-this->y)*(p-this->z));
    }
private:
    int x;
    int y;
    int z;
};
class Rectangle
{
public:
    Rectangle(int a,int b)
    {
        x=a;
        y=b;
    }
    double perimeter()
    {
        return 2*(this->x+this->y);
    }
    double area()
    {
        return this->x*this->y;
    }
private:
    int x;
    int y;
};
class Round
{
public:
    Round(int a)
    {
        x=a;
    }
    double perimeter()
    {
        return 2*3.14*this->x;
    }
    double area()
    {
        return 3.14*this->x*this->x;
    }
private:
    int x;
};
int main()
{
    int x,a,b,c;
    cout<<"Please select the type you want to calculate."<<endl;/*先确定要计算的图形类型*/
    cout<<"1:Triangle"<<endl<<"2:Rectangle"<<endl<<"3:Round"<<endl;
    cin>>x;
    if(x==1)
    {
    cin>>a>>b>>c;
        if(a+b<=c||b+c<=a||a+c<=b||a<=0||b<=0||c<=0)/*排除数据不符合条件的情况*/
        {
            cout<<"Wrong data.";
        }
        else
        {
            Triangle A(a,b,c);
            cout<<A.perimeter()<<' '<<A.area()<<endl;
        }
    }
    else if(x==2)
    {
        cin>>a>>b;
        if(a<=0||b<=0)//排除数据不符合条件的情况
        {
            cout<<"Wrong data.";
        }
        else
        {
        Rectangle A(a,b);
        cout<<A.perimeter()<<' '<<A.area()<<endl;
        }
    }
    else if(x==3)
    {
        cin>>a;
        if(a<=0)//排除数据不符合条件的情况
        {
            cout<<"Wrong data.";
        }
        else
        {
        Round A(a);
        cout<<A.perimeter()<<' '<<A.area()<<endl;
        }
    }
    else
    {
        cout<<"Wrong type.";//当所选图形类型不在范围内时结束程序
        exit(0);
    }
    return 0;
}
最后改成了这样,运行正常,但总感觉略显臃肿,也算是解决了。
感谢大家的帮助!
2020-06-10 20:58
快速回复:C++期末大作业,菜鸟一只,也不知道写的对不对,麻烦帮忙看一下,蟹蟹
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037175 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved