C语言日记 32 类的对象,this指针

例8-4时钟类的完整程序。

源程序:

#include <iostream>
using namespace std;//类的定义
class Clock
{
public:
	void SetTime(int NewH, int NewM, int NewS);
	void ShowTime();
private:
	int Hour, Minute, Second;
};
//时钟类成员函数的实现
void Clock::SetTime(int NewH, int NewM, int NewS)
{
	Hour = NewH;
	Minute = NewM;
	Second = NewS;
}

void Clock::ShowTime()
{
	cout << Hour << ":" << Minute << ":" << Second << endl;
}
//主函数
int main()
{
	Clock myClock;//定义对象
	myClock.SetTime(8, 30, 0);
	myClock.ShowTime();
	return 0;
}

结果:

例8-5对象数组的使用方法。

源程序:

#include <iostream>
using namespace std;
class Box
{
private:
	int height;
	int width;
	int length;
public:
	void SetBox(int h = 10, int w = 12, int len = 15)
	{
		height = h;
		width = w;
		length = len;
	}
	int volume();
};
	int Box::volume()
{
	return (height * width * length);
 }
	int main()
	{
		Box a[3]; //定义对象数组
		a[0].SetBox();
		a[1].SetBox(15, 18, 20);
		a[2].SetBox(16, 20, 26);
		cout << "volume of a[0] is " << a[0].volume() << endl;
		cout << "volume of a[1] is " << a[1].volume() << endl;
		cout << "volume of a[2] is " << a[2].volume() << endl;
		return 0;
 }

结果:

书P123:

可以通过对象(例如a[0])访问到他(对象数组)的公有成员

这里面的公有成员在这里(上述程序里)只包含了公有的那几个函数,不包含任何变量

也就是说在上述程序里我们已经用完了所有的公有成员,没办法输出任何对象数组的单个属性

除非将输出的属性改为公有:

#include <iostream>
using namespace std;
class Box
{
private:
	int width;
	int length;
public:
	int height;
	void SetBox(int h = 10, int w = 12, int len = 15)
	{
		height = h;
		width = w;
		length = len;
	}
	int volume();
};
int Box::volume()
{
	return (height * width * length);
}
int main()
{
	Box a[3]; //定义对象数组
	a[0].SetBox();
	a[1].SetBox(15, 18, 20);
	a[2].SetBox(16, 20, 26);
	cout << a[0].height << endl;
	return 0;
}

结果:

 另外值得注意的是:

输出时我们只能输出变量,不能输出形参,例如:(输入)

cout << a[1].h << endl;

结果:

修改例8-5,在主函数中定义以下语句:

	int main()
	{
		Box t;//定义t为 Box 类对象
		Box* pt;//定义 pt 为指向 Box类对象的指针变量
		pt = &t;//将t 的起始地址赋给 pt
		(*pt).SetBox();//调用 pt 所指向的对象中的 SetBox()函数,即t.SetBox
		cout << "Volume of a[0]is" << pt->volume() << endl;//调用pt 所指
	//向的对象中的Volume()函数,即t.Volume
		return 0;
 }

补充为完整程序:

#include <iostream>
using namespace std;
class Box
{
private:
	int height;
	int width;
	int length;
public:
	void SetBox(int h = 10, int w = 12, int len = 15)
	{
		height = h;
		width = w;
		length = len;
	}
	int volume();
};
int Box::volume()
{
	return (height * width * length);
}
int main()
{
	Box t;//定义t为 Box 类对象
	Box* pt;//定义 pt 为指向 Box类对象的指针变量
	pt = &t;//将t 的起始地址赋给 pt
	(*pt).SetBox();//调用 pt 所指向的对象中的 SetBox()函数,即t.SetBox
	cout << "Volume of a[0]is" << pt->volume() << endl;//调用pt 所指
//向的对象中的Volume()函数,即t.Volume
	return 0;
}

结果:

例8-6有关对象指针的使用方法。

源程序:

#include <iostream>
using namespace std;
class Time
{
public:
	int Hour;
	int Minute;
	int Second;
	void SetTime(int H, int M, int S);
	void Get_Time();
};
void Time::SetTime(int H, int M, int S)
{
	Hour = H;
	Minute = M;
	Second = S;
}
void Time::Get_Time()//定义公有成员函数
{
	cout << Hour << ":" << Minute << ":" << Second << endl;

}

int main()
{
	Time t1;//定义 Time 类对象 t1
	int* p1 = &t1.Hour; 
	//定义指向整型数据的指针变量p1,并使 p1 指向 t1.Hour
	t1.SetTime(10, 13, 56);
	cout << *p1 << endl;
	//输出p1所指的数据成员t1.Hour
	t1.Get_Time();//调用对象t1的成员函数Get_Time
	Time* p2 = &t1; //定义指向 Time 类对象的指针变量 p2,并使 p2 指向t1
	p2->Get_Time();//调用 p2 所指向对象(即t1)的Get_Time 函数
	void(Time:: * p3)();//定义指向 Time 类公用成员函数的指针变量P3
	p3 = &Time::Get_Time;//使p3 指向 Time 类公用成员函数 Get_Time
	(t1.*p3)();//调用对象t1中p3所指的成员函数(即t1.Get_Time())
	return 0;
}

结果:

对于t1:如果我们本身没有给t1赋值,即:将主函数改为        

int main()
{
	Time t1;//定义 Time 类对象 t1
	int* p1 = &t1.Hour;
	//定义指向整型数据的指针变量p1,并使 p1 指向 t1.Hour
	cout << *p1 << endl;
	return 0;
}

则输出一个任意值(系统随机分配的值):

另外,这里的

p2->Get_Time();等价于 (*p2).Get_Time();等价于 t1.Get_Time();

验证:

#include <iostream>
using namespace std;
class Time
{
public:
	int Hour;
	int Minute;
	int Second;
	void SetTime(int H, int M, int S);
	void Get_Time();
};
void Time::SetTime(int H, int M, int S)
{
	Hour = H;
	Minute = M;
	Second = S;
}
void Time::Get_Time()//定义公有成员函数
{
	cout << Hour << ":" << Minute << ":" << Second << endl;

}

int main()
{
	Time t1;//定义 Time 类对象 t1
	t1.SetTime(10, 13, 56);
	t1.Get_Time();//调用对象t1的成员函数Get_Time
	Time* p2 = &t1; //定义指向 Time 类对象的指针变量 p2,并使 p2 指向t1
	p2->Get_Time();//调用 p2 所指向对象(即t1)的Get_Time 函数
	(*p2).Get_Time();
	t1.Get_Time();
	return 0;
}

结果:

例8-7 this指针的作用

源程序:

#include <iostream>
using namespace std;
class A
{
private:
int i;
public:
	int get() const { return i;}
	void set(int x)
	{
	this->i = x;
	cout << "this 指针保存的内存地址为:" << this << endl;
	}
};
//
int main()
{
	A a;
	a.set(9);
	cout << "对象a所在的内存地址为:" << &a << endl;
	cout << "对象 a所保存的值为:" << a.get() << endl;
	cout << endl;
	A b;
	b.set(999);
	cout << "对象b所在的内存地址为:" << &b << endl;
	cout << "对象b 所保存的值为:" << b.get() << endl;
	return 0;
}

结果:

问题:

一:代码段

    this->i = x;

有意思(意义)吗?他这里不写这一段程序运行结果不也一样?

如果写了:

 ((this->i )= x;)

第一步:this指针指向i变量

注意:这里的this虽然是一个指针,但是这里用了成员选择符号“->”,它并不是指向目标对象的地址,而是访问目标对象

第二步:this指针(也就是this指针指向i变量)等于x;

如果没写:

(1):将对象的地址赋给this指针

(2):调用成员函数

二:他这里这个const的这个用法是什么意思?我好像没见过啊?

之前确实没学过const关于常量指针的用法:

const :

表示该函数不修改任何值;详见:35 类和对象-对象特性-const修饰成员函数_哔哩哔哩_bilibili

const:

  • 1.修饰变量时,表示:该变量是常量;即:该变量不可改(变)
  • 2.修饰成员函数时,(即:放在成员函数"尾巴"上)
  • 在这里,即“const”在“int get()”成员函数的背后(后面)
  • 该成员函数被称为“常(量)成员函数”;表示该成员函数不可以修改类内的成员变量(为了保证封装性)
     

回归到本例中,即表示(我们只要知道):成员函数get()不能改变成员变量i(即可)

参考与LSGO一起学“8指针(8.19 常量指针)”,试图用实践证明上述结论:

1.const修饰变量时,表示:该变量是常量;即:该变量不可改(变)

project 1:

#include <iostream>
using namespace std;
int main()
{
	int a = 3;
	int const* p = &a;
	cout << "*p=" << *p << endl;
	cout << "a=" << a << endl;
}

结果:

*p=3
a=3

project 2:

#include <iostream>
using namespace std;
int main()
{
	int a = 3;
	int const* p = &a;
	*p = 4;
	cout <<"*p=" << *p << endl;
	cout << "a=" << a << endl;
}

结果:

 这里的:  “p”: 不能给常量赋值;无疑已经说明:该(被const修饰的)变量是常量,不可改(变)。

project 3:

#include <iostream>
using namespace std;
int main()
{
	int a = 3;
	int const* p = &a;
	p++;
	cout << "*p=" << *p << endl;
	cout << "a=" << a << endl;
}

结果:

 *p=-858993460
a=3

OK,到这里关于这个const我们先到这里点到为止

具体详细情况以后再说,详见与LSGO一起学“8指针(8.19 常量指针)”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值