初识类
来源:互联网 发布:算法精解 java 编辑:程序博客网 时间:2024/06/08 20:01
#define STOCK00_H_
#include <string>
class Stock
{
private://这里面的对象和函数都是不给用户直接调用的,而是通过调用public函数
//来使用到这里的函数和对象。
std::string company;
long shares;
double share_val;
double total_val;
void set_tot(){ total_val = shares*share_val; }
public://这里只是定义了函数的接口和参数类型,但没有具体的实现方式。
void acquire(const std::string & co, long n, double pr); //这里的第一个参数是指向字符串的指针,该字符串用于初始化成员函数company。
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show();
};
#endif
这一段是定义头文件,但是并没有指明下面这些函数的具体实现方式。注意,private里面的void set_tot(){ total_val = shares*share_val; }是不用外面重写的,直接当对象调用。
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include"stdafx.h"
#include<iostream>
#include"stock00.h"//包含头文件,否则下面的Stock,company,shares之类的关键词就会出错,
//会显示未定义的标识符因为根本不知道,这到底是什么,
void Stock::acquire(const std::string & co, long n, double pr)
//这是对头文件中的void acquire(const std::string & co, long n, double pr);进行定义细化。
{
company = co;
if (n < 0)
{
std::cout << "Number of share can't be negative;" << company << "shares set to 0.\n";
shares = 0;
}
else
{
shares = n;
share_val = pr;
set_tot();//这里shares share_val和set_tot等函数对象都不在acquire里面,但是在Stock里面,而且前面的是在private里面的函数,
//而acquire是public里面的函数,那么是不是说public的函数可以直接调用private的函数。
}
}
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 << " Share :" << shares << '\n'
<< " Share price:$ " << share_val
<< " Total worth: $" << total_val << '\n';
}
这一段是对上面的头文件中的public中的函数进行定义,头文件里面只是给了一个借口,和参数的形式,但是并没有指明这到底怎么用,这里是给函数细化。源文件,很重要,
这里也是说明里头文件里面的public和private的参数,函数的关系。
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include"stdafx.h"
#include<iostream>
#include"stock00.h"//一定要包含头文件。
int main()
{
Stock fluffy_the_cat;//这里便是类->对象的应用。fluffy_the_cat是class Stock的对象,
//所以Stock里面的所以参数,函数都可以被它应用。
fluffy_the_cat.acquire("NanoSmart", 20, 12.50);//////////////////////fluffy_the_cat.acquire("NanoSmart", 20, 12.50);应用类里面的参数的方式即用“.”中间过度。
fluffy_the_cat.show();
fluffy_the_cat.buy(15, 18.125);
fluffy_the_cat.show();
fluffy_the_cat.sell(400, 20.00);
fluffy_the_cat.show();
fluffy_the_cat.buy(300000, 40.125);
fluffy_the_cat.sell(300000, 0.125);
fluffy_the_cat.show();
return 0;
}
- 初识类
- 初识MFC窗口类
- 初识工厂类:
- 初识枚举类
- 初识Qt Object 类
- 类的初识
- c++初识类
- 初识Android Bundle类
- 对类的初识
- 初识Calendar类
- C++学习:初识类
- 初识c++的类
- 初识类与对象
- php 初识抽象类
- 初识类和对象
- 初识StringBuffer类
- JAVA 初识Class类
- Java 包装类初识
- folder汇总字段的实现
- Spring4.2第一个小例子
- 从零开始搭建架构实施Android项目
- NFC技术的基础应用
- iOS x-www-form-urlencoded格式传参
- 初识类
- 12、String类
- gedit中文乱码问题
- 最大子序列和问题
- 近200篇机器学习&深度学习资料分享
- Android 如何从系统图库中选择图片
- hadoop常用算法简单实例
- Spring mvc常用注解
- 一位工程师的FPGA项目开发经验总结