运算符重载 "abc"+"def"="abcdef"
发布网友
发布时间:2024-10-19 04:27
我来回答
共3个回答
热心网友
时间:2024-12-02 09:55
//运算符重载 "abc"+"def"="abcdef",还可以连加
注意:用到了友元,所以请不要使用VC++ 6.0这个编译器,不支持友元
#include<iostream>
using namespace std;
class Str
{
private: char *a;
public:
Str(){}
Str(char *s)//用字符串初始化类成员
{
int n=strlen(s);
a=new char[n+1];
strcpy(a,s);
}
Str(Str &s)//复制构造函数
{
int n=strlen(s.a);
a=new char[n+1];
strcpy(a,s.a);
}
Str& operator+(const Str &s)//重载+运算符
{
int n1=strlen(s.a),n2=strlen(a);
Str temp(a);
delete[] a;
a=new char[n1+n2+1];
strcpy(a,temp.a);
strcat(a,s.a);
return *this;
}
Str& operator=(const Str &s)//重载=运算符
{
int n=strlen(s.a);
a=new char[n+1];
strcpy(a,s.a);
return *this;
}
friend ostream& operator<<(ostream &os,Str &s)//重载<<运算符
{
os<<s.a<<endl;
return os;
}
~Str()
{
delete[] a;
}
};
int main()
{
Str a("abc"),b("cde"),c("ghi"),d;
d=a+b+c;
cout<<d<<endl;
return 0;
}
热心网友
时间:2024-12-02 09:58
#include<cstring>
#include<iostream>
using namespace std;
template<class T>
class Mychar{};
template<>class Mychar<char>{
char* pc;
public:
Mychar<char>(){
pc=new char[5];
strcpy(pc,"null");
}
Mychar<char>(char* c){
pc=new char[strlen(c)+1];
strcpy(this->pc,c);
}
Mychar<char>(const Mychar<char>& left);
void set_char(const char* c);
char* get_char()const;
Mychar<char>& operator=(const Mychar<char>& t);
friend ostream& operator<< (ostream& os,const Mychar<char>& mychar){
return os<<mychar.pc;
}
friend Mychar<char> operator+(const Mychar<char>& left,const Mychar<char>& right){
Mychar<char> temp;
delete []temp.pc;
temp.pc=new char[strlen(left.pc)+strlen(right.pc)+1];
temp.pc=strcat(left.pc,right.pc);
return temp;
}
};
Mychar<char>::Mychar(const Mychar<char> &left){
this->pc=new char[strlen(left.pc)+1];
strcpy(this->pc,left.pc);
}
void Mychar<char>::set_char(const char *c){
delete []this->pc;
this->pc=new char[strlen(c)+1];
strcpy(this->pc,c);
}
char* Mychar<char>::get_char() const{
return this->pc;
}
Mychar<char>& Mychar<char>::operator =(const Mychar<char>& t){
if(this!=&t){
delete [] this->pc;
this->pc=new char[strlen(t.pc)+1];
strcpy(this->pc,t.pc);
}
return *this;
}
int main(){
Mychar<char> c1="abc";
Mychar<char> c2("def");
Mychar<char>c3=c1+c2;
cin.get();
}
//另: 用char很麻烦,还不如用std::string
直接加就OK了
热心网友
时间:2024-12-02 10:01
#include <iostream.h >
#include <stdlib.h >
#include <string.h >
//const int CHAIN_NUM = 3;
class RCInst
{
public:
//char q[CHAIN_NUM];
char* ocname;
int ocNum;
RCInst()
{}
RCInst(char* a,int b)
{ // ocname=new char[strlen(a)+1];
ocname=strdup(a);
ocNum=b;
cout <<"constructing " <<ocname <<endl;
}
RCInst(RCInst& a)
{ ocname=new char[strlen(a.ocname)+1];
strcpy(ocname,a.ocname);
ocNum=a.ocNum;
// ocname=strdup(a.ocname);
cout <<"copying " <<ocname <<endl;
}
~RCInst()
{
cout <<"destructing " <<ocname <<endl;
};
RCInst operator+(RCInst& b,RCInst& c)
{
int num=b.ocNum+c.ocNum;
char* ch=new char[strlen(b.ocname)+strlen(c.ocname)];
ch=strcat(b.ocname,c.ocname);
RCInst z(ch,num);
return z;
}
void main()
{
// RCInst::RCInst_iterator a;
// a.show();
RCInst a("a",11);
cout <<"ocNum" <<a.ocNum <<endl;
RCInst b("b",21);
RCInst c;
c=a+b;
cout <<"c" <<c.ocNum <<endl;
}