C++ Prime plus 类和对象

面向对象(oop):
• 抽象
• 封装和数据隐藏
• 继承
• 代码可重用性

类规范:
• 类声明:以数据成员的方式描述数据部分,以成员函数(方法)的方式描述共有接口。类的内存大小,就是数据成员的内存之和
• 类方法定义:描述如何实现类成员函数

类申明:头文件

//File : Cplusplusplus_10_.hpp
#include<iostream>
#include<string>
#include<cstring>

//Stock 类
class Stock {   //class declaration
private:
    char company[30];
    int shares;  //股份
    double share_val;
    double total_val;
    //定义位于类声明中的函数自动成为内联函数,省去了调用函数的步骤
    //void set_tot() { total_val = shares * share_val; }
    void set_tot();
public:
    void acquire(const char *co, int n, double pr);
    void buy(int num, double price);
    void sell(int num, double price);
    void update(double price);
    void show();
};  //note semicolon at the end

//内联函数也可以写在类声明之外,内联函数的特殊规则要求每个使用他们的文件都对其进行定义,
//因此简便的方法是:将内联定义放在定义类的头文件中
inline void Stock::set_tot()
{
    total_val = shares * share_val;
}

实现类成员函数

//File : Cplusplusplus_10_1.hpp
#include "cplusplusplus_10_.hpp"

//more stocks.cpp -- implementing the class member functions

//实现类成员函数
/*
使用域解析操作符(::)来标识函数所属的类
类方法可以访问类的private组件
*/


void Stock::acquire(const char *co, int n, double pr) {
    std::strncpy(company, co, 29); //truncate co to fit company
    company[29] = '\0'; //保证输入的是字符串
    if (n < 0) {
        std::cerr << "number of shares cannot be negative."
            << company << "shares set to be 0.\n";
        shares = 0;
    }
    else
        shares = n;    //占多少股份
    share_val = pr;    //每股多少钱
    set_tot();
}

void Stock::buy(int num, double price) {
    if (num < 0) {
        std::cerr << "number of shares purchased cannot be negative."
            << "Transacton is aborted.\n";
    }
    else {
        shares += num;
        share_val = price;
        set_tot();
    }
}

void Stock::sell(int num, double price) {
    using std::cerr;
    if (num < 0) {
        cerr << "number of shares purchased cannot be negative."
            << "Transacton is aborted.\n";
    }
    else if (num > shares) {
        cerr << "you cannot sell more than you have."
            << "Transacton is aborted.\n";
    }
    else {
        shares -= num;
        share_val = price;
        set_tot();
    }
}

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

void Stock::show() {
    using std::cout;
    using std::endl;
    cout << "company: " << company 
        << " shares: " << shares << endl
        << " shares price: $" << share_val 
        << " total worth: $" << total_val << endl;
}

使用类

#include "cplusplusplus_10_2.hpp"

//使用类
int main() {
    using std::cout;
    using std::ios_base;
    //创建类,可以申明类对象,也可以使用new为类对象分配存储空间。
    Stock kate;  //申明了就分配了内存空间?
    kate.acquire("Baidu", 20, 12.50);
    cout.setf(ios_base::fixed); //use fixed decimal point format 使用固定的小数点格式 
    cout.precision(2);  //two places to right of decimal 小数点后两位 
    cout.setf(ios_base::showpoint); //show trailing zeros  显示尾随0 
    kate.show();
    kate.buy(15, 18.5);
    kate.show();
    kate.sell(400, 20.0);
    kate.show();
    return 0;
}

运行结果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值