码迷,mamicode.com
首页 > 编程语言 > 详细

Effective C++ 12代码

时间:2014-09-15 12:35:48      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:des   blog   io   os   div   sp   cti   代码   log   

#include <iostream>
#include <string>
using namespace std;

class Phone
{
public:
	Phone(string name);
	virtual ~Phone(){cout<<"Phone destory"<<endl;}
	virtual void print();
	string GetName(){return m_name;}
private:
	string m_name;
};
Phone::Phone(string name)
: m_name(name)
{
	cout<<"Phone create"<<endl;
}
void Phone::print()
{
	cout<<"phone name:"<<m_name<<endl;
}

class Iphone:public Phone
{
public:
	Iphone(string name, bool IsSiri = 1);
	~Iphone();
	Iphone(const Iphone& rhs);
	Iphone& operator=(const Iphone& rhs);
	virtual void print();
private:
	bool m_bIsSiri;
};
Iphone::Iphone(string name, bool IsSiri)
:Phone(name)
, m_bIsSiri(IsSiri)
{
	cout<<GetName() + " create"<<endl;
}
Iphone::Iphone(const Iphone& rhs)
:Phone(rhs)	//base的copy 函数
{
	cout<< "copy func"<<endl;
	m_bIsSiri = rhs.m_bIsSiri;
}
Iphone& Iphone::operator=(const Iphone& rhs)
{
	cout<< "operator= func"<<endl;
	Phone::operator=(rhs);	//base的赋值操作
	m_bIsSiri = rhs.m_bIsSiri;
	return *this;
}
Iphone::~Iphone()
{
	cout<<GetName() + " destory"<<endl;
}
void Iphone::print()
{
	cout<<"i am"<<" " + GetName();
	if (m_bIsSiri)
	{
		cout<<" and i have siri"<<endl;
	}
	else
	{
		cout<<" and i have not siri"<<endl;
	}
}
int main()
{
	Phone* pPhone = new Iphone("iphone");
	pPhone->print();
	delete pPhone;
	
	pPhone = new Iphone("HTC", 0);
	pPhone->print();
	delete pPhone;
	pPhone = NULL;

	Iphone iphone("iphone");
	Iphone HTC("HTC");
	HTC = iphone;//operator=
	HTC.print();
	Iphone samsung(HTC);//copy构造函数
	samsung.print();
	return 1;
}

  

Effective C++ 12代码

标签:des   blog   io   os   div   sp   cti   代码   log   

原文地址:http://www.cnblogs.com/2012harry/p/3972460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!