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

运行结果:

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

相关推荐
啊阿狸不会拉杆1 小时前
《算法导论》第 32 章 - 字符串匹配
开发语言·c++·算法
小学生的信奥之路1 小时前
洛谷P3817题解:贪心算法解决糖果分配问题
c++·算法·贪心算法
曙曙学编程2 小时前
stm32——GPIO
c语言·c++·stm32·单片机·嵌入式硬件
△曉風殘月〆2 小时前
Visual Studio中的常用调试功能(下)
c++·ide·visual studio·调试
武当豆豆3 小时前
C++编程学习(第25天)
开发语言·c++·学习
minji...6 小时前
C++ string类(STL简介 , string类 , 访问修改字符)
开发语言·c++
Forward♞6 小时前
Qt——文件操作
开发语言·c++·qt
十五年专注C++开发7 小时前
CMake进阶: CMake Modules---简化CMake配置的利器
linux·c++·windows·cmake·自动化构建
winds~8 小时前
【git】 撤销revert一次commit中的某几个文件
linux·c++
carver w8 小时前
MFC,C++,海康SDK,回调,轮询
开发语言·c++·mfc