C++ Premier Plus 6th edition - Programming excercise - Chapter13 - 4

port.h

// class declarations
#ifndef PORT_H_
#define PORT_H_

#include <iostream>
using namespace std;
class Port
{
private:
    char * brand;
    char style[20]; // i.e., tawny, ruby, vintage
    // suppose max bottle num is 50
    static const int max = 50;
    int bottles;
public:
    Port(const char * br = "none", const char * st = "none", int b = 0);
    Port(const Port & p); // copy constructor
    virtual ~Port()
    {
        delete[] brand;
    }
    Port & operator=(const Port & p);
    Port & operator+=(int b); // adds b to bottles
    Port & operator-=(int b); // subtracts b from bottles, if available
    int BottleCount() const
    {
        return bottles;
    }
    virtual void Show() const;
    friend ostream & operator<<(ostream & os, const Port & p);
};

class VintagePort : public Port // style necessarily = "vintage"
{
private:
    char * nickname; // i.e., "The Noble" or "Old Velvet", etc.
    int year; // vintage year
public:
    VintagePort();
    VintagePort(const char * br,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

port.cpp

// implementations for port and VintagePort
#include<iostream>
#include"port.h"
// implementation for class-Port
Port::Port(const char * br, const char * st, int b)
{
    int len = strlen(br)+1;
    brand = new char[len];
    strcpy_s(brand, len, br);
    strcpy_s(style, strlen(st) + 1, st);
    while (b < 0 || b > max)
    {
        cout << "Invalid input,can't be negative or more than 50,enter bottle num again: ";
        cin >> b;
    }
    bottles = b;
}

Port::Port(const Port & p)
{
    int len = strlen(p.brand) + 1;
    brand = new char[len];
    strcpy_s(brand, len, p.brand);
    strcpy_s(style, strlen(p.style) + 1, p.style);
    bottles = p.bottles;
}

Port & Port::operator=(const Port & p)
{
    if (style == p.brand)
        return *this;
    delete[]brand;
    int len = strlen(p.brand) + 1;
    brand = new char[len];
    strcpy_s(brand, len, p.brand);
    strcpy_s(style, strlen(p.style) + 1, p.style);
    bottles = p.bottles;
    return *this;
}

Port & Port::operator+=(int b)
{
    int temp = bottles + b;
    while (b < 0 || temp > max)
    {
        cout << "Invalid input,can't be negative or more than 50,enter bottle num again: ";
        cin >> b;
    }
    bottles = temp;
    return *this;
}
// adds b to bottles
Port & Port::operator-=(int b)
{
    int temp = bottles - b;
    while (b < 0 || temp < 0)
    {
        cout << "Invalid input,can't be negative or more than current bottles,enter bottle num again: ";
        cin >> b;
    }
    bottles = temp;
    return *this;
}

void Port::Show() const
{
    cout << "\nBrand: " << brand << endl
         << "Kind: " << style << endl
         << "Bottles:" << bottles << endl;
}

ostream & operator<<(ostream & os, const Port & p)
{
    os << "\nBrand: " << p.brand << endl
       << "Kind: " << p.style << endl
       << "Bottles:" << p.bottles << endl;
    return os;
}

// implementations for claa-VintagePort
VintagePort::VintagePort():Port()
{
    nickname = nullptr;
    year = 0;
}

// no style in arguments,but class need it,transfer a given const char*
// as this derived class is specified for vintage
VintagePort::VintagePort(const char * br, int b, const char * nn, int y):Port(br,"Vintage",b)
{
    int len = strlen(nn) + 1;
    nickname = new char[len];
    strcpy_s(nickname, len, nn);
    year = y;
}

VintagePort::VintagePort(const VintagePort & vp):Port(vp)
{
    int len = strlen(vp.nickname) + 1;
    nickname = new char[len];
    strcpy_s(nickname, len, vp.nickname);
    year = vp.year;
}

VintagePort & VintagePort::operator=(const VintagePort & vp)
{
    if (nickname == vp.nickname)
        return *this;
    delete[]nickname;
    Port::operator=(vp);
    int len = strlen(vp.nickname) + 1;
    nickname = new char[len];
    strcpy_s(nickname, len, vp.nickname);
    year = vp.year;
    return *this;
}

void VintagePort::Show() const
{
    Port::Show();
    cout << "Nicknmae: " << nickname << endl;
    cout << "Year: " << year <<endl;
}

ostream & operator<<(ostream & os, const VintagePort & vp)
{
    os << (Port)vp;// explicitly involk
    if(vp.nickname==nullptr)
        os << "Nickname: " << "none" << endl;
    else
        os << "Nickname: " << vp.nickname << endl;
    os << "Year: " << vp.year << endl;
    return os;
}

main

// main()
#include<iostream>
#include"port.h"
using std::cout;
using std::endl;

int main()
{
    Port p1;
    cout << p1;

    Port p2("brandA", "sytle1", 20);
    cout << p2;

    p2 += 25;
    cout << p2;// bottles should be 45

    p2 -= 10;
    cout << p2;// bottles should be 35

    p1 = p2;
    cout << p1;// bottles should be 35

    p1.Show();

    cout << "There are " << p1.BottleCount() << " bottles in p1" << endl;

    cout << "\n\nNow turns to VintagePort class:\n";
    VintagePort vp1;
    cout << vp1;

    VintagePort vp2("BrandVP", 20, "Vintage", 2018);
    cout << vp2;

    vp2 += 13;
    cout << vp2;// bottles should be 23

    vp2 -= 10;
    cout << vp2;// bottles should be 13

    vp1 = vp2;
    cout << vp1;

    vp1.Show();

    cout << "There are " << vp1.BottleCount() << " bottles in vp1" << endl;
    cin.get();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值