c++学习(五)

#ifndef STOCK20
#define STOCK20

#include "string.h"
//定义类的各种成员属性和成员方法
class Stock
{
private:
    std::string company;
    long shares;
    double share_val;
    double total_val;
    void set_tot() {total_val = shares*share_val;}
public:
    //建立两个构造函数
    Stock();
    Stock(const std::string&co,long n,double pr);
    ~Stock();
    void buy(long num,double price);
    void sell(long num,double price);
    void updata(double price);
    void show() const;
    const Stock & topval(const Stock & a) const;
    //前面一个stock作为的是一个stock的对象 后面一个也是一个stock的对象
};//最后要加分号




#endif // STOCK20
#include 

#include"stock20.h"

Stock::Stock()
{
    std::cout<<"Default constructor called\n";
    company = "no name";
    shares = 0;
    share_val = 0.0;
    total_val = 0.0;
}
Stock::Stock(const std::string &co, long n, double pr)
{
    std::cout<<"Constructor using "<shares)
    {
        std::cout<<"You can not sell so mush"<show();

{
    using std::cout;
    using std::endl;
    using std::ios_base;
    ios_base::fmtflags orig =
            cout.setf(ios_base::fixed,ios_base::floatfield);
    //定义这是流文件的长度
    std::streamsize prec = cout.precision(3);

    cout<<"Company: " <total_val)//后面的是代表当前的stock
        return a;
    else
        return *this;//这里的意思是返回一整个对象

}



#include 
#include"stock20.h"
const int STKS = 4;

int main()
{
    std::cout<<"zzm";
    Stock stocks[STKS] =
    {
        Stock{"NanoSmart",12,20},
        Stock{"Boffo Objects",200,2},
    };//到最后都调用了析构函数
    std::cout<<"Stock holdings:\n";

    int st;
    for(st=0;sttopval(stocks[st]);//top->...返回的是一个值
    //最大的指针是指向最大值的
    std::cout<<"\nMost valuable holding:\n";
    top->show();
    return 0;
}

面向对象的章节的内容

你可能感兴趣的:(c++)