第004课 C++ 类与对象(二)

本文详细介绍了C++中的类嵌套、如何通过公私有成员函数操作对象的私有数据、成员函数重载(包括缺省参数和类型重载)、使用指针和引用访问私有数据以及this指针的作用。
摘要由CSDN通过智能技术生成

一、类的嵌套:

     在定义一个类时, 在其类体中又包含了一个类的完整定义,称为类的嵌套类是允许嵌套定义的 。

例1: 

#include <iostream>
using namespace std;

class CA  //类是允许嵌套定义的
{
    class CB //类CB包含在类CA当中,为嵌套定义
    {
        int i, j;
     public:
        void set_ij_func(int m,int n)
        {
            i = m;
            j = n;
        }
     };
    float x, y;
public:
    CB cb1, cb2; //嵌套类的对象
    void set_xy_func(float a, float b)
    {
        x = a;
        y = b;
    }
    void printfunc()
    {
        cout << " x=" << x << endl;
        cout << " y=" << y << endl;
    }
}; 

例2: 

#include <iostream>
using namespace std;

class CC1 
{
public:
    int x;
    void func(); //类体内函数声明

    class CC2 //类CC2定义在类CC1体内
    {
    public:
        int y;
        void func();//类体内函数声明
    }obc; //定义类的对象
}; 

void CC1::func()//在类体外 函数定义的,函数名前要+类名+::域包含符
{
    x = 3000;
    cout << " x= " << x << endl;
}
void CC1::CC2::func()//函数定义 在类体外 
{
    y = 40000;
    cout << " y= " << y << endl;
} 

int main()
{
     CC1 obj;
     obj.func(); //此处是调用CC1::func()函数
     obj.obc.func(); //此处是调用 CC1::CC2::func()函数
     return 0;
}

 输出结果:

二、类的对象引用私有数据成员

1)通过公有成员函数为私有数据成员赋值

#include <iostream>
using namespace std;

class Test 
{
private:   //私有成员
    int x, y;
public:    //公有成员
    void set_xy_func(int a, int b)
    {
        x = a;
        y = b;
    }
    void printfunc()
    {
        cout << " x=" << x << endl;
        cout << " y=" << y << endl;
    }
}

int main()
{
   Test test1;
   test1.set_xy_func(100, 200); //调用公有成员函数为私有数据成员赋值
   test1.printfunc();

   return 0;
}

输出结果: 

2)通过指针访问私有数据成员

#include <iostream>
using namespace std;

class Test 
{
    int x, y;
public:
    void set_xy_func(int a, int b)
    {
        x = a;
        y = b;
    }
    void  getxy(int* px, int* py) //提取x y 的值 ,利用指针访问私有数据成员
    {
        *px = x;
        *py = y;
    }
}

int main()
{
    Test test1;
    test1.set_xy_func(100, 200);

    int m, n;
    test1.getxy(&m, &n); //将m=x, n=y   //通过指针访问私有成员数据
    cout << " m=" << m << endl;
    cout << " n=" << n << endl;  

    return 0;
}

 输出结果:

3)通过函数访问私有数据成员

#include <iostream>
using namespace std;

class Test 
{
    int x, y;
public:
    void set_xy_func(int a, int b)
    {
        x = a;
        y = b;
    }
    int  getx()  //通过函数访问私有数据成员
    {
       return x; 
    }
}

int main()
{
    Test test1;
    test1.set_xy_func(100, 200);

    cout << " x=" << test1.getx() << endl; //利用函数访问私有数据成员
    return 0;
}

4)通过变量的引用访问私有数据成员

#include <iostream>
using namespace std;

class Test 
{
    int x, y;
public:
    void set_xy_func(int a, int b)
    {
        x = a;
        y = b;
    }
    void  getxy(int& px, int& py) //通过变量的引用访问私有数据成员
    {
        px = x;
        py = y;
    }
}

int main()
{
    Test test1;
    test1.set_xy_func(40, 60);

    int m, n;
    test1.getxy(m, n); //将m=x, n=y    //通过变量的引用访问私有数据成员
    cout << " m=" << m << endl;
    cout << " n=" << n << endl;  

    return 0;
}

输出结果:

 三、成员函数重载

      类中的成员函数与前面介绍的普通函数一样,成员函数可以带有缺省的参数,也可以重载成员函数 。重载时,函数的形参必须在类型数目上不同。

1)缺省参数的成员函数     

例:

#include <iostream>
using namespace std;

class Test
{
	int x, y;
	int m, n;
	double x1, y1;
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 setxy(double a, double b)//参数类型不同
	{
		x1 = a;
		y1 = b;
	}
	void disxymn()//参数缺省,
	{
		cout << " x=" << x << ", y=" << y << ", m=" << m << ",n=" << n << endl;
	}
	void disxy(int a)
	{
		if (a)
		{
			cout << " x=" << x << ", y=" << y << endl;
        }
		else
		{
			cout << " x1=" << x1 << ", y1=" << y1 << endl; 
		}
	}
};

int main()
{
	Test test1;
	test1.setxy(10,20);//成员函数重载
	test1.disxy(1);//输出 int 型数据

	test1.setxy(10, 20, 490, 47);//参数数目不同
	test1.disxymn();

	test1.setxy(2.47, 9.47);//参数类型不同
	test1.disxy(0); //输出 double 型数据
	
	return 0;
}

 输出结果:

2)定义类的指针及如何用指针来引用对象     

 例:

#include <iostream>
using namespace std;

  //指针引用对象
class Test
{
private:
	double x, y;
public:
	double sum()
	{
		return x + y;
	}
	void setxy(double a, double b)
	{
		x = a;
		y = b;
	}
	void disxy()
	{
		cout << " x=" << x << ", y=" << y  << endl;
	}
};

int main()
{
	Test test1;//定义对象
	Test *pt1 = &test1;//定义类的指针,指向类的对象,对象类的指针,(对象指针)
	
	pt1->setxy(89.4, 86.3); //通过指针引用对象的成员函数
	pt1->disxy();

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

 输出结果:

3)定义类的数组及数组中元素的引用

例: 

#include <iostream>
using namespace std;

class Test
{
private:
	int x[5]; // //定义类的数组
public:
	void setxy(int *a)
	{
		for (int i = 0; i < 5; i++)
			x[i] = a[i];
	}
	void disxy()
	{
		for (int i = 0; i < 5; i++) 
		  cout << " x[" <<i <<"] = " << *(x+i)  << endl;
	}
};

int main() //定义类的数组及数组中元素的引用
{
	Test test1;//定义对象   
	int arg[5] = { 4,5,6,7,8 };
    test1.setxy(arg); //类的数组中元素的引用
	test1.disxy();
	return 0;
}

  输出结果:

四、this指针

     不同对象占据内存中的不同区域,它们所保存的数据各不相同,但对成员数据进行操作的成员函数的程序代码均是一样的。     

     当对一个对象调用成员函数时,编译程序先将对象的地址赋给this指针,然后调用成员函数,每次成员函数存取数据成员时,也隐含使用this指针。

 例: 

#include <iostream>
using namespace std;

// this指针
class Test
{
private:
	int x;
public:
	int getx() const {
		return x;
	}
	void set(int x)  //当对一个对象调用成员函数时,编译程序先将对象的地址赋给this指针
	{
		this->x = x;
		cout << " this指针存储的内存地址为:" << this << endl; // 与 对象test1 的内存地址相同
	}
};

int main()
{
	Test test1;
	test1.set(888);
	cout << " 对象test1在内存的地址为:" << &test1 << endl;//会先将地址 赋给this指针,地址相同
	cout << " 对象test1所保存的值为:" << test1.getx() << endl;// 输出 888

	return 0;
}

输出结果: 

  • 16
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值