c++primer 第十章 类的构造函数与析构函数

main.cpp

#include <iostream>
#include "stock00.h"
int main()
{
    {
            using std::cout;
            cout << "Using constructors to create new objects\n";
            Stock stock1("NanoSmart", 12, 20.0);            // syntax 1
            stock1.show();
            Stock stock2 = Stock ("Boffo Objects", 2, 2.0); // syntax 2
            stock2.show();

            cout << "Assigning stock1 to stock2:\n";
            stock2 = stock1;
            cout << "Listing stock1 and stock2:\n";
            stock1.show();
            stock2.show();

            cout << "Using a constructor to reset an object\n";
            stock1 = Stock("Nifty Foods", 10, 50.0);    // temp object
            cout << "Revised stock1:\n";
            stock1.show();
            cout << "Done\n";
        }
        // std::cin.get();
        return 0;
}

stock00.cpp

//
// Created by sunshine on 19-4-30.
//
#include <iostream>
#include "stock00.h"//将从源文件所在的目录中搜索它
Stock::Stock()
{
    std::cout<<"Default constructor called\n";
    company="no name";
    shares=0;
    share_val=0.0;
    total_val=0.0;
}
//void Stock::acquire(const std::string & co, long n, double pr)
Stock::Stock(const std::string & co, long n, double pr)
{
    std::cout<<"Constructor using "<<co<<"called\n";
    company = co;
    if (n < 0)
    {
        std::cout << "Number of shares can't be negative; "
                  << company << " shares set to 0.\n";
        shares = 0;
    }
    else
        shares = n;
    share_val = pr;
    set_tot();
}
Stock::~Stock()
{
    std::cout<<"Bye, "<<company<<"!\n";
}
void Stock::buy(long num, double price)
{
    if (num < 0)
    {
        std::cout << "Number of shares purchased can't be negative. "
                  << "Transaction is aborted.\n";
    }
    else
    {
        shares += num;
        share_val = price;
        set_tot();
    }
}

void Stock::sell(long num, double price)
{
    using std::cout;
    if (num < 0)
    {
        cout << "Number of shares sold can't be negative. "
             << "Transaction is aborted.\n";
    }
    else if (num > shares)
    {
        cout << "You can't sell more than you have! "
             << "Transaction is aborted.\n";
    }
    else
    {
        shares -= num;
        share_val = price;
        set_tot();
    }
}

void Stock::update(double price)
{
    share_val = price;
    set_tot();
}

void Stock::show()
{
    std::cout << "Company: " << company
              << "  Shares: " << shares << '\n'
              << "  Share Price: $" << share_val
              << "  Total Worth: $" << total_val << '\n';
}

stock00.h

//
// Created by sunshine on 19-4-30.
//

#ifndef STOCK00_H
#define STOCK00_H
#include <string>

class Stock  // class declaration
{
private:
    std::string company;//公司的名称
    long shares;//股票的数量
    double share_val;//每股的价格
    double total_val;//股票总价格
    void set_tot() { total_val = shares * share_val; }
public:
    Stock();//默认构造函数
    //void acquire(const std::string & co, long n, double pr);//管理某一个公司的首次购买
    Stock(const std::string & co, long n=0, double pr=0.0);
    ~Stock();//析构函数
    void buy(long num, double price);//买入
    void sell(long num, double price);//卖出
    void update(double price);//更新
    void show();//显示
};    // note semicolon at the end
#endif //STOCK00_H

程序输出结果:

Using constructors to create new objects
Constructor using NanoSmartcalled
Company: NanoSmart  Shares: 12
  Share Price: $20  Total Worth: $240
Constructor using Boffo Objectscalled
Company: Boffo Objects  Shares: 2
  Share Price: $2  Total Worth: $4
Assigning stock1 to stock2:
Listing stock1 and stock2:
Company: NanoSmart  Shares: 12
  Share Price: $20  Total Worth: $240
Company: NanoSmart  Shares: 12
  Share Price: $20  Total Worth: $240
Using a constructor to reset an object
Constructor using Nifty Foodscalled
Bye, Nifty Foods!
Revised stock1:
Company: Nifty Foods  Shares: 10
  Share Price: $50  Total Worth: $500
Done
Bye, NanoSmart!
Bye, Nifty Foods!

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纷繁中淡定

你的鼓励是我装逼的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值