zoukankan      html  css  js  c++  java
  • 2020/3/4

    网课+作业:5小时

    cf:2小时

    树哈希知识学习:1小时

    英语单词:1小时

    回顾c++第10章:1小时

    #include"iostream"
    #include"cstring"
    
    class Stock
    {
    private:
        char company[30];
        int shares;
        double share_val;
        double total_val;
        void set_tot(){///类声明常将短小的成员函数作为内联函数
            total_val = shares * share_val;
        }
    public:///定义于类声明中的函数都将自动成为内联函数
        Stock();
        Stock(const char *co,int n,double pr);///构造函数
        ~Stock();///析构函数
        void buy(int num,double price);
        void sell(int num,double price);
        void update(double price);
        void show();
        const Stock & topval(const Stock & s) const;
    };
    
    Stock :: Stock(const char * co,int n,double pr)
    {
       // std::cout << "Constructor using " ;
        std::strncpy(company,co,29);
        company[29] = '';
        if(n < 0)
        {
            std :: cerr << "Number of shares can't be negative. "
            << company << " shares set to 0.
    ";
            shares = 0;
        }
        else
            shares = n;
        share_val = pr;
        set_tot();
    }
    Stock :: Stock()
    {
        std :: cout <<" Default constructor called
    ";
        std :: strcpy(company,"no name");
        shares = 0;
        share_val = 0.0;
        total_val = 0.0;
    }
    Stock :: ~Stock()
    {
        std :: cout << "Bye, " << company << "!
    ";
    }
    void Stock :: buy(int num,double price)
    {
        if(num < 0)
        {
            std:: cerr<<"Number of shares purchased can't be negative. "
            << "Transaction is aborted.
    ";
        }
        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 sold can't be negative."
            << "Transaction is aborted.
    ";
        } else if(num > shares)
        {
            cerr << "You can't sell more than you have! "
            << "Transaction is aborted.
    ";
        } 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 <<" Share Price: $" << share_val
        << " Total Worth: $" << total_val << endl;
    }
    const Stock & Stock :: topval(const Stock & s)const
    {
        if(s.total_val > total_val) return s;
        else return *this;
    }
    int main()
    {
        using std :: cout;
        using std :: ios_base;
        cout.precision(2);
        cout.setf(ios_base :: fixed,ios_base :: floatfield);
        cout.setf(ios_base::showpoint);
    
        cout << "Using constructors to create new objects
    ";
        Stock stock1("NanoSmark",12,20.0);
        stock1.show();
        Stock stock2 = Stock("Boffo Objects",2,2.0);
        stock2.show();
    
        cout << "Assigning stock1 to stock2: 
    ";
        stock2 = stock1;
        cout << "Listing stock1 ans stock2: 
    ";
        stock1.show();
        stock2.show();
        cout << "Using a constructor to reset an object
    ";
        stock1 = Stock("Nofty Foods",10,50.0);///这里注意,因stock1是有值了的
        ///所以这里并不是stock1再次调用构造函数,而是有一个匿名对象赋值给
        ///stock1,赋值完成后,该匿名对象执行析构函数
        cout << "Revised stock1: 
    ";
        stock1.show();
        cout << "Done
    ";
    
        const Stock land = Stock("Kludgehorn Properties");
        //land.show();///注意这条语句会出错,因为无法保证,show函数,不对land进行更改
        ///除非在定义show()函数的时候定义为:void show() const;保证函数不会修改调用对象
    
        return 0;
    }
    
  • 相关阅读:
    Linux基础命令grep(如何过滤字符串)
    Linux基础命令wc(如何统计文件的行数?如何统计文件的字节数?)
    Linux基础命令tr(如何替换字符)
    django4
    django3
    django2
    django1
    jQuery2
    jQuery1
    事件
  • 原文地址:https://www.cnblogs.com/yrz001030/p/12417390.html
Copyright © 2011-2022 走看看