| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 784 人关注过本帖
标题:编写一个名为door的类,其中定义两个公有浮点型成员变量height和width, 其 ...
只看楼主 加入收藏
baolis
Rank: 2
来 自:呼啦瓦星
等 级:论坛游民
帖 子:39
专家分:59
注 册:2021-11-10
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
编写一个名为door的类,其中定义两个公有浮点型成员变量height和width, 其赋其缺省值分别为 2.0 和 1.5
编写一个名为door的类,其中定义两个公有浮点型成员变量height和width,
其赋其缺省值分别为 2.0 和 1.5。改写其构造和析构函数。构造对象时,可以
给成员变量 height 和 width 赋新值,并且输出字符串“The door is created…”。
析构对象时,输出“The door is destroyed…”。类中定义两个成员函数,分别求
出 door 的周长 perimeter 和面积 acreage。
要求写出包含主程序和实例化对象在内的完整的程序。
搜索更多相关主题的帖子: 成员 变量 定义 浮点 公有 
2022-11-21 09:06
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
哪里不会你要讲出来,而不是全抄

程序代码:
#include <iostream>

class door
{
public:
    double height;
    double width;

    explicit door( double height=2.0, double width=1.5 ) : height(height), width(width)
    {
        std::cout << "The door is created…\n";
    }
    door( const door& rhs ) :  height(rhs.height), width(rhs.width)
    {
        std::cout << "The door is created…\n";
    }
    door( door&& rhs ) noexcept = default;

    ~door()
    {
        std::cout << "The door is destroyed…\n";
    }

    double perimeter() const noexcept
    {
        return 2*(height+width);
    }
    double acreage() const noexcept
    {
        return height*width;
    }
};

std::ostream& operator<<( std::ostream& os, const door& rhs )
{
    return os << "{ height=" << rhs.height << ", width=" << rhs.width << " }";
}

using namespace std;

int main( void )
{
    {
        door a;
        cout << a << endl;

        door b{ 3, 4 };
        cout << b << endl;

        door c = b;
        cout << b << endl;

        cout << "perimeter: " << c.perimeter() << endl;
        cout << "acreage: " << c.acreage() << endl;
    }
}
2022-11-21 09:50
baolis
Rank: 2
来 自:呼啦瓦星
等 级:论坛游民
帖 子:39
专家分:59
注 册:2021-11-10
收藏
得分:0 
回复 2楼 rjsp

  printf("Hello, world!\n"); cout << "Hello, world!" << endl;
2022-11-21 11:18
baolis
Rank: 2
来 自:呼啦瓦星
等 级:论坛游民
帖 子:39
专家分:59
注 册:2021-11-10
收藏
得分:0 
回复 2楼 rjsp

  printf("Hello, world!\n"); cout << "Hello, world!" << endl;
2022-11-21 14:10
快速回复:编写一个名为door的类,其中定义两个公有浮点型成员变量height和width ...
数据加载中...
 
   



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

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