C++:对象成员方法的使用

首先复习一下const :

//const:

//Complex* const pthis1 = &ca; //约束指针自身 不能指向其他对象

// pthis1 = &cb; err

//pthis1->real;

//const Complex* const pthis1 = &ca;//指针指向 指针自身 都不能改

//pthis1->real; 只可读 不可写

//const Complex cc{5,6}; 当前对象为常对象(属性值不能改变)

//Complex* pthis3 = &cc; err pthis3指向cc 可以改变cc的值

// Complex* const pthis4 = &cc; err const约束指针的自身 我们需要约束指针的指向

//const Complex* const pthis4 = &cc; OK

C++类的编译:

1.识别属性

2.函数名,返回类型 函数形参 形参个数

3.改写 :标识符和类型里面设计的属性标识符相同时加this指针

1.普遍方法与const成员函数

示例:

cpp 复制代码
class Complex
{
private:
    int real;
    int image;
public:
    Complex(int r = 0, int i = 0) :real(r), image(i)
    {

        cout << "Create Complex: " << this << endl;
        Print();
    }
    ~Complex()
    {
        cout << "Destroy Complex: " << this << endl;
    }


    //函数内部的方法
    //void Print(const* Complex * const this)
    void Print() const //常方法   
    {
        cout << "Real= " << this->real << "Image= " << this->image << endl;
        int x = this->GetReal();

    }
    //void Print(Complex * const this)
    void Print()
    {

    }

    void SetReal(int r)
    {
        real = r;
    }
    void SetImage(int i)
    {
        this->Print();
        image = i;
    }
    //int GetReal(Complex* const this) 自身为常性
    int GetReal()const
    {
        return real;
    }
    int GetImage()const
    {
        return image;
    }

    //函数内部的方法
};

//普通对象既可以调动普通方法 又可以调动常方法
//常对象只可以调动常方法

int main()
{
    Complex ca{ 1,2 };         //普通对象 优先调动同名的普通方法,其次调动常方法
    const Complex cb{ 2,3 };
    ca.Print();
    
    cb.Print();
    return 0;
}

运行结果:

2.引用的使用:

cpp 复制代码
#include<iostream>
using namespace std;
class Complex
{
	int Real;//实部
	int Image;//虚部
public:
	Complex() //缺省函数的构造
	{
		Real = 0.0;
		Image = 0.0;
		cout << "creat object:Complex()" << endl;
	}
	Complex(int r, int i) //带参数的构造函数
	{
		Real = r;
		Image = i;
		cout << "creat object:Complex(int ,int)" << endl;
	}
	void SetReal(int r) 
	{
		Real = r; 
	}
	void SetImage(int i)
	{
		Image = i;
	}
	int GetReal()
	{
		return Real;
	}
	int GetImage()
	{
		return Image;
	}
	//int GetReal() const {return Real;}
	//int GetImage()comst {return Image;}
	int &Realval()
	{
		return Real;
	}
	int &Imageval()
	{
		return Image;
	}
	int Realval() const 
	{
		return Real;
	}
	int Imageval() const
	{
		return Image;
	}
	void print() const //常方法
	{
		cout << "Real= " << Real << 't' << "Image= " << Image << endl;
	}
};
int main()
{
	Complex ca;
	Complex cb(1, 2);
	Complex cc = Complex(3, 4);
	Complex cd{ 5,6 };
	Complex ce();//是否可以构造对象
	return 0;
}

运行结果:

常对象只能调用常方法,而不能调用不同方法;普通对象既可以调用普通方法(优先选择),也可以调用常方法。

相关推荐
R-G-B几秒前
【10】MFC入门到精通——MFC 创建向导对话框、属性页类、属性表类、代码
c++·mfc·创建向导对话框·创建属性页类·创建属性表类
AA陈超1 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 P06-16 属性菜单 - 构建
c++·游戏·ue5·游戏引擎·虚幻
天***88962 小时前
Edge浏览器无法安装扩展CRX_REQUIRED_PROOF_MISSING扩展的按钮开关为灰色,无法启用
c++
云知谷2 小时前
【经典书籍】C++ Primer 第19章特殊工具与技术精华讲解
c语言·开发语言·c++·软件工程·团队开发
不是老弟2 小时前
rwqsd
数据结构·c++·算法
小龙报2 小时前
《C语言疑难点 --- 字符函数和字符串函数专题(上)》
c语言·开发语言·c++·算法·学习方法·业界资讯·visual studio
咔咔咔的3 小时前
3461. 判断操作后字符串中的数字是否相等 I
c++
liulilittle3 小时前
LwIP协议栈MPA多进程架构
服务器·开发语言·网络·c++·架构·lwip·通信
艾莉丝努力练剑4 小时前
【C++:继承】面向对象编程精要:C++继承机制深度解析与最佳实践
开发语言·c++·人工智能·继承·c++进阶
penguin_bark4 小时前
C++ 异步编程(future、promise、packaged_task、async)
java·开发语言·c++