c++ prime plus 10

继续学习,昨天第十章还没有看,今早补上。神奇的是,我去图书馆又忘记带笔了。。。下面是一些笔记和问题。
  1. 两个源文件的链接。(很简单。。。)
  2. 控制格式的函数。P351
  3. 析构函数的调用。//虽然我不是很清楚,但觉得很好玩。
  4. P356 类声明中函数原型对变量初始化——默认参数。P274
  5. 接受一个参数的构造函数允许使用赋值语法将对象初始化一个值。P362
  6. 引用传递P363,P369、368(*).。。。比较两个对象。this指针,指向,用来调用成员函数的对象。*this作为调用对象的别名。//这里面涉及一些指针,我有点乱。
  7. 创建类对象数组,必须构造默认构造函数。
  8. 类作用域(略过)
  9. 抽象数据类型 以 通用的方式描述数据类型。//这个好像很重要。
  10. string类型。const string& name。还有在下面的头文件里,好像有

    #include <iostream>
    #include <string>
估计还是重要的吧。我删删看。第一行可以删。第二行不可以。
然后就做了10.10.1,并加了一些东西,比如默认构造函数,析构函数,和this指针的运用。
//Test.h
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED

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

class Account
{
public:
    Account(const string& name, const string& id, double money);
    Account();
    ~Account();
    void show() const;
    void add(double m);
    void decrease(double m);
    const Account & topal(const Account & s) const;
private:
    string _name;
    string _id;
    double _money;
};


#endif // TEST_H_INCLUDED

//c++ 10.9.5.cpp
#include "Test.h"
#include <iostream>

using namespace std;


Account::Account(const string& name, const string& id, double money)
{
    _name = name;
    _id = id;
    _money = money;
}

Account::Account()
{
    _name = "no name";
    _id = "000000";
    _money = 0;
}

Account::~Account()
{
    cout<< "Happy Today!";
}

void Account::show() const
{
    cout << "User: " << _name
        << " id:" << _id
        << " money:" << _money
        <<endl;
}

void Account::add(double m)
{
    _money += m;
}

void Account::decrease(double m)
{
    _money -= m;
}

const Account & Account::topal(const Account & s) const
{
    if(s._money>_money)
        return s;
    else
        return *this;
}

//main.cpp
#include <iostream>
#include "Test.h"
#include <new>
using namespace std;


int main(int argc, const char * argv[])
{
    Account a ("Hellwrld", "123456", 900);
    Account b;
    a.show();
    b.show();
    b.add(15456);
    a.add(12345);
    a.show();
    b.show();
    a.decrease(1234);
    a.show();
    const Account * top = &a;
    top = & top -> topal(b);
    std::cout << "\nTHE richest one :\n";
    top->show();
    return 0;
}

#include <iostream>
#include <string>

结果:
User: Hellwrld id:123456 money:900
User: no name id:000000 money:0
User: Hellwrld id:123456 money:13245
User: no name id:000000 money:15456
User: Hellwrld id:123456 money:12011


THE richest one :
User: no name id:000000 money:15456
Happy Today!Happy Today!
遇到一个问题,指向
top->show();
问题描述:
C:\Users\Sjpei\Desktop\总文件夹\c++ prime plus\c++10.9.5\main.cpp|20|error: passing 'const Account' as 'this' argument of 'void Account::show()' discards qualifiers [-fpermissive]|
这是由于常量对象调用了非常量成员函数引起的错误,错误原因在于常量对象只能调用常量成员函数(因为常量成员函数约定不对非静态成员进行修改).
解决办法:在show()后面加const。

c++pp的题目还比较多,我智商不高,慢慢来,每做一题,看能不能改一下程序,什么的。怎么说呢,继续吧。

#include <iostream>
#include <string>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值