您好,欢迎来到榕意旅游网。
搜索
您的当前位置:首页VS2017学习C++基础十五(函数构造)

VS2017学习C++基础十五(函数构造)

来源:榕意旅游网

头文件:

#pragma once
#include <string>
#include<iostream>
#include <memory.h>
using namespace std;
class LandOwnerv3
{
	long score;
	int cards[20];
	string  name;

	public:
		LandOwnerv3();
		~LandOwnerv3();
};

LandOwnerv3::LandOwnerv3()
{
	//一般使用构造函数进行成员变量的初始化
	name = "张三";
	score = 20;
	//将用户的手牌数目初始化为0
	memset(cards, 0, sizeof(cards));
	cout << "LandOwnerv3的无参构造函数被调用" << endl;
	cout << "初始化的结果如下" << endl;
	cout << "名称:" <<name <<  endl;
	cout << "积分:" << score << endl;
	cout << "手牌数组:";
	for (int i = 0; i < sizeof(cards) / sizeof(cards[0]); i++)
	{
		cout << cards[i] <<",";
	}
}
LandOwnerv3::~LandOwnerv3()
{
	//dtor
}

源文件:

#pragma once
#include <string>
#include<iostream>
#include <memory.h>
using namespace std;
class LandOwnerv3
{
	long score;
	int cards[20];
	string  name;

	public:
		LandOwnerv3();
		~LandOwnerv3();
};

LandOwnerv3::LandOwnerv3()
{
	//一般使用构造函数进行成员变量的初始化
	name = "张三";
	score = 20;
	//将用户的手牌数目初始化为0
	memset(cards, 0, sizeof(cards));
	cout << "LandOwnerv3的无参构造函数被调用" << endl;
	cout << "初始化的结果如下" << endl;
	cout << "名称:" <<name <<  endl;
	cout << "积分:" << score << endl;
	cout << "手牌数组:";
	for (int i = 0; i < sizeof(cards) / sizeof(cards[0]); i++)
	{
		cout << cards[i] <<",";
	}
}
LandOwnerv3::~LandOwnerv3()
{
	//dtor
}

运行结果:

LandOwnerv3的无参构造函数被调用
初始化的结果如下
名称:张三
积分:20
手牌数组:0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- nryq.cn 版权所有 赣ICP备2024042798号-6

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务