开发基本的C++代码+检查错误/警告

Develop basic C++ code + Check the errors/warnings

本文关键字:检查 错误 警告 代码 C++ 开发      更新时间:2023-10-16

嗨,这是一个uni赋值我在parta中要做的是:使用Kouti类声明编写main()代码-将变量ogkos的值初始化为0.0(在此变量中,将存储稍后计算的每个盒子的体积)-它将使用值2.0、3.2和6.0初始化KoutiA-它将使用值2.5、4.0和5.0初始化KoutiB-盒子A的体积将被计算并保存在变量ogkos中,然后显示在屏幕上-盒子B的体积将被计算并保存在变量ogkos中,然后显示在屏幕上

这是类声明

class Kouti
{
   public:
     double length;
     double breadth;
     double height;
};

到目前为止,我已经完成了parta。这是我的代码:

#include <iostream>
using namespace std;
class Kouti
{
    public:
        //constructor that initialize Box dimensions
        explicit Kouti(double lengthOfKouti, double breadthOfKouti, 
        double heightOfKouti)
        : length(lengthOfKouti), breadth(breadthOfKouti), 
        height(heightOfKouti)
        {
            //empty body
        }
        double length;
        double breadth;
        double height;
        double ogkos = 0.0;
        /*
        void displayMessage() const
        {
            cout << "Welcome to the Kouti program!" << endl;
        }
        */
        /*
        void calculateOgkos() const
        {
            ogkos = length*breadth*height;
            cout << ogkos;
        }
        */
};
int main()
{
    Kouti KoutiA(2.0, 3.2, 6.0);
    Kouti KoutiB(2.5, 4.0, 5.0);
    KoutiA.ogkos = KoutiA.length*KoutiA.breadth*KoutiA.height;
    KoutiB.ogkos = KoutiB.length*KoutiB.breadth*KoutiB.height;
    // Display the volume of each box
    cout << "KoutiA volume is: " << KoutiA.ogkos
        << "nKoutiB volume is: " << KoutiB.ogkos
        << endl;
    return 0;
}

它似乎运行得很好不过,它给了我一个关于非静态数据成员初始化器的警告,该初始化器仅在-std=c++11或-std=gnu++11时可用。我正在使用代码块

Partb

在第b部分中,我必须执行以下操作:将类成员数据转换为private,并提供适当的公共函数calculateOgkos(),该函数将计算每个对象的体积。在main()中进行适当的更改,使其像在parta中一样工作。你能帮我吗?

还有一些不相关的。。。我一直在尝试注册代码块论坛,但我没有收到激活电子邮件。我试过好几次了。感谢

您几乎已经完成了工作…:

class Kouti
{
public:
    //constructor that initialize Box dimensions
    explicit Kouti(double lengthOfKouti, double breadthOfKouti, 
        double heightOfKouti)
        : length(lengthOfKouti), breadth(breadthOfKouti), 
        height(heightOfKouti)
    {
        //empty body
    }
    double calculateOgkos() const
    {
        return length*breadth*height;
    }
private:
    double length;
    double breadth;
    double height;
};

Main:

int main()
{
    Kouti KoutiA(2.0, 3.2, 6.0);
    Kouti KoutiB(2.5, 4.0, 5.0);
    // Display the volume of each box
    cout << "KoutiA volume is: " << KoutiA.calculateOgkos()
        << "nKoutiB volume is: " << KoutiB.calculateOgkos()
        << endl;
    return 0;
}