C++之构造函数/析构函数——斗地主

知识点比较多,包括构造函数,析构函数相关的知识,考察的比较全面。除了类本身的定义,包括类的创建,构造函数的声明(4个不同的构造函数),函数的定义(showinfo),析构函数(一个就够了),可以看到构造函数的重载,不同参数个数之间,构造函数的命名方式,域空间解析运算符的使用,析构函数的运行方式(堆和栈),内存被回收释放的顺序,.h用来声明类。.cpp来实现类
私有属性只能在本类中使用,其他的类不能使用
私有属性可以通过封装方法进行封装,称为数据的隐藏
成员函数,最好放在两个文件里面.h声明函数,.cpp定义函数等等知识点
实例化对象的时候既可以通过堆内存中指针实例化,也可以通过栈内存直接实例化。注意在使用堆内存实例化是,一定要记得手动delete释放内存
//main.cpp

#include <iostream>
#include "Students.h"
using namespace std;
#include "LandOwner.h"
int main()
{
    //使用指针的方式进行构造函数的调用,同时看到构造函数之间的重载
    LandOwner* ptr_LandOwner1 = new LandOwner();
    LandOwner* ptr_LandOwner2 = new LandOwner("专业斗地主");
    LandOwner* ptr_LandOwner3 = new LandOwner("周扒皮","男", 123, 456);

    //指针方式的调用只能通过delete释放
    ptr_LandOwner1->showinfo();
    ptr_LandOwner2->showinfo();
    ptr_LandOwner3->showinfo();
    delete ptr_LandOwner1;
    delete ptr_LandOwner2;
    delete ptr_LandOwner3;
    
	LandOwner();				//可以看到调用构造函数时被释放只是昙花一现,立马被释放
    LandOwner().showinfo();		//这个可以看到调用构造函数时打印输出后也是立马被释放

    return 0;
}

//LandOwner.h

#ifndef LANDOWNER_H
#define LANDOWNER_H
#include <iostream>
using namespace std;

class LandOwner
{
    public:
        LandOwner();
        LandOwner(string);
        LandOwner(string, string, int, long);

        ~LandOwner();

        string Getlan_name() { return lan_name; }
        void Setlan_name(string val) { lan_name = val; }
        string Getlan_gender() { return lan_gender; }
        void Setlan_gender(string val) { lan_gender = val; }
        int Getlan_exp() { return lan_exp; }
        void Setlan_exp(int val) { lan_exp = val; }
        long Getlan_gold() { return lan_gold; }
        void Setlan_gold(long val) { lan_gold = val; }
        void showinfo();
    protected:

    private:
        string lan_name;
        string lan_gender;
        int lan_exp;
        long lan_gold;
};

#endif // LANDOWNER_H

//LandOwner.cpp

/*
*创建时间:
*描    述:
*作    者:
*/
#include <iostream>
using namespace std;

#include "LandOwner.h"

LandOwner::LandOwner()//无参数传进来,四个参数都要设置默认参数
{
    Setlan_name("默认系统");
    Setlan_gender("不知道是啥");
    Setlan_exp(888);
    Setlan_gold(1000);
    cout << "这是系统默认构造" << endl;
}
LandOwner::LandOwner(string name):lan_name(name)//只传一个参数,其他参数要设置成默认值的形式
{
    Setlan_gender("老千");
    Setlan_exp(666);
    Setlan_gold(999);
    cout << "这是LandOwner(string)构造" << endl;
}

LandOwner::LandOwner(string name, string gender, int exp, long gold):lan_name(name),lan_gender(gender),lan_exp(exp),lan_gold(gold)
{//这里指定传进四个参数不用设置默认值
    cout << "这是LandOwner(string, string, int, long)构造" << endl;
}
LandOwner::~LandOwner()
{
    cout << Getlan_name() << "被释放啦!" << endl;
}
void LandOwner::showinfo()//打印信息
{
    cout << "昵称: " << Getlan_name() << endl;
    cout << "性别: " << Getlan_gender() << endl;
    cout << "经验: " << Getlan_exp() << endl;
    cout << "金钱: " << Getlan_gold() << endl;
}

/程序输出

这是系统默认构造
这是LandOwner(string)构造
这是LandOwner(string, string, int, long)构造
昵称: 默认系统
性别: 不知道是啥
经验: 888
金钱: 1000
昵称: 专业斗地主
性别: 老千
经验: 666
金钱: 999
昵称: 周扒皮
性别: 男
经验: 123
金钱: 456
默认系统被释放啦!
专业斗地主被释放啦!
周扒皮被释放啦!
这是系统默认构造
默认系统被释放啦!
这是系统默认构造
昵称: 默认系统
性别: 不知道是啥
经验: 888
金钱: 1000
默认系统被释放啦!

Process returned 0 (0x0)   execution time : 0.110 s
Press any key to continue.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值