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;
}