【13.4】c++ primer plus 课后编程答案
来源:互联网 发布:深入浅出mysql第三版 编辑:程序博客网 时间:2024/06/06 17:17
C++ PRIMER PLUS 课后答案
使用IDE为window7系统下的VS2010
/*user.h*/#ifndef USERSH_H_#define USERSH_H_#include <string>using std::ostream;using std::istream;using std::string;class Port{private:char * brand;char style[20];int bottles;public:Port(const char * br = "none", const char *st = "none", int b = 0);Port(const Port & p);virtual ~Port(){delete [] brand;}Port & operator = (const Port & p);Port & operator += (int b);Port & operator -= (int b);int BottleCount()const {return bottles;}virtual void Show() const;friend ostream & operator << (ostream & os, const Port & p);};class VintagePort : public Port{private:char * nickname;int year;public:VintagePort();VintagePort(const char * br, const char *st, int b, const char *nm, int y);VintagePort(const VintagePort & vp);~VintagePort() {delete [] nickname;}VintagePort & operator = (const VintagePort & vp);void Show() const;friend ostream & operator << (ostream & os, const VintagePort & vp);};#endif
/*userfucntion.cpp*/#include "usersh.h"#include <iostream>#include <cstring>using std::cout;using std::cin;using std::endl;using std::string;using std::ostream;using std::istream;Port::Port(const char * br /* = "none" */, const char * st /* = "none" */, int b /* = 0 */){brand = new char [strlen(br) + 1];strcpy(brand, br);strcpy(style, st);bottles = b;}Port::Port(const Port & p){brand = new char [strlen(p.brand) + 1];strcpy(brand, p.brand);strcpy(style, p.style);bottles = p.bottles;}Port & Port::operator = (const Port & p){if(this == &p)return * this;delete [] brand;brand = new char [strlen(p.brand) + 1];strcpy(brand, p.brand);strcpy(style, p.style);bottles = p.bottles;return * this;}Port & Port::operator += (int b){bottles += b;return * this;}Port & Port::operator -=(int b){bottles -= b;return * this;}void Port::Show()const{cout << "Brand: " << brand <<endl;cout << "Style: " << style <<endl;cout << "Bottles: " << bottles <<endl;}ostream & operator << (ostream & os, const Port & p){os << p.brand <<", ";os << p.style <<", ";os << p.bottles <<", ";return os;}VintagePort::VintagePort():Port(){nickname = new char [5];strcpy(nickname, "none");year = 0;}VintagePort::VintagePort(const char * br, const char *st, int b, const char *nm, int y): Port(br,st,b){nickname = new char [strlen(nm) + 1];strcpy(nickname, nm);year = y;}VintagePort::VintagePort(const VintagePort & vp):Port(vp){nickname = new char [strlen(vp.nickname) + 1];strcpy(nickname, vp.nickname);year = vp.year;}VintagePort & VintagePort::operator = (const VintagePort & vp){if(this == &vp)return * this;Port::operator =(vp);delete [] nickname;nickname = new char [strlen(vp.nickname) + 1];strcpy(nickname, vp.nickname);year = vp.year;return * this;}void VintagePort::Show()const{Port::Show();cout << "nickname: " << nickname <<endl;cout << "year: " << year <<endl;}ostream & operator << (ostream & os, const VintagePort & vp){os << (const Port &)vp;os << vp.nickname << ", ";os << vp.year << ". ";return os;}
/*main*/#include <iostream>#include <Windows.h>#include <cstring>#include "usersh.h"using std::cout;using std::cin;using std::endl;int main(){ Port a;a.Show();Port b("123", "456", 20);cout << b << endl;b+=1;b.Show();a=b;a-=2;cout << a << endl << endl;VintagePort aa;aa.Show();VintagePort bb("aaa", "bbb", 1, "ccc", 1995);aa=bb;aa+=1;aa.Show();aa-=2;aa.Show();cout << aa << endl;system("pause");return 0;}
阅读全文
0 0
- C++primer plus第六版课后编程题答案13.4
- C++primer plus第六版课后编程练习答案13.4
- 【13.4】c++ primer plus 课后编程答案
- C++PRIMER PLUS第六版课后编程答案 4.1-4.5
- C++PRIMER PLUS第六版课后编程答案 4.6-4.10
- C++PRIMER PLUS第六版课后编程答案 5.1-5.5
- C++PRIMER PLUS第六版课后编程答案 5.6-510
- C++primer plus第六版课后编程题答案 6.1
- C++primer plus第六版课后编程题答案 6.2
- C++primer plus第六版课后编程题答案 6.3
- C++primer plus第六版课后编程题答案 6.4
- C++primer plus第六版课后编程题答案 6.5
- C++primer plus第六版课后编程题答案 6.6
- C++primer plus第六版课后编程题答案 6.7
- C++primer plus第六版课后编程题答案 6.8
- C++primer plus第六版课后编程题答案 6.9
- C++primer plus第六版课后编程题答案7.1
- C++primer plus第六版课后编程题答案7.2
- 【转】中国与华尔街不同的投行人生
- 【转】sublime text 3 注册码
- どゆことの意味は何ですか?
- C语言学习心得一:__stdcall…
- 融资融券”(securities margi…
- 【13.4】c++ primer plus 课后编程答案
- 【转】 C++ 右值引用详…
- kafka java代码编写
- crond和crontab的tag
- php中的session性能
- 438. Find All Anagrams in a String
- Solr对数据库中的CLOB字段建立索引的问题
- kmalloc vmalloc kzalloc malloc 和 get_free_page()
- 为什么要学习Kotlin