您的位置:首页 > 编程语言 > C语言/C++

C++Primer第五版 7.1.2节练习

2015-09-16 07:16 423 查看
练习7.2:曾在2.6.2节练习(第67页)中编写了一个Sales_data类,请向这个类添加combine和isbn成员。

练习7.3:修改7.1.1节(第229页)的交易处理程序,令其使用这些成员。

练习7.4:编写一个名为Person的类,使其表示人员的姓名和地址。使用string对象存放这些元素,接下来的练习将不断充实这个类的其他特征。

答:练习7.2-7.3见云盘程序。

练习7.5:在你的Person类中提供一些操作使其能够返回姓名和住址。这些函数是否应该是const的呢?解释原因。

答:是const的,原因是this指针指向的姓名和地址的存储地址不变,即this指针一直指向所指的对象。

练习7.2

/*
*练习7.2 
*日期:2015/7/1 
*问题描述:练习7.2:曾在2.6.2节练习(第67页)中编写了一个Sales_data类,请向这个类添加combine和isbn成员。
*功能;写一个类 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/

#include <iostream>
#include <string> 

using namespace std;

struct Sales_data
{
    string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;

    string isbn() const {return bookNo;}
    Sales_data& combine(const Sales_data&);
    double avg_price() const;
};

double Sales_data::avg_price() const
{
    if(units_sold)
        return revenue/units_sold;
    else 
        return 0;
}

Sales_data& Sales_data::combine(const Sales_data &rhs)
{
    units_sold += rhs.units_sold; //把rhs的成员加到this对象的成员上 
    revenue += rhs.revenue;
    return *this;                //返回调用该函数的对象 
}

int main()
{
    return 0;
}


练习7.3

/*
*练习7.3 
*日期:2015/7/1 
*问题描述:练习7.3:修改7.1.1节(第229页)的交易处理程序,令其使用这些成员。
*说明;229页的函数中,功能都已经实现了,而我写的这个Sales_data类还不全,因此只是为了在主函数里面调用这些方法,才进行了改写,写得不好 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/

#include <iostream>
#include <string>
using namespace std;

struct Sales_data
{
    string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;

    string isbn() const {return bookNo;}
    Sales_data& combine(const Sales_data&);
    double avg_price() const;
};

double Sales_data::avg_price() const
{
    if(units_sold)
        return revenue/units_sold;
    else 
        return 0;
}

Sales_data& Sales_data::combine(const Sales_data &rhs)
{
    units_sold += rhs.units_sold; //把rhs的成员加到this对象的成员上 
    revenue += rhs.revenue;
    return *this;                //返回调用该函数的对象 
}

int main()
{
    Sales_data total;
    total.bookNo = "123";
    total.units_sold = 10;
    total.revenue = 5;

    if(total.bookNo != "")
    {
        Sales_data trans;
        trans.bookNo = "123";
        trans.units_sold = 10;
        trans.revenue = 5;

        if(total.isbn() == trans.isbn())
            total.combine(trans);
        else 
         {
            cout << total.units_sold << endl;
            total.bookNo = trans.bookNo;
            total.units_sold = total.units_sold;
            total.revenue = total.revenue;
         }
         cout << total.units_sold << endl;
    }else
    {
        cerr << "No data?" << endl;
    }
    return 0;
}


练习7.4

/*
*练习7.4 
*日期:2015/7/1
*问题描述:练习7.4:编写一个名为Person的类,使其表示人员的姓名和地址。使用string对象存放这些元素,接下来的练习将不断充实这个类的其他特征。
*功能;正式开始写一个类啦,是不是很激动咧 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/

#include <iostream>
#include <string>

using namespace std;

struct Person{
    string name;
    string address;

};


练习7.5

#include <iostream>
#include <string> 

using namespace std;

struct Person{
    string name;
    string address;

    string getName() const {return name;}
    string getAddress() const {return address;}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: