初学OOP --- 类的使用

Class.h

#pragma once

#include<string>
using namespace std;


// eg: 类,名称空间,模板函数等是不能定义两次的,如果有一个Class2.h头文件中又包含了Class.h,则class.h中的类就被定义了两次,这是不被允许的
// 所以此时加上 #ifndef,#define,#endf来防止多次定义,如果出现了上次情况,#ifndef会检测出该标识符已被定义,则会跳过重复定义过程,直接到#endif
// #ifndef name 中的name一般为该头文件的全部大写,切 ','换成" _" ,前后加上" _ "
#ifndef _CLASS_H_ // #ifndef和#endif是一套防止程序中含有多个相同的文件的指令,如果有相同的文件名,则会直接跳到endif那一行,进而结束该部分内容
#define _CLASS_H_

class Stock
{
private:  //隐藏文件,其中的内容只允许在public中的函数中调用
    std::string company;
    long shares;
    double share_val;
    double share_total;
    void set_tol(long sh, double sh_v) //在类中直接定义的函数(一般来说都比较短),为内联(inline)函数,每次调用该函数时都会在调用的文件中生成一份该函数
        {
            share_total = sh * sh_v;
        }
public:
    void acquire(const string& name, long sh, double sh_v); //初始化函数
    void buy(long num, double price);
    void sell(long num, double price);
    void updata(double price);
    void show();
};


#endif



fuc.cpp

#include"Class.h"
#include<iostream>

//类函数的定义 (函数原型: returnname classname::fucname() )    --   通过classname::fucname来确定是给哪个类中的函数进行定义
void Stock::acquire(const string &name, long sh, double sh_v) 
{
    company = name;
    if (sh < 0)
    {
        cout << "The shares can not be negative" << endl;
        cout << company << " shares set to 0" << endl;
        shares = 0;
    }
    else
    {
        shares = sh;
        share_val = sh_v;
        set_tol(shares, share_val);
    }
}

void Stock::buy(long num, double price)
{
    if (num < 0)
    {
        cout << "The method you are perform is negative" << endl;
    }
    else
    {
        shares += num;
        share_val = price;
        set_tol(shares, share_val);
    }
}

void Stock::sell(long num, double price)
{
    if (num < 0)
    {
        cout << "The method you are performing is negative" << endl;
    }
    else
    {
        shares -= num;
        share_val = price;
        set_tol(shares, share_val);
    }
}

void Stock::updata(double price)
{
    share_val = price;
    set_tol(shares, share_val);
}

void Stock::show()
{
    cout << "Company :" << company << endl;
    cout << "Shares: " << shares << endl;
    cout << "Shares_value: "<< share_val << endl;
    cout << "Total_value: " << share_total << endl;
}

main.cpp

//类的使用: 在头文件中进行类的创建,其中包含private和public两个部分:有数据和函数声明或者函数定义
//通过 classname name 来创建对象
//再通过 returntype classname::fucname()来给函数定义, name.fucname()来调用函数
//类中private的数据只能通过public中的函数进行访问

#include<iostream>
#include"Class.h"


int main()
{
    Stock GodFishhh;
    GodFishhh.acquire("AFish", 100, 200);
    GodFishhh.show();
    cout << "---------------" << endl;
    GodFishhh.buy(10, 300);
    GodFishhh.show();
    cout << "---------------" << endl;
    GodFishhh.sell(20, 500);
    GodFishhh.show();
    cout << "---------------" << endl;
    GodFishhh.updata(500);
    GodFishhh.show();

    system("pause");
    return 0;
}

//1. 类中包括private和public部分,其中private中的为隐藏内容,想要访问这部分内容只能通过public中的函数

//2. 头文件中进行类的定义,源文件中进行类中的函数的定义(也叫做实现文件)

//3. 在实现文件中进行函数的定义时,应当用‘ :: '来指明是对哪个类中的函数定义,                            语法为: returntype classname::fucname()

//4.创建类的对象: Classname name

//5.调用类函数时,与调用结构体中的元素类似: name.fucname()

//6.头文件中一般要用到 #ifndef(if not define) #define #endif (end if define)

#ifdef与#endif的作用及用法 - andylanzhiyong的博客 - CSDN博客
#ifdef与#endif的作用及用法_andylan_zy的博客-CSDN博客_ifdef               

对于此段代码的个人理解: 头文件中的类,结构体,模板函数等是不能重复定义的,如果在class.h中定义了一个类,而在class1.h中包含了class.h,则会出现重复定义的问题,但是如果使用了#ifndef _CLASS_H_ 则会检测该类是否已经定义,若发现已经定义,则会跳转到#endif,即结束了其中的重复定义过程。(#define的名称一般为头文件全大写,' . '换成 ' _ ',前后加上' _ ', eg: class.h  -> _CLASS_H_ )

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值