C++-04-类与对象2

类的嵌套

#include <iostream>
using namespace std;
//类的嵌套
class CC1
{
public:
	int x;
	void Func();
	class CC2
	{
	public:
		int x;
		void Func();
	}objc;
};
void CC1::Func()
{
	x = 3000;
	cout << "x=" << x << endl;

}
void CC1::CC2::Func()
{
	x = 40000;
	cout << "x=" << x << endl;
}
int main()
{
	CC1 obj;
	obj.Func();
	obj.objc.Func();

	cout << "\n\n";
	cout << "2:x=" << obj.x << endl;
	cout << "2:x=" << obj.objc.x << endl;

	return 0;
}
#include <iostream>
using namespace std;
//类的嵌套设计2
class CA
{
	class CB
	{
		int i,j;//类CB包含在类CA当中,类的嵌套
	public:
		void setijfunc(int m, int n)
		{
			i = m;
			j = n;
		}
	};
	float x, y;
public:
	CB cb1, cb2;//嵌套类的对象
	void setxyfunc(float a, float b)
	{
		x = a;
		y = b;
	}
	void printfunc()
	{
		cout << "x=" << x << endl;
		cout << "y=" << y << endl;
	}
};

int main()
{
	
	return 0;
}

对象引用私有数据成员

//#include <iostream>
//using namespace std;
1.通过公有函数为私有成员赋值
//
//class CTest 
//{
//	int x, y;
//public:
//	void setxy(int a, int b)
//	{
//		x = a;
//		y = b;
//	}
//	void dispxy()
//	{
//		cout << "\nx=" << x << ",y=" << y << endl << endl;
//	}
//};
//int main()
//{
//	CTest obj1;
//	obj1.setxy(78, 90);//调用公有成员函数为私有对象赋值
//	obj1.dispxy();//输出结果x=78,y=90
//	return 0;
//}

//#include <iostream>
//using namespace std;
2.利用指针访问私有数据成员
//
//class CTest
//{
//	int x, y;
//public:
//	void setxy(int a, int b)
//	{
//		x = a;
//		y = b;
//	}
//	void printfxy()
//	{
//		cout << "\nx=" << x << ",y=" << y << endl << endl;
//	}
//	void getxy(int* px, int * py)//提取x,y的值
//	{
//		*px = x;
//		*py = y;
//	}
//};
//int main()
//{
//	CTest obj;
//	obj.setxy(100, 200);
//	obj.printfxy();
//
//	int m, n;
//	obj.getxy(&m, &n);//将m=x,n=y
//	cout << "\n\nm=" << m << ",n=" << n << endl << endl;
//	return 0;
//}



//#include <iostream>
//using namespace std;
3.利用函数访问私有数据成员
//
//class CTest
//{
//	int x, y;
//public:
//	void setxy(int a, int b)
//	{
//		x = a;
//		y = b;
//	}
//	void printfxy()
//	{
//		cout << "\nx=" << x << ",y=" << y << endl << endl;
//	}
//	int getx()
//	{
//		return x;
//	}
//	int gety() 
//	{
//		return y;
//	}
//};
//int main()
//{
//	CTest obj;
//	obj.setxy(100, 200);
//	obj.printfxy();
//
//	cout << "\n\nx=" << obj.getx() << ",y=" << obj.gety() << endl << endl;
//	return 0;
//}



#include <iostream>
using namespace std;
//4.利用引用访问私有数据成员

class CTest
{
	int x, y;
public:
	void setxy(int a, int b)
	{
		x = a;
		y = b;
	}
	void printfxy()
	{
		cout << "\nx=" << x << ",y=" << y << endl << endl;
	}
	void getxy(int& px, int& py)
	{
		px = x;
		py = y;
	}
};
int main()
{
	CTest obj;
	obj.setxy(100, 200);
	obj.printfxy();

	int m, n;
	obj.getxy(m, n);
	cout << "m=" << m << ",n=" << n << endl << endl;

	return 0;
}

成员函数重载

//#include <iostream>
//using namespace std;
//
//class CTest
//{
//	int x, y;
//	int m, n;
//public:
//	void setxy(int a, int b)
//	{
//		x = a;
//		y = b;
//	}
//	void setxy(int a, int b, int c, int d)
//	{
//		x = a;
//		y = b;
//		m = c;
//		n = d;
//	}
//	void dispxy(int x)
//	{
//		cout << x << "," << y <<  endl << endl;
//	}
//	void dispxymn()
//	{
//		cout << x << "," << y << "," << m << "," << n << endl << endl;
//	}
//};
//int main()
//{
//	CTest obj1, obj2;
//	//参数不同
//	obj1.setxy(10, 20);
//	obj2.setxy(10, 20, 30, 40);
//	//参数类型不同
//	obj1.dispxy(666);
//	obj2.dispxymn();
//	return 0;
//}
#include <iostream>
using namespace std;

class CTest
{
public:
	double sum()
	{
		return x + y;
	}
	void setxy(double a, double b)
	{
		x = a;
		y = b;
	}
	void dispxy()
	{
		cout << "x=" << x << "," << "y=" << y << endl << endl;
	}
private:
	int x, y;

};
int main()
{
	CTest obj1, obj2;//定义对象
	CTest* pobj;//对象类的指针(对象指针)
	pobj = &obj1;
	pobj->setxy(3.4, 8.9);//通过指针引用对象的成员函数
	pobj->dispxy();

	cout << "x+y" << pobj->sum() << endl;
	return 0;
}

this指针

#include <iostream>
using namespace std;
class CTest
{
private:
	int x;
public:
	int getx() const {
		return x;
	}
	void setx(int x) {
		this->x = x;
		cout << "this指针存储的内存地址为:" << this << endl << endl;
	}
};
int main()
{
	CTest obj;
	obj.setx(888);
	cout << "对象obj在内存的地址为:" << &obj << endl;
	cout << "对象obj所保存的值为:" << obj.getx() << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值