类,指向对象的指针,

/*
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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值