您的位置:首页 > 编程语言 > C语言/C++

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

2013-02-28 11:36 197 查看
在类里面声明了构造函数,但是没有写出它的实现,则在运行的时候会出现

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: