一、问题引入

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

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

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

二、解决过程2-1 类抽象

股票类的抽象化

  • 获得股票

  • 增持股票

  • 卖出股票

  • 更新股票价格

  • 显示所持股票的信息

股票的数据类型抽象化

  • 发行股票公司名称

  • 所持股票的数量

  • 股票的单价

  • 股票总值

2-2 类的代码实现

1️⃣ stock.h

#ifndef __STOCK_H__#define __STOCK_H__#include // 类的声明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 // default constructorStock::Stock(){}// class destructorStock::~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. "             < 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);}

3️⃣ stock_call.cpp

#include #include "..\inc\stock.h"int main(void){    Stock fluffy_the_cat("NanoSmart", 0, 0.0);    fluffy_the_cat.get_stock("NanoSmart", 20, 12.50);    fluffy_the_cat.show();    std::cout << '\n';    fluffy_the_cat.add_stock(15, 18.125);    fluffy_the_cat.show();    std::cout << '\n';    fluffy_the_cat.sell_stock(400, 20);    fluffy_the_cat.show();    std::cout << '\n';    fluffy_the_cat.add_stock(300000, 40.125);    fluffy_the_cat.show();    std::cout << '\n';    fluffy_the_cat.sell_stock(300000, 0.125);    fluffy_the_cat.show();    return 0;}

运行结果

三、反思总结

一般来说,类规范由两部分组成。

  • 类声明:以数据成员的方式描述数据部分,以成员函数(即方法)的方式描述公有接口

  • 类方法定义:描述如何实现类成员函数

四、参考引用

C++ Primer Plus:中文版 (第六版)