免费影视网站建设备案域名交易平台
文章目录
- 练习7.31
- 练习7.32
- 练习7.33
- 练习7.34
- 练习7.35
- 练习7.36
- 练习7.37
- 练习7.38
- 练习7.29
- 练习7.40
练习7.31
定义一对类X 和Y,其中X 包含一个指向 Y 的指针,而Y 包含一个类型为 X 的对象。
class Y;class X{Y* y = nullptr;
};class Y{X x;
};
练习7.32
定义你自己的Screen 和 Window_mgr,其中clear是Window_mgr的成员,是Screen的友元。
#ifndef CP5_ex7_32_h
#define CP5_ex7_32_h#include <vector>
#include <iostream>
#include <string>class Screen;class Window_mgr
{
public:using ScreenIndex = std::vector<Screen>::size_type;inline void clear(ScreenIndex);private:std::vector<Screen> screens;
};class Screen
{friend void Window_mgr::clear(ScreenIndex);public:using pos = std::string::size_type;Screen() = default;Screen(pos ht, pos wd) :height(ht), width(wd), contents(ht*wd,' ') {}Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht*wd, c) {}char get() const { return contents[cursor]; }char get(pos r, pos c) const { return contents[r*width + c]; }inline Screen& move(pos r, pos c);inline Screen& set(char c);inline Screen& set(pos r, pos c, char ch);const Screen& display(std::ostream& os) const { do_display(os); return *this; }Screen& display(std::ostream& os) { do_display(os); return *this; }private:void do_display(std::ostream &os) const { os << contents; }private:pos cursor = 0;pos width = 0, height = 0;std::string contents;
};inline void Window_mgr::clear(ScreenIndex i)
{Screen& s = screens[i];s.contents = std::string(s.height*s.width,' ');
}inline Screen& Screen::move(pos r, pos c)
{cursor = r*width + c;return *this;
}inline Screen& Screen::set(char c)
{contents[cursor] = c;return *this;
}inline Screen& Screen::set(pos r, pos c, char ch)
{contents[r*width + c] = ch;return *this;
}#endif
练习7.33
如果我们给Screen 添加一个如下所示的size成员将发生什么情况?如果出现了问题,请尝试修改它。
pos Screen::size() const
{return height * width;
}
未定义标识符 pos。应该改为:
Screen::pos Screen::size() const
{return height * width;
}
练习7.34
如果我们把第256页Screen类的pos的typedef放在类的最后一行会发生什么情况?
在 dummy_fcn(pos height)
函数中会出现 未定义的标识符pos。
类型名的定义通常出现在类的开始处,这样就能确保所有使用该类型的成员都出现在类名的定义之后。
练习7.35
解释下面代码的含义,说明其中的Type和initVal分别使用了哪个定义。如果代码存在错误,尝试修改它。
typedef string Type;
Type initVal();
class Exercise {
public:typedef double Type;Type setVal(Type);Type initVal();
private:int val;
};
Type Exercise::setVal(Type parm) { val = parm + initVal(); return val;
}
书上255页中说:
然而在类中,如果成员使用了外层作用域中的某个名字,而该名字代表一种类型,则类不能在之后重新定义该名字。
因此重复定义 Type 是错误的行为。
- 虽然重复定义类型名字是错误的行为,但是编译器并不为此负责。所以我们要人为地遵守一些原则,在这里有一些讨论。
练习7.36
下面的初始值是错误的,请找出问题所在并尝试修改它。
struct X {X (int i, int j): base(i), rem(base % j) {}int rem, base;
};
应该改为:
struct X {X (int i, int j): base(i), rem(base % j) {}int base, rem;
};
练习7.37
使用本节提供的Sales_data类,确定初始化下面的变量时分别使用了哪个构造函数,然后罗列出每个对象所有的数据成员的值。
Sales_data first_item(cin); // 使用 Sales_data(std::istream &is) ; 各成员值从输入流中读取
int main() {Sales_data next; // 使用默认构造函数 bookNo = "", cnt = 0, revenue = 0.0// 使用 Sales_data(std::string s = ""); bookNo = "9-999-99999-9", cnt = 0, revenue = 0.0Sales_data last("9-999-99999-9");
}
练习7.38
有些情况下我们希望提供cin作为接受istream& 参数的构造函数的默认实参,请声明这样的构造函数。
Sales_data(std::istream &is = std::cin) { read(is, *this); }
练习7.29
如果接受string 的构造函数和接受 istream& 的构造函数都使用默认实参,这种行为合法吗?如果不,为什么?
不合法。当你调用 Sales_data()
构造函数时,无法区分是哪个重载。
练习7.40
从下面的抽象概念中选择一个(或者你自己指定一个),思考这样的类需要哪些数据成员,提供一组合理的构造函数并阐明这样做的原因。
(a) Book
(b) Data
(c) Employee
(d) Vehicle
(e) Object
(f) Tree
(a) Book.
class Book
{
public:Book(unsigned isbn, std::string const& name, std::string const& author, std::string const& pubdate):isbn_(isbn), name_(name), author_(author), pubdate_(pubdate){ }explicit Book(std::istream &in) { in >> isbn_ >> name_ >> author_ >> pubdate_;}private:unsigned isbn_;std::string name_;std::string author_;std::string pubdate_;
};