c++输入输出重载,赋值,加法运算符重载

在类里面声明了构造函数,但是没有写出它的实现,则在运行的时候会出现

error LNK2001: unresolved external symbol "public: __thiscall MyClass::MyClass(void)" (??0MyClass@@QAE@XZ)

这样的错误,所以要在外部去定义该构造函数或者是在内部定义


如果要cout输出string类型,但是没有引用头文件的话

error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<
char> >' (or there is no acceptable conversion)

会出现上面的错误


类里面的成员函数的参数,在声明和定义时一定要写一样,如果不一样,则会被认为是两个不同的函数。编译时就会报错了

error C2248: 'data' : cannot access private member declared in class 'MyString'

例如上面的是因为参数有出入,则被当成了别的函数,不能访问private成员


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

class MyClass{

	friend istream & operator>>(istream &, MyClass &);
	friend ostream & operator<<(ostream &, MyClass &);
public:



	MyClass();
	MyClass(string m_name,int m_age,int m_score):name(m_name),age(m_age),score(m_score){}
	~MyClass();
private:
	string name;
	int age;
	int score;
};


MyClass::MyClass()
:name(""),age(0),score(0)
{

}

MyClass::~MyClass()
{
	
}

istream & operator>>(istream & is, MyClass & s)
{
	cout<<"input name age score"<<endl;
	is >> s.name>> s.age >> s.score;
	return is;
}


ostream & operator<<(ostream & out, MyClass &s)
{

	out<<s.name<<" "<<s.age<<" "<<s.score<<endl;
	return out;
}


int main(void)
{
	MyClass a;
	cin>>a;
	cout<<a;
	return 0;
}


#include <iostream>
using namespace std;

class MyString
{
	friend ostream & operator<<(ostream &,const MyString &);
	friend MyString operator+(const MyString&,const MyString &);

public:
	MyString(const char *);
	MyString(const MyString &);
	~MyString();

	MyString & operator=(const MyString &);
	int length(void) const;
	bool isEmpty(void) const;

private:
	char *data;
	int len;
};

MyString::MyString(const char * str)
{
	cout<<"普通构造函数"<<endl;
	if(str == NULL)
	{
		len = 0;
	data = new char[1];
	*data = '\0';
	}
	else
	{
		len = strlen(str);
		data = new char[len+1];
		strcpy(data,str);
	}
}

MyString::MyString(const MyString & other)
{
	cout<<"拷贝构造函数"<<endl;
	len = other.len;
	data = new char[len+1];
	strcpy(data,other.data);
}

MyString::~MyString(void)
{
	cout<<"析构函数"<<endl;
	delete[] data;
}

MyString & MyString::operator=(const MyString &other)
{
	cout<<"赋值构造函数"<<endl;
	if(this == &other)
		return *this;
	delete[] data;
	len = other.len;
	 data = new char[len+1];
	 strcpy(data,other.data);
	 return *this;
}

int MyString::length(void) const
{
	return len;
}

bool MyString::isEmpty(void) const
{
	return len==0;
}

ostream & operator<<(ostream & out,const MyString & str)
{
	out<<str.data;
	return out;
}

MyString operator+( const MyString & a,const MyString & b)
{
	cout<<"运算符重载+号"<<endl;

	MyString temp("");
	delete[] temp.data;
	temp.len = a.len  + b.len;
	temp.data = new char[temp.len+1];
	strcpy(temp.data,a.data);
	strcat(temp.data,b.data);
	return temp;
}


int main(void)
{
	MyString str("hello"); //普通的构造函数
	MyString str1 = str;  //拷贝构造函数
	cout<<"length of st1 "<<str1.length()<<endl;
	cout<<"value of str1 "<<str1<<endl;

	 //1.先通过构造函数MyString(const char *)构造一个Mystring的对象,
	 //2.然后调用拷贝构造函数 {str1 = str;和str1(str)是一样的}
	 //3.拷贝构造函数和赋值构造函数的区别是,拷贝构造函数是对象被创建的时候调用的,赋值构造函数只能被已经定义的对象调用
	 //4.然后把临时创建的对象析构掉
	MyString str2=""; 
	cout<<"Is str2 empty?  "<<(str2.isEmpty()?"true":"false")<<endl;

	//1.先通过MyString(const char *)把"world"转成一个临时的MyString对象,
	//2.然后调用赋值构造函数,给str2赋值
	//3.然后把临时创建的对象析构掉
	str2 = "world";
	cout<<"Now value of str2: "<<str2<<endl;


	//1.先调用重载运算符+把str1和str2相加,里面创建一个临时的对象temp
	//2.把str1和str2的相加后的返回值,通过拷贝构造函数,给str对象
	//3.调用析构函数,把temp的对象析构掉
	cout<<"Now Value of str: "<< str1+str2<<endl;
	//4.把str对象析构掉
	

	//1.先把"_world"变成一个MyString对象,调用普通构造函数
	//2.然后调用重载的运算符+号,
	//3,重载运算符的时候,调用普通的构造函数创建一个临时的对象
	//4,调用拷贝构造函数把重载运算符的返回值放到一个对象里
	//5,析构函数析重载运算符里面创建的临时对象
	cout<<"Value of another expression: "<< str1+"_world" <<endl; //为什么函数里面必须是const的参数呢
	//6.析构函数析构存放"_world"的临时对象
	//7.析构函数析构放算数运算符返回值的对象

	cout<<"Value of another expression: "<<"hello "+str2<<endl;
	
	//1.调用我们实现的operator+函数,参数为str1,str2的对象的引用
	//2.在operator+函数的栈上构造一个局部变量temp
	//3.将str1和str2的字符串内存赋值到temp对象中
	//4.return temp;这一句,首先根据temp拷贝构造一个临时对象,然后operator+函数返回,temp对象被析构
	MyString str3 = str1 + str2;
	//5.根据上一部构造的临时对象的拷贝构造str3,然后临时对象析构
	cout<<"Value of str3: "<<str3<<endl;
	

	return 0;
}




  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值