【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 \n";
             m_shares = 0;
     }
     else
     {
             m_shares = n;
     }
     m_share_val = pr;
     set_tot();
}

//析构函数
Stock::~Stock()
{
    std::cout << "Bye " << m_company << " ~\n";
}

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 \n";
                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.\n";
    }
    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.\n";
    } 
        else if(num > m_shares)
    {
        cout << "You can't sell more than you have!" <<  "Transaction is aborted.\n";
    }
    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 << '\n'
                  << "   Share price: $" << m_share_val
                  << "   Total worth: $" << m_total_val << '\n';
}


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 << "################################################" << '\n';

        Stock b("tencent", 120, 30.2);
        b.show();
        std::cout << "################################################" << '\n';

        {
            Stock c = Stock("hello", 100, 12.3);
            c.show();
        }  //作用域结束,对象c会消失,调用析构函数

        std::cout << "main is end\n";

        return 0;
}

 

编译:

  

g++ stock00.cpp -o test

 

运行:

./test

 

运行结果:

 

 

 

 

 

总结:

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

 

posted @ 2021-08-04 15:26  小小小p鱼  阅读(470)  评论(0编辑  收藏  举报