C++特殊成员函数实例

 

#include "stdafx.h"
#include<iostream>
#include<cstring>
#include <fstream>
#pragma warning( disable : 4996)
using std::cout;

class stringBad
{
public:
	stringBad(const char * s){//构造函数
		len = std::strlen(s);
		str = new char[len + 1];
		std::strcpy(str, s);
		num_strings++;
		cout << num_strings << ":\"" << str << "\"object created\n";
	}
	stringBad(const stringBad & s){//复制构造函数
		num_strings++;
		len = s.len;
		str = new char[len + 1];
		std::strcpy(str, s.str);
		cout << num_strings << ":\"" << str << "\"object created\n";
	}
	stringBad(){//不含参数的构造函数
		len = 4;
		str = new char[4];
		std::strcpy(str, "C++");
		num_strings++;
		cout << num_strings << ":\"" << str << "\"default object created\n";
	}
	~stringBad(){//析构函数
		cout << "\"" << str << "\"object deleted, ";
		--num_strings;
		cout << num_strings << " left\n";
		delete[] str;
	}
	friend std::ostream & operator<<(std::ostream & os, const stringBad & st){
		os << st.str;
		return os;
	}
	stringBad & operator=(const stringBad & st){//赋值运算符
		if (this == &st)
			return *this;
		delete[] str;
		len = st.len;
		str = new char[len + 1];
		std::strcpy(str,st.str);
		return *this;
	}
private:
	char*str;
	int len;
	static int num_strings;//类外初始化
};
int stringBad::num_strings = 0;
void callme1(stringBad &);
void callme2(stringBad);
int main()
{
	using std::endl;
	{
		cout << "Starting an inner block.\n";
		stringBad headline1("Celery Stalks at Midnight"); //调用含参数构造函数
		stringBad headline2("Lettuce Prey");              //调用含参数的构造函数
		stringBad sports("Spinach Leaves Bow");           //调用含参数的构造函数
		cout << "headline1:" << headline1 <<endl;         //调用operator<<
		cout << "headline2:" << headline2 << endl;        //调用operator<<
		cout << "sports:" << sports << endl;              //调用operator<<
		callme1(headline1);        //传引用不会调用构造函数和复制构造函数
		cout << "headline1:" << headline1 << endl;        //调用operator<<
		callme2(headline2);        //传引用会调用复制构造函数,运行完只会调用析构函数进行内存释放
		cout << "headline2:" << headline2 << endl;        //调用operator<<
		cout << "initialize one object to another:\n";
		stringBad sailor = sports;                        //调用复制构造函数
		cout << "sailor:" << sailor << endl;              //调用operator<<
		cout << "assgin one object to another:\n";
		stringBad knot;            //调用不含参数的构造函数
		knot = headline1;           //调用赋值运算符
		cout << "knot:" << knot << endl;
		cout << "exiting the block\n";
	}
	cout << "End of main()\n";
	system("pause");
	return 0;
}

void callme1(stringBad & rsb)
{
	cout << "sting passed by reference:\n";
	cout << "   \"" << rsb << "\n";
}
void callme2(stringBad sb)
{
	cout << "string passed by value:\n ";
	cout << "   \"" << sb << "\"\n";
}

运行结果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WJsuperrunner

你的鼓励是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值