c++面对对象高级开发

例子1 complex

//complex.h
#ifndef _COMPLEX_
#define _COMPLEX_

#include<iostream>

using namespace std;

//template<typename T>//定义一个函数模版 complex<int> c1(1,2)

class complex {
public:
	complex(double r = 0, double i = 0) :re(r), im(i) {}//有默认参数的构造函数 
	//complex(T r=0,T i=0){re=r;im=i;} 赋值操作,等价,但是效率差一些 
	//complex():re(0),im(0){} 构造函数重载 这里不能写,因为与前函数冲突了 
	//在单例模式中构造函数放在private中 

	complex& operator += (const complex&);//pass by reference(to const) 引用传递 
	//返回值的传递也尽量用引用传递,但是如果返回的数据对象是local object(临时对象),就必须使用值传递 
	//操作符重载 

	double real() const { return re; }//函数在class内部实现,可以成为inline的候选 
	double imag() const { return im; }//const修饰函数的作用是,保证这个函数不会改变数据 
	//double real(const complex& x)  const { return x.re; } //函数重载 
	//double imag(const complex& x)  const { return x.im; }

//	int fun(const complex& param){
//		//相同class的各个对象互为友元 
//		return param.re+param.im;
//	} 
private:
	double re, im;

	friend complex& _doapl(complex*, const complex&);//友元函数 

};

complex& _doapl(complex* ths, const complex& x) {
	ths->re += x.re;
	ths->im += x.im;
	return *ths;
}

inline complex& complex::operator += (const complex & x) {
	return _doapl(this, x);
}

//如果返回的数据对象是local object(临时对象),就必须使用值传递 
inline complex operator +(const complex& x, const complex& y) {
	return complex(x.real() + y.real(), x.imag() + y.imag());
}

//输入操作符重载 
ostream& operator <<(ostream& os, const complex& x) {
	return os << '(' << x.real() << ',' << x.imag() << ')';
}

#endif


//main.cpp 
int main() {
	complex c1(2.5, 3.5);
	complex c2(2, 3);
	c1 += c2;
}

例子2 String

#ifndef _MYSTRING_
#define _MYSTRING_

#include<iostream>

class String {
public:
	String(const char* cstr = 0);
	String(const String& str);
	String& operator = (const String& str);
	~String();
	char* get_c_str() const { return m_data; }
private:
	char* m_data;
};

//构造函数
inline String::String(const char* cstr = 0) {
	if (cstr) {
		m_data = new char[strlen(cstr) + 1];
		strcpy(m_data, cstr);
	}
	else {
		//未指定初值,空字符串
		m_data = new char[1];
		*m_data = '\0';
	}
}

//析构函数
inline String::~String() {
	delete[] m_data;
}

//拷贝构造函数(深拷贝)
//注意编译器自动生成的拷贝构造函数是浅拷贝,不会创造一片空间,
//string a,b; 
//a=b;//此时ab指向的是同一片空间
inline String::String(const String& str) {
	m_data = new char[strlen(str.m_data) + 1];//需要先创建一块空间
	strcpy (m_data, str.m_data);
}

//拷贝赋值
inline String& String::operator=(const String& str) {
	if (this == &str) {//检测自我复制,因为自我拷贝会出错,第一步已将本体销毁了
		return *this;
	}
	delete[] m_data;
	m_data = new char[strlen(str.m_data) + 1];
	strcpy(m_data, str.m_data);
	return *this;
}

//输出<<重载
std::ostream& operator<<(std::ostream& os, const String& str) {
	os << str.get_c_str();
	return os;
}
int main() {
	String s1();
	String s2("hello");
	String s3(s2);//拷贝构造
	String* p = new String("hello");//动态创建对象
	delete p;//使用完必须释放,否则发生内存泄漏
	std::cout << s3 << std::endl;
	s3 = s2;//拷贝赋值
	std::cout << s3 << std::endl;
}

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值