设为首页 加入收藏

TOP

C++的对象和类(一)
2023-07-23 13:33:12 】 浏览:54
Tags:

一、问题引入

区分面向过程编程和面向对象编程的最大的特性就是 ,类是一种将抽象转换为用户定义类型的C++工具,它将数据表示和操纵数据的方法组合成一个整洁的包。

那么如何声明类、定义类、调用类?

C++ Primer Plus:中文版 (第六版) 的股票类举例说明。

二、解决过程

2-1 类抽象

股票类的抽象化

  • 获得股票

  • 增持股票

  • 减持股票

  • 更新股票价格

  • 显示所持股票的信息

股票的数据类型抽象化

  • 发行股票公司名称

  • 所持股票的数量

  • 股票的单价

  • 股票总值

2-2 类的代码实现

1?? stock.h

#ifndef __STOCK_H__
#define __STOCK_H__

#include <string>

// 类的声明
class Stock {
public:
    Stock(); // default constructor
    Stock(const std::string & co, long n = 0, double pr = 0.0); // constructor prototype with some default arguments
    ~Stock(); // noisy destructor

    void get_stock(const std::string & co, long n, double pr);  // 获得股票
    void add_stock(long num, double price);                     // 增持股票
    void sell_stock(long num, double price);                    // 减持股票
    void update_price(double price);                            // 更新股票价格
    void show();                                                // 显示所持股票的信息

private:
    std::string company; // 发行股票公司名称
    long shares;         // 所持股票的数量
    double share_val;    // 股票的单价
    double total_val;    // 股票总值
    void set_total() {total_val = shares * share_val;};
};

#endif

2?? stock.cpp

#include "stock.h"
#include <iostream>

// default constructor
Stock::Stock()
{
    company = "no name";
    shares = 0;
    share_val = 0.0;
    total_val = 0.0;
}

// class destructor
Stock::~Stock() // verbose class destructor
{

}
// 构造函数的参数表示的不是类成员,而是赋给类成员的值。因此,参数名不能与类成员相同,否则出现同名混乱
Stock::Stock(const std::string & co, long n, double pr) 
{
    company = co;
    if(n < 0)
    {
        std::cerr << "Number of shares can't be negative; "
                  << company << " shares set to 0.\n";
        shares = 0;
    }
    else
        shares = n;
    share_val = pr;
    set_total();
}

/* other methods */

void Stock::get_stock(const std::string & co, long n, double pr) 
{
    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_total();
}

void Stock::add_stock(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_total();
    }
}

void Stock::sell_stock(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_total();
    }
}

void Stock::update_price(double price) 
{
    share_val = price;
    set_total();
}

void Stock::show() 
{
    using std::cout;
    using std::endl;
    using std::ios_base;
    // set format to #.###
    ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
    std::streamsize prec = cout.precision(3);

    cout << "Stock Information:" << endl;
    cout << "Company company: " << company << endl;
    cout << "Number of Stocks: " << shares << endl;
    cout << "Price per Stock: $" << share_val<< endl;

    // set format to #.##
    cout.precision(2);
    cout << "Total Value: $" << total_val<< endl;

    // restore original format
    cout.setf(orig, ios_base::floatfield);
    cout.precision(prec);
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++ 测试框架 GoogleTest 初学者.. 下一篇【Visual Leak Detector】在 VS 2..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目