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;
}

运行结果:

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

相关推荐
Larry_Yanan3 小时前
QML学习笔记(四十二)QML的MessageDialog
c++·笔记·qt·学习·ui
R-G-B3 小时前
【35】MFC入门到精通——MFC运行 不显示对话框 MFC界面不显示
c++·mfc·mfc运行 不显界面·mfc界面不显示
Madison-No74 小时前
【C++】探秘vector的底层实现
java·c++·算法
晚风残4 小时前
【C++ Primer】第十二章:动态内存管理
开发语言·c++·c++ primer
liu****4 小时前
8.list的模拟实现
linux·数据结构·c++·算法·list
保持低旋律节奏4 小时前
C++ stack、queue栈和队列的使用——附加算法题
c++
初圣魔门首席弟子4 小时前
【C++ 学习】单词统计器:从 “代码乱炖” 到 “清晰可品” 的复习笔记
开发语言·c++
十五年专注C++开发5 小时前
CFF Explorer: 一款Windows PE 文件分析的好工具
c++·windows·microsoft
郝学胜-神的一滴5 小时前
计算机图形学中的光照模型:从基础到现代技术
开发语言·c++·程序人生·图形渲染
深耕AI7 小时前
MFC + OpenCV 图像预览显示不全中断问题解决:GDI行填充详解
c++·opencv·mfc