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

c++ prime plus 10

2017-09-09 10:22 447 查看
继续学习,昨天第十章还没有看,今早补上。神奇的是,我去图书馆又忘记带笔了。。。下面是一些笔记和问题。
两个源文件的链接。(很简单。。。)
控制格式的函数。P351
析构函数的调用。//虽然我不是很清楚,但觉得很好玩。
P356 类声明中函数原型对变量初始化——默认参数。P274
接受一个参数的构造函数允许使用赋值语法将对象初始化一个值。P362
引用传递P363,P369、368(*).。。。比较两个对象。this指针,指向,用来调用成员函数的对象。*this作为调用对象的别名。//这里面涉及一些指针,我有点乱。
创建类对象数组,必须构造默认构造函数。
类作用域(略过)
抽象数据类型 以 通用的方式描述数据类型。//这个好像很重要。
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: