The First Column | The Second Column |
---|---|
attribute | [e'tribute]n.属性 |
choose start menu folder | 选择“开始”菜单文件夹 |
expired | 期满的 |
expire | 期满 |
scratch | 擦,刮,adj,打草稿用的,随便写的 |
#include <iostream>
using namespace std;
class Student {
public:
void setname(char *name);
void setage(int age);
void setscore(float score);
void show();
private:
char *name;
int age;
float score;
};
void Student::setname(char *name) {
this->name = name;
}
void Student::setage(int age) {
this->age = age;
}
void Student::setscore(float score) {
this->score = score;
}
void Student::show() {
cout << this->name << "的年龄是" << this->age << ",成绩是" << this->score << endl;
}
int main() {
Student *pstu = new Student;
pstu->setname("李华");
pstu->setage(16);
pstu->setscore(96.5);
pstu->show();
return 0;
}
The Result:李华的年龄是16,成绩是96.5
This
can only be used inside the class,and all members of the calss can be accessed throughthis
,includingprivate
,protected
andpublic
attributes.- In this example,the name of the member functions' parameters are the same as those of member functions' variables,It can only be distinguished by
this
.- Take the member function
setname(char* name)
for example,its parameter isname
,and the member variablename
is named,if you writename=name
;such a statement is to assign a parameter to the parameter name,instead of assigning the member variable name.And writingthis -> name=name;
,the name on the left is the member variable,and the name on the right is the parameter,which is clear at a glance.This
is a pointer to access member variables or member functions with->
This
is used whthin the class,but only when the object is created will it be assigned tothis
,and the process of the assignment is done automatically by the compiler,without user intervention,and the user can not explicitly assign a value to thethis
.In this this case,the value of this is the same as that of pstu.
Adding a member function(printThis()) in class Student to specially print the value of this
:
void Student::printThis(){
cout<<this<<endl;
}
then create an object in the main() function and call printThis():
#include <iostream>
using namespace std;
class Student {
public:
void setname(char *name);
void setage(int age);
void setscore(float score);
void show();
void printThis();
private:
char *name;
int age;
float score;
};
void Student::setname(char *name) {
this->name = name;
}
void Student::setage(int age) {
this->age = age;
}
void Student::setscore(float score) {
this->score = score;
}
void Student::show() {
cout << this->name << "的年龄是" << this->age << ",成绩是" << this->score << endl;
}
void Student::printThis() {
cout << this << endl;
}
int main() {
Student *pstu = new Student;
pstu->setname("李华");
pstu->setage(16);
pstu->setscore(96.5);
pstu->show();
Student *pstu1 = new Student;
pstu1->printThis();
cout << pstu1 << endl;
Student *pstu2 = new Student;
pstu2->printThis();
cout << pstu2 << endl;
return 0;
}
- The result:
李华年龄是16,成绩是96.5
0000026391651120
0000026391651120
0000026391651580
0000026391651580
It can be found that this does point to the current object,and for different objects,the value of this is different.
A few notes:
This
isconst
pointer,its value can not be modified,all attempts to modify the pointer's operation,such as assignment,increment,decrement and so on,are not allowed.- 'This' can only be used inside the memeber function,and it is illegal to use it elsewhere.
- 'This' is meaningful only when the object is created,so it can not be used in the
static
member function(followed by static members).