#ifndef PORT_H_
#define PORT_H_
#include <iostream>
using namespace std;
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* nn, 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
#include<iostream>
#include"diyi.h"
#pragma warning(disable:4996)
Port::Port(const char* br, const char* st, int b)
{
brand = new char[std::strlen(br) + 1];
std::strcpy(brand, br);
std::strncpy(style, st, 19);
bottles = b;
}
Port::Port(const Port& p)
{
brand = new char[std::strlen(p.brand) + 1];
std::strcpy(brand, p.brand);
std::strncpy(style, p.style, 19);
bottles = p.bottles;
}
Port& Port::operator=(const Port& p)
{
if (this == &p)
return *this;
delete[]brand;
brand = new char[std::strlen(p.brand) + 1];
std::strcpy(brand, p.brand);
std::strncpy(style, p.style, 19);
bottles = p.bottles;
return *this;
}
Port& Port::operator+=(int b)
{
bottles += b;
return *this;
}
Port& Port::operator-=(int b)
{
if (b > bottles)
std::cout << "no!";
bottles -= b;
return *this;
}
void Port::Show()const
{
std::cout << "brand: " << brand << std::endl;
std::cout << "kind: " << style << std::endl;
std::cout << "bottles: " << bottles << std::endl;
}
std::ostream& operator<<(std::ostream& os, const Port& p)
{
os << p.brand << " ," << p.style << " ," << p.bottles << std::endl;
return os;
}
VintagePort::VintagePort():Port()
{
nickname = 0;
year = 0;
}
VintagePort::VintagePort(const char* br, const char* st, int b,const char* nn, int y):Port(br,st,b)
{
nickname = new char[std::strlen(nn) + 1];
std::strcpy(nickname, nn);
year = y;
}
VintagePort::VintagePort(const VintagePort& vp):Port(vp)
{
nickname = new char[std::strlen(vp.nickname) + 1];
std::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[std::strlen(vp.nickname) + 1];
std::strcpy(nickname, vp.nickname);
year = vp.year;
return *this;
}
void VintagePort::Show()const
{
Port::Show();
std::cout << "nickname: " << nickname << std::endl;
std::cout << "year: " << year << std::endl;
}
std::ostream& operator<<(std::ostream& os, const VintagePort& vp)
{
os << (const Port&)vp;
os << "nickname; " << vp.nickname << std::endl;
os << "year: " << vp.year << std::endl;
return os;
}
#include <iostream>
#include <string>
#include "diyi.h"
using std::cout;
using std::endl;
int main()
{
Port port1("gallo", "tawny", 20);
cout << port1 << endl << endl;
VintagePort vp("gallo", "vintage", 24, "Old Velvet", 16);
VintagePort vp2(vp);
cout << vp2 << endl << endl;
VintagePort vp3;
vp3 = vp;
cout << vp3 << endl << endl;
Port* p_port;
p_port = &port1;
p_port->Show();
cout << endl;
p_port = &vp;
p_port->Show();
cout << endl;
return 0;
}