【自用23.】C++-const数据成员及const成员函数

const数据成员

const数据成员的初始化方式:

  1. 使用类内值(C++11支持)
  2. 使用构造函数的初始化列表

(如果同时使用这两种方式,以初始化列表中的值为最终初始化结果)

注意: 不能在构造函数或其他成员函数内,对const成员赋值!

代码:

Human.h

#pragma once

#include <string>

using namespace std;

extern int HumanCount;

class Human {
public:
	Human();
	Human(int age, int salary, string bldType);

	int getAge();
	Human* compare1(Human* other);
	static int gethumanCount();
	void description();

private:
	string name = "Unknown";
	int age = 28;
	int salary;
	char* addr;
	const string bloodType;
	//const string bloodType = "A";//仅C++11支持类内初始化
	
	static int humanCount;
};

Human.cpp

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

using namespace std;

int HumanCount = 0;

int Human::humanCount = 0;

//初始化列表
Human::Human():bloodType("未知") {
	name = "无名氏";
	age = 18;
	salary = 30000;
	HumanCount++;
	humanCount++;
}

Human::Human(int age, int salary, string bldType):bloodType(bldType) {
	cout << "调用自定义的构造函数" << endl;
	this->age = age;      //this是一个特殊的指针,指向这个对象本身
	this->salary = salary;
	name = "无名";

	addr = new char[64];
	strcpy_s(addr, 64, "China");
	HumanCount++;
	humanCount++;
}

int Human::getAge() {
	return age;
}

int Human::gethumanCount() {
	return humanCount;
}

void Human::description() {
	cout << "age:" << age << " salary:" << salary << " bloodType:" << bloodType << endl;
}

Human* Human::compare1(Human* other) {
	if (age > other->age) {
		return  this; //没有创建新的对象
	}
	else {
		return other;
	}
}

main.cpp

#include <iostream>
#include <Windows.h>

#include <string.h>
#include "Human.h"

using namespace std;

void test0() {
	Human temp1;
	Human temp2;
}

int main(void) {
	Human h1;
	Human h2(18, 8000, "B");

	h1.description();
	h2.description();

	system("pause");
	return 0;
}

使用结果如下

 使用步骤如下:

在类定义中定义一个const常量,其中C++11版本可以满足类内初始化的需求,为了兼容更多版本,我们可以使用其他初始化方法。

在手动定义的默认构造函数中,在初始化列表中对这个常量进行初始化,如下

Human::Human():bloodType("未知") {

同时也要在自定义的构造函数中,在初始化列表中对常量进行初始化

Human::Human(int age, int salary, string bldType):bloodType(bldType) {

 const成员函数

需求分析:

const的Human对象,不能调用普通的成员函数。

分析:

C++认为,const(常量)对象,如果允许去调用普通的成员函数,而这个成员函数内部可能会修改这个对象的数据成员!而这讲导致const对象不再是const对象!

解决方案:

如果一个成员函数内部,不会修改任何数据成员,就把它定义为const成员函数。

代码如下

Human.h

#pragma once

#include <string>

using namespace std;

extern int HumanCount;

class Human {
public:
	Human();
	Human(int age, int salary, string bldType);

	int getAge();
	Human* compare1(Human* other);
	static int gethumanCount();
	void description() const;

private:
	string name = "Unknown";
	int age = 28;
	int salary;
	char* addr;
	const string bloodType;
	//const string bloodType = "A";//仅C++11支持类内初始化
	
	static int humanCount;
};

 Human.cpp

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

using namespace std;

int HumanCount = 0;

int Human::humanCount = 0;

//初始化列表
Human::Human():bloodType("未知") {
	name = "无名氏";
	age = 18;
	salary = 30000;
	HumanCount++;
	humanCount++;
}

Human::Human(int age, int salary, string bldType):bloodType(bldType) {
	cout << "调用自定义的构造函数" << endl;
	this->age = age;      //this是一个特殊的指针,指向这个对象本身
	this->salary = salary;
	name = "无名";

	addr = new char[64];
	strcpy_s(addr, 64, "China");
	HumanCount++;
	humanCount++;
}

int Human::getAge() {
	return age;
}

int Human::gethumanCount() {
	return humanCount;
}

void Human::description() const {
	cout << "age:" << age << " salary:" << salary << " bloodType:" << bloodType << endl;
}

Human* Human::compare1(Human* other) {
	if (age > other->age) {
		return  this; //没有创建新的对象
	}
	else {
		return other;
	}
}

main.cpp

#include <iostream>
#include <Windows.h>

#include <string.h>
#include "Human.h"

using namespace std;

void test0() {
	Human temp1;
	Human temp2;
}

int main(void) {
	const Human h1;
	Human h2(18, 8000, "B");

	h1.description();
	h2.description();

	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值