程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用 C++ 使用面向对象编程查找盒子的体积大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用 C++ 使用面向对象编程查找盒子的体积?

开发过程中遇到使用 C++ 使用面向对象编程查找盒子的体积的问题如何解决?下面主要结合日常开发的经验,给出你关于使用 C++ 使用面向对象编程查找盒子的体积的解决方法建议,希望对你解决使用 C++ 使用面向对象编程查找盒子的体积有所启发或帮助;

编写一个名为 BoxVolume 的类,以长度、宽度和高度为数据成员,以 readData()、disPDAta() 和 computeVol() 作为函数。还要写一个 main() 函数来测试 BoxVolume 类。

我试过了 -

#include <iostream>

class BoxVolume
{
    public:

    float length;
    float wIDth;
    float height;

    voID readData()
    {
        using namespace std
        cout << "length: ";
        cin >> 'BoxVolume::length';
        cout << "wIDth: ";
        cin >> 'BoxVolume::wIDth';
        cout << "height: ";
        cin >> 'BoxVolume::height';
    }
    voID computeVolume()
    {
        float volume;
        volume = 'voID readData()::length' * 'voID readData()::wIDth' * 'voID readData()::height';
    }

    voID disPDAta()
    {
        using namespace std
        cout << "Volume is:" << 'voID computeVolume()::volume';

    }

};

int main()
{
    BoxVolume obj1,obj2,obj3;
    obj1.readData();
    obj2.computeVolume();
    obj3.disPDAta();

};

解决方法

您应该先阅读一本 C++ 介绍性书籍以获得该语言的基础知识,然后再开始编码。我建议 Accelerated C++ 因为它比它的竞争对手短,但仍然相当完整(在它发布时)。现在它缺乏对语言中后续标准的支持,从 C++11 开始,所以目前 C++ Primer 可能是最好的选择,然大约有 1000 页,并且即将推出新版本几个月后出来。或者,还有 Stroustrup 的 C++ 之旅。他是语言的创造者。

下面是您代码的编译版本,我添加了封装,但如果您不关心公开数据成员,则可以使用 struct 代替 class

#include <iostream>

using namespace std;

class BoxVolume
{
public:
    void readData()
    {
        cout << "length: ";
        cin >> length;
        cout << "width: ";
        cin >> width;
        cout << "height: ";
        cin >> height;
    }

    float computeVolume()
    {
        return length * width * height;
    }

    void disPDAta()
    {
        //using namespace std  // this is artistic I couldnt cancel it
        cout << "Volume is: " << computeVolume() << endl;

    }

private:
    float length;
    float width;
    float height;
};

int main()
{
    BoxVolume obj1,obj2,obj3;
    obj1.readData();
    obj2.computeVolume();  // data undefined for obj2
    obj3.disPDAta();       // data undefined for obj3
};

是的,我忍不住要回答这个问题。

大佬总结

以上是大佬教程为你收集整理的使用 C++ 使用面向对象编程查找盒子的体积全部内容,希望文章能够帮你解决使用 C++ 使用面向对象编程查找盒子的体积所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:使用