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

运行结果:

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

相关推荐
落羽的落羽16 小时前
【C++】C++11的包装器:function与bind简介
c++·学习
打不了嗝 ᥬ᭄17 小时前
【Linux】线程概念与控制
linux·c++
UnnamedOrange17 小时前
ROS1 配置代码覆盖率
c++·cmake
沐怡旸17 小时前
【底层机制】std::unordered_map 扩容机制
c++·面试
沐怡旸17 小时前
【底层机制】auto 关键字的底层实现机制
c++·面试
华溢澄17 小时前
macOS下基于Qt/C++的OpenGL开发环境的搭建
c++·qt·macos·opengl
dragoooon3417 小时前
[优选算法专题二滑动窗口——串联所有单词的子串]
数据结构·c++·学习·算法·leetcode·学习方法
刃神太酷啦17 小时前
C++ 异常处理机制:从基础到实践的全面解析----《Hello C++ Wrold!》(20)--(C/C++)
java·c语言·开发语言·c++·qt·算法·leetcode
CYRUS_STUDIO18 小时前
OLLVM 移植 LLVM18 踩坑:一步步调试修复控制流平坦化
c语言·c++·llvm
将编程培养成爱好18 小时前
C++ 设计模式《外卖菜单展示》
c++·设计模式