C++入门基础程序

这篇博客深入讲解了C++的基础知识,包括构造函数的初始化方式、运算符重载的规则、inline函数与友元函数的概念,以及this指针的使用。同时,讨论了参数传递的不同方式(值传递、引用传递和指针传递)以及返回值类型的选取。通过实例解析了成员函数和全局函数中如何应用这些概念。
摘要由CSDN通过智能技术生成

本篇博文以一个简单的例子来讲述C++重点基础,应该适合有一点C++基础的童鞋。

涉及:构造函数,运算符重载,inline(内联)函数,friend(友元)函数,this指针,参数传递和返回值选型,临时对象

  1. 构造函数:
    1. 与类名相同,创建object时首先运行的函数
    2. 初始化方式很多,可以在函数体内{  }赋值的方式初始化,也可以采用初始化列表的方式(建议采用)
  2. ​​​运算符重载 关键字 operator描述,并不是所有的运算符都可以重载,限制自行查阅
  3. 在class body 已经实现的函数,比如real imag 默认就是inline函数
    1. 在定义任何一个函数时,需要考虑到函数是否需要用const描述,主要看函数的作用是否会更改private data
    2. 传入的参数也要注意是否用const描述,主要看参数是否会发生改变
    3. 熟悉使用const描述,会给code添加一些自行判断是否出错
  4. 友元函数:可以打破封装直接访问private data
  5. 成员函数、全局函数
    1. 成员函数:在class body 声明的
    2. 成员函数也可以成为inline函数,inline 描述符,最后是否成为内联函数,要看编译器的判断
  6. this 指针
    1. 谁调用就指向谁,比如 C1+=C2 C1调用"+="那么this指针就默认指向C1,一般情况左侧是this
  7. 参数传递和返回值类型的选择
    1. 参数传递有两种方式
      1. by value: 值的方式传递
      2. by reference: 引用传递,形式"XXX&",占4个字节,传递速度快,类似指针但不是,推荐使用
      3. by point:指针传递
    2. 返回类型
      1. void:空
      2. int...:变量类型
      3. xxxx&:reference 引用
      4. int*:指针
  8. 全局函数
    1. 和成员函数相比,没有this 指针
    2. 返回值肯定不能是by reference,因为返回的是一个local object
    3. 在这个函数中  inline complex operator + (const complex& x,const complex& y) 实际返回的是一个临时变量由complex( )创建
    4. 在这个函数inline complex&  complex::operator +=(const complex& r)能返回reference,是因为C1+=C2,C1是已经存在的object

complex.h

#ifndef __COMPLEX__
#define __COMPLEX__
class complex
{
public:
	complex(double r=0,double i=0)//pass by value
		:re(r),im(i)
	{}
	//构造函数初始化列表方式初始化data
	complex& operator += (const complex&);//by reference
	//参数传递和返回类型
	double real() const {return re;}//在class已经实现的是inline function
	double imag() const {return im;}
	//在设计任何函数时,要考虑这个函数是否加const,关键是看这个函数是否会更改private data
	~complex(){};
private:
	double re,im;
/*
友元函数,可以直接调用private data,不需要通过其他函数来取private data
友元函数可以打破封装

相同class的各个object相互为friend 友元
*/
	friend complex& __doapl (complex*,const complex&);
	
};
#endif

complex.cpp

#include "complex.h"
#include <iostream>
using namespace std;
/*
	by value by reference
	在C++编程中  参数传递和返回值  尽量使用by reference,类似指针,可以加快运行速度,占4个字节
*/
//操作符重载是成员函数或者全局函数
/*成员函数*/
inline complex&
__doapl(complex* ths,const complex& r)
{
	ths->re += r.re;
	ths->im += r.im;
	return *ths;
}

/*
operator += 实际上有两个参数,右边加在左边,左边是调用者,右边的是不会变的,所以是const pass by refrence
返回值 也是refrence,也申明成inlien函数,但是最后是不是inline要看编译器的判断

*/
inline complex&
complex::operator +=(const complex& r)
{
	//this 指针指向调用者,左边
	return __doapl(this,r);
}

//全局函数,左右都不会改变,产生的新值不是local,不能return referen
//复数+复数,复数+实数,实数+复数
inline complex
operator + (const complex& x,const complex& y)
{
	//创建对象可以用complex( ) 创建一个临时对象,也可以complex z
	return complex(x.real()+ y.real(),
				   x.imag() + y.imag());
}

inline complex
operator + (const complex& x,double y)
{
	//创建对象可以用complex( ) 创建一个临时对象,也可以complex z
	return complex(x.real() + y,x.imag());
}
inline complex
operator + (double x,const complex& y)
{
	//创建对象可以用complex( ) 创建一个临时对象,也可以complex z
	return complex(x + y.real(),y.imag());
}

ostream&
operator << (ostream& os,const complex& x)
{
	return os << '(' << x.real() << ',' << x.imag() <<')';
}

int main()
{
	double DD = 12.36;
	complex AA(10,20);
	complex BB(30,20);
	cout << AA << endl;
	cout << AA + DD << endl;
	cout << DD + BB << endl;
//	AA+=BB;
	cout << (AA+=BB) << endl;
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值