this->age = age是啥意思?

时间: 2024-03-29 16:37:28 浏览: 15
this->age = age这行代码表示将当前对象的成员变量age的值设置为参数age的值。其中,this->age表示当前对象的成员变量age,而age则是传入的参数。这种语法常用于成员函数的实现中,用于区分成员变量和函数参数同名的情况。通过使用this指针来明确指出是访问当前对象的成员变量age,而不是参数age。
相关问题

c++ Stu& operator=(const Stu& other) { this->name = other.name; this->age = other.age; cout << "=号运算符重载函数" << endl; return *this; }啥意思

这是一个 C++ 中的类 Stu 的赋值运算符重载函数。其作用是将一个 Stu 类型的对象的值赋给另一个 Stu 类型的对象。 函数的参数 `const Stu& other` 是一个常量引用,表示传入的另一个对象的引用,避免了进行拷贝构造函数的开销。 函数体中的代码 `this->name = other.name; this->age = other.age;` 表示将参数 `other` 对象的 `name` 和 `age` 成员变量的值分别赋值给当前对象的 `name` 和 `age` 成员变量。 最后,函数返回一个指向当前对象的引用 `return *this`,用于支持链式赋值操作。 在函数体中的 `cout << "=号运算符重载函数" << endl;` 用于输出一条信息,表示这个函数被调用了。

#include<iostream> #include<string> using namespace std; class employee { private: string name; int age; double salary; public: employee(string name="",int age=0,double salary=0) { this->name = name; this->age = age; this->salary = salary; } void setname(string name){ this->name = name; } void setage(int age) { this->age = age; } void setsalary(double salary) { this->salary = salary; } void display() { cout << "姓名:" << name << endl; cout << "年龄:" << age << endl; cout << "工资:" << salary << endl; } string getname() { return name; } int getage() { return age; } double getsalary() { return salary; } }; int main() { employee emp("王鹏",18,18000); emp.display(); emp.getname("王赟煜"); emp.setsalary(200000); emp.display(); return 0; }怎么修改程序

你需要将 `emp.getname("王赟煜");` 修改为 `emp.setname("王赟煜");`,因为 `getname()` 是获取姓名的函数,而不是设置姓名的函数。修改后的程序如下: ``` #include<iostream> #include<string> using namespace std; class employee { private: string name; int age; double salary; public: employee(string name="",int age=0,double salary=0) { this->name = name; this->age = age; this->salary = salary; } void setname(string name){ this->name = name; } void setage(int age) { this->age = age; } void setsalary(double salary) { this->salary = salary; } void display() { cout << "姓名:" << name << endl; cout << "年龄:" << age << endl; cout << "工资:" << salary << endl; } string getname() { return name; } int getage() { return age; } double getsalary() { return salary; } }; int main() { employee emp("王鹏",18,18000); emp.display(); emp.setname("王赟煜"); emp.setsalary(200000); emp.display(); return 0; } ```

相关推荐

#include<iostream> #include<string> using namespace std; class Pet { protected: string name; int age; double weight; public: //Pet(string name,int age,float weight) // { // this->name=name; //this->age=age; //this->weight=weight; // } Pet() { name=""; age=0; weight=0.0; } Pet(const string&petName,int petAge,double petWeight) { name = petName; age = petAge; weight = petWeight; } string getName() { return name; } int getAge() { return age; } double getWeight() { return weight; } void setName(string name) { this->name=name; } void setAge(int age) { this->age=age; } void setWeight(double weight) { this->weight=weight; } virtual string getLifespan() { return "unknown lifespan"; } }; class Dog:public Pet { private: string breed; public: Dog(string name,int age,double weight,string breed) : Pet(name,age,weight) { this->breed=breed; } string getBreed() { return breed; } void setBreed(string breed) { this->breed=breed; } virtual string getLifespan() { if (weight>100) { return "Approximately 7 years"; } else { return "Approximately 13 years"; } } }; class Rock:public Pet { public: Rock(string name,int age,double weight) : Pet(name,age,weight) {} virtual string getLifespan() { return"Thousands of years"; } }; int main() { Pet*pet1=new Rock("Peter",1000,1000); Pet*pet2=new Dog("Mike",3,80,"Golden Retriever"); Pet*pet3=new Dog("Ham",5,80,"Labrador Retriever"); cout<getName()<<", "<getLifespan()<< endl; pet1->setAge(2); pet1->setWeight(1.0); cout<getName()<<"," <getLifespan()<<endl; cout<<"Breed: "getBreed()<<endl; pet2->setWeight(85.0); cout<getName()<<"," <getLifespan()<<endl; cout<<"Breed: "<getBreed() <<endl; pet3->setWeight(110.0); return 0; } 这段代码无法运行

最新推荐

recommend-type

grpcio-1.44.0-cp39-cp39-manylinux2010_x86_64.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

grpcio-1.42.0-cp38-cp38-macosx_10_10_x86_64.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

江西省抚州市信通网络科技有限公司主页图标修正版

各页面加入图标 新网站,新气象。
recommend-type

C评级客户流失率相对误差.jpg

C评级客户流失率相对误差
recommend-type

tensorflow-2.9.3-cp39-cp39-win-amd64.whl

python爬虫数据可视化
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

HSV转为RGB的计算公式

HSV (Hue, Saturation, Value) 和 RGB (Red, Green, Blue) 是两种表示颜色的方式。下面是将 HSV 转换为 RGB 的计算公式: 1. 将 HSV 中的 S 和 V 值除以 100,得到范围在 0~1 之间的值。 2. 计算色相 H 在 RGB 中的值。如果 H 的范围在 0~60 或者 300~360 之间,则 R = V,G = (H/60)×V,B = 0。如果 H 的范围在 60~120 之间,则 R = ((120-H)/60)×V,G = V,B = 0。如果 H 的范围在 120~180 之间,则 R = 0,G = V,B =
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。