/*
t his指针:
1、每一个类都包含一个指向本类对象的指针(this)
2、this指针指向本类对象的起始地址
3、当数据名与成员函数中的形参相同时常有this区分
eg:
class time
{
private:
int hour,minute,second;
public:
void ste_time(int hour,int minute,int second)
{
this->hour=hour;
this->minute=minute;
this->second=second;
}
}
4、this指针实际运用
//分数相加——本类对象加u
fraction fraction::add(fraction u)
{
int tmp;
fraction v;
v.a=a*u.b+b*u.a;//分子
v.b=b*u.b;//分母
tmp=divisor(v.a,v.b);//计算分子分母的公约数
v.a=v.a/tmp;//分子约去公约数
v.b=v.b/tmp;//分母约去公约数
return v;//返回结果
}
{
int tmp;
//fraction v;
a=a*u.b+b*u.a;//分子
b=b*u.b;//分母
tmp=divisor(a,b);//计算分子分母的公约数
a=v.a/tmp;//分子约去公约数
b=v.b/tmp;//分母约去公约数
return *this;//返回结果 省去v,直接返回类首地址得到结果
}
*/
#include<iostream>
#include<string.h>
#define N 9
using namespace std;
class person
{
private:
char Name[N];
char Sex;
int Age;
public:
person()
{
strcpy(Name,"XXX");
Age=0;
Sex=' ';
}
~person()
{
cout<<"delete this person!"<<endl;
}
void Register(char *name,char sex,int age);
void cout_me()
{
cout<<Name<<" "<<Sex<<" "<<Age<<endl;
}
void text_this();//this指针测试函数
};
void person::Register(char *name,char sex,int age)
{
strcpy(Name,name);
Sex=sex;
Age=age;
}
void person::text_this() //this指针测试
{
cout<<" Age="<<Age<<endl; //测试证明三种输出结果是一致的
cout<<" this->Age="<<this->Age<<endl;
cout<<"(*this)Age="<<(*this).Age<<endl;
}
int main()
{
person ToT;
person *p1,*p2;//定义两个类指针;
p1=new person;//建立存储空间;
cout<<"person1:\t";//\t跳跃8个光标
p1->Register("zhang3",'men',20);//字符串组打双引号,字符打单引号
p1->cout_me();
p1->text_this();
p2=new person;
*p2=*p1;
p2->cout_me();
delete p1;
return 0;
}
t his指针:
1、每一个类都包含一个指向本类对象的指针(this)
2、this指针指向本类对象的起始地址
3、当数据名与成员函数中的形参相同时常有this区分
eg:
class time
{
private:
int hour,minute,second;
public:
void ste_time(int hour,int minute,int second)
{
this->hour=hour;
this->minute=minute;
this->second=second;
}
}
4、this指针实际运用
//分数相加——本类对象加u
fraction fraction::add(fraction u)
{
int tmp;
fraction v;
v.a=a*u.b+b*u.a;//分子
v.b=b*u.b;//分母
tmp=divisor(v.a,v.b);//计算分子分母的公约数
v.a=v.a/tmp;//分子约去公约数
v.b=v.b/tmp;//分母约去公约数
return v;//返回结果
}
{
int tmp;
//fraction v;
a=a*u.b+b*u.a;//分子
b=b*u.b;//分母
tmp=divisor(a,b);//计算分子分母的公约数
a=v.a/tmp;//分子约去公约数
b=v.b/tmp;//分母约去公约数
return *this;//返回结果 省去v,直接返回类首地址得到结果
}
*/
#include<iostream>
#include<string.h>
#define N 9
using namespace std;
class person
{
private:
char Name[N];
char Sex;
int Age;
public:
person()
{
strcpy(Name,"XXX");
Age=0;
Sex=' ';
}
~person()
{
cout<<"delete this person!"<<endl;
}
void Register(char *name,char sex,int age);
void cout_me()
{
cout<<Name<<" "<<Sex<<" "<<Age<<endl;
}
void text_this();//this指针测试函数
};
void person::Register(char *name,char sex,int age)
{
strcpy(Name,name);
Sex=sex;
Age=age;
}
void person::text_this() //this指针测试
{
cout<<" Age="<<Age<<endl; //测试证明三种输出结果是一致的
cout<<" this->Age="<<this->Age<<endl;
cout<<"(*this)Age="<<(*this).Age<<endl;
}
int main()
{
person ToT;
person *p1,*p2;//定义两个类指针;
p1=new person;//建立存储空间;
cout<<"person1:\t";//\t跳跃8个光标
p1->Register("zhang3",'men',20);//字符串组打双引号,字符打单引号
p1->cout_me();
p1->text_this();
p2=new person;
*p2=*p1;
p2->cout_me();
delete p1;
return 0;
}