• 【C++入门课程一】类的私有成员、共有成员


    环境: linux

    编译器:  g++

    学习C++,首先要学习类的使用,下面的代码,包含了私有成员、共有成员的使用,话不多说,我们先跑起来。

    创建 stock00.h  文件

    #ifndef STOCK00_H_
    #define STOCK00_H_
    
    #include <string.h>
    
    class Stock
    {
    private:
            std::string m_company;
            long m_shares;
            double m_share_val;
            double m_total_val;
            void set_tot();
    public:
            Stock();
            Stock(const std::string &co, long n, double pr);
            ~Stock();
        void acquire(const std::string &co, long n, double pr);
        void buy(long num, double price);
        void sell(long num, double price);
        void update(double price);
        void show();
    };
    
    #endif

    创建 stock00.cpp  文件

    #include <iostream>
    #include "stock00.h"
    
    //构造函数
    Stock::Stock()
    {
      m_company = "huawei";
      m_shares = 100;
      m_share_val = 5.4;
      set_tot();
    }
    
    Stock::Stock(const std::string &co, long n, double pr)
    {
         m_company = co;
         if(n < 0)
         {
                 std::cout << "Number of shares can't be negative; " << m_company << " shares set to 0 
    ";
                 m_shares = 0;
         }
         else
         {
                 m_shares = n;
         }
         m_share_val = pr;
         set_tot();
    }
    
    //析构函数
    Stock::~Stock()
    {
        std::cout << "Bye " << m_company << " ~
    ";
    }
    
    inline void Stock::set_tot()
    {
        m_total_val = m_shares * m_share_val;
    }
    
    void Stock::acquire(const std::string & co, long n, double pr)
    {
            m_company = co;
        if(n < 0)
        {
                    std::cout << "Number of shares can't be negative; " << m_company << " shares set to 0 
    ";
                    m_shares = 0;
        }
        else
        {
                    m_shares = n;
        }
            m_share_val = pr;
        set_tot();
    }
    
    void Stock::buy(long num, double price)
    {
        if(num < 0)
        {
            std::cout << "Number of shares can't be negative; " <<  "Transaction is aborted.
    ";
        }
        else
        {
                    m_shares += num;
                    m_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.
    ";
        } 
            else if(num > m_shares)
        {
            cout << "You can't sell more than you have!" <<  "Transaction is aborted.
    ";
        }
        else
        {
                    m_shares -= num;
                    m_share_val = price;
            set_tot();
        }
    }
    
    void Stock::update(double price)
    {
            m_share_val = price;
        set_tot();
    }
    
    void Stock::show()
    {
            std::cout << "Company: " << m_company
                      << "   Shares:" << m_shares << '
    '
                      << "   Share price: $" << m_share_val
                      << "   Total worth: $" << m_total_val << '
    ';
    }
    
    
    int  main()
    {
        Stock a;
            //a.sales = 3;  此行是错误的,私有成员是无法直接赋值的,只能通过成员函数赋值。
    
            a.show();
            a.buy(20, 2.8);
            a.show();
            a.sell(30, 3.0);
            a.show();
            std::cout << "################################################" << '
    ';
    
            Stock b("tencent", 120, 30.2);
            b.show();
            std::cout << "################################################" << '
    ';
    
            {
                Stock c = Stock("hello", 100, 12.3);
                c.show();
            }  //作用域结束,对象c会消失,调用析构函数
    
            std::cout << "main is end
    ";
    
            return 0;
    }

    编译:

      

    g++ stock00.cpp -o test

    运行:

    ./test

    运行结果:

    总结:

    public: 能被类成员函数、子类函数、友元访问,也能被类的对象访问。
    private: 只能被类成员函数及友元访问,不能被其他任何访问,本身的类对象也不行。
    protected: 只能被类成员函数、子类函数及友元访问,不能被其他任何访问,本身的类对象也不行。

    Talk is cheap, show me the code
  • 相关阅读:
    c++实现执行脚本命令
    常用命令
    ssh2.cpp
    通过scp传输文件到多个主机
    启动和监控脚本
    centos 7.2 svn 安装
    SVN提交时必须填写备注信息 配置
    centos 7.2 配置mysql
    centos 7.2 配置Nginx
    js调试的时候用console.log("变量"+scrollTop+windowHeight)
  • 原文地址:https://www.cnblogs.com/birdBull/p/15099081.html
Copyright © 2020-2023  润新知