首页 > 编程语言 >【自用21.】C++-this指针

【自用21.】C++-this指针

时间:2024-09-10 21:24:09浏览次数:3  
标签:salary 21 int age C++ 自用 Human include addr

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

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

说明:在类的静态成员函数【后续学习】中,不能使用this指针!

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

using namespace std;

// 定义一个“人类”
class Human {
public:
	Human();
	Human(int age, int salary);

	int getAge();
	Human& compare1(Human& other);

private:
	string name = "Unknown";
	int age = 28;
	int salary;
	char* addr;
};

Human::Human() {
	name = "无名氏";
	age = 18;
	salary = 30000;
}

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

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

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

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

int main(void) {
	Human h1(25, 30000);
	Human h2(18, 8000);
	cout << h1.compare1(h2).getAge() << endl;

	system("pause");
	return 0;
}
#include <iostream>
#include <Windows.h>
#include <string>
#include <string.h>

using namespace std;

// 定义一个“人类”
class Human {
public:
	Human();
	Human(int age, int salary);

	int getAge();
	Human* compare1(Human* other);

private:
	string name = "Unknown";
	int age = 28;
	int salary;
	char* addr;
};

Human::Human() {
	name = "无名氏";
	age = 18;
	salary = 30000;
}

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

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

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

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

int main(void) {
	Human h1(25, 30000);
	Human h2(18, 8000);

	Human* p;
	p = &h1;

	cout << p->compare1(&h2)->getAge() << endl;

	system("pause");
	return 0;
}

上面两种代码,一种是用指针的,一种是用引用的,它们使用的符号稍微有一点不一样,大家要注意!!也要注意函数中传入参数的种类!

标签:salary,21,int,age,C++,自用,Human,include,addr
From: https://blog.csdn.net/m0_57667919/article/details/142064224

相关文章

  • C++入门基础知识61——【关于C++继承】
    成长路上不孤单......
  • C++入门基础知识60——【关于C++ 类 & 对象】
    成长路上不孤单......
  • C++入门基础知识59——【关于C++数据结构】
    成长路上不孤单......
  • C++ 中的左值引用和右值引用
    1、前言概念左值引用一直有这个概念。C++11中才出现了右值引用的概念。注意本文只讨论左值引用和右值引用,但需要提前了解一下左值和右值。//以下的a、p、*p、b都是左值inta=3;int*p=&a;*p;constintb=2;doublex=1.3,y=3.8;......
  • [COCI2020-2021#4] Vepar
    [COCI2020-2021#4]Vepar题意给定两组正整数\(a,a+1,\ldots,b\)和\(c,c+1,\ldots,d\)。判断\(c\times(c+1)\times\ldots\timesd\)能否被\(a\times(a+1)\times\ldots\timesb\)整除。思路将\(c\times(c+1)\times\ldots\timesd\)转化为\(\frac{d!}{(c-1)!}......
  • [COCI2020-2021#3] Vlak
    [COCI2020-2021#3]Vlak题意Nina和Emilija在玩游戏。Nina先手,两人轮流在纸上写下一个字母。每个玩家写下字母后得到的单词必须是该玩家喜欢的歌曲中某个单词的前缀。不能操作的玩家输,判断最后谁会赢。思路对每个玩家喜欢的歌曲建立字典树。搜索每个玩家的操作,每次在两......
  • [COCI2020-2021#5] Po
    [COCI2020-2021#5]Po题意给出一个序列\(a\),有一个序列\(b\),初始全为\(0\)。可以对序列\(b\)进行如下操作:使一个连续的区间内的所有数加上一个正整数\(x\)。但要求任意两个操作区间要么互不相交,要么一个包含另外一个。求将序列\(b\)变为序列\(a\)的最小操作次数。......
  • [COCI2021-2022#6] Zemljište
    [COCI2021-2022#6]Zemljište题意给出一个矩阵,一个子矩阵的权值为\(|m-a|+|m-b|\),\(m\)为子矩阵数值和,\(a,b\)为给出的数。求该矩阵权值最小的子矩阵。思路枚举子矩阵上界和下界,左右界使用双指针枚举,令\(a<b\)。对于每个左界,不断扩展右界直到子矩阵和大于\(b\),因为再......
  • 计算机知识科普问答--5 (21-25)
    21、程序一定是算法吗?不是程序和算法的区别算法(Algorithm):解决问题的一组明确、有序的步骤或规则。特性:有穷性、确定性、可行性。程序(Program):用编程语言编写的一组指令,包含算法的实现和其他功能。特性:执行性、完整性。程序不一定是算法,但程序可以包含一个......
  • 「NOI2021 D1T3 庆典」题解
    uoj675加强:\(\sumk\le6\times10^5\)暴力\(u\)在\(s\Rightarrowt\)路径上\(\iff\)正图上\(s\Rightarrowu\)且反图上\(u\Rightarrowt\)时间复杂度\(O((n+m)q)\)正解只关心可达性,不妨SCC缩点成DAG。注意到一个奇怪的条件:对于三座城市\(x,y,z\),若\(x\Right......