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

运行结果:

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

相关推荐
Once_day15 分钟前
CC++八股文之基础语法(2)
c语言·c++
dawnButterfly21 分钟前
C 语言标准、编译器与操作系统的关系
c语言·开发语言·c++
程序员龙一24 分钟前
进程、线程、协程通俗讲解与对比
c++·线程·进程·协程
Dream it possible!1 小时前
LeetCode 面试经典 150_回溯_单词搜索(104_79_C++_中等)
c++·leetcode·面试·回溯
superman超哥1 小时前
仓颉语言智能指针深度实战:突破 GC 与所有权的边界
c语言·开发语言·c++·python·仓颉
八月的雨季 最後的冰吻1 小时前
FFmepg-- 39-ffplay源码-ffplay 播放器中视频输出和尺寸变换
c++·音视频
AuroraWanderll1 小时前
类和对象(四):默认成员函数详解与运算符重载(下)
c语言·数据结构·c++·算法·stl
Cinema KI1 小时前
二叉搜索树的那些事儿
数据结构·c++
Trouvaille ~2 小时前
【C++篇】C++11新特性详解(一):基础特性与类的增强
c++·stl·c++11·类和对象·语法·默认成员函数·初始化列表
CSDN_RTKLIB2 小时前
【类定义系列一】C++ 头文件 / 源文件分离
开发语言·c++