应聘网站运营建设面试,wordpress一栏一个主题,做网站 360,群晖服务器可做网站服务器么文章目录练习7.31练习7.32练习7.33练习7.34练习7.35练习7.36练习7.37练习7.38练习7.29练习7.40练习7.31 定义一对类X 和Y#xff0c;其中X 包含一个指向 Y 的指针#xff0c;而Y 包含一个类型为 X 的对象。 class Y;class X{Y* y nullptr;
};class Y{X x;
};练习7.32 定义你…
文章目录练习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 stringclass Screen;class Window_mgr
{
public:using ScreenIndex std::vectorScreen::size_type;inline void clear(ScreenIndex);private:std::vectorScreen 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_;
};