初识类

来源:互联网 发布:算法精解 java 编辑:程序博客网 时间:2024/06/08 20:01
#ifndef STOCK00_H_//定义头文件的方式,这个头文件名就是“stock00.h”注意#ifndef和#define在开头,而#endif在结尾。
#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;


}


0 0
原创粉丝点击