文档库

最新最全的文档下载
当前位置:文档库 > C++程序设计7单元

C++程序设计7单元

杭州电子科技大学

C++程序设计

实验报告

实验名称:实验7 运算符重载

实验组号:周五3-5

指导老师:钟华

专业:通信工程

班级:

姓名:王健

学号: 14072119

实验日期:2015年12月25日

1.阅读程序,写出程序运行结果

(1)

#include

classabc

{

inta,b,c;

public:

abc(intx,inty,int z):a(x),b(y),c(z){}

friend ostream&operator<<(ostream&out,abc& f); //对"<<"运算符的重载};

ostream&operator<<(ostream&out,abc& f)

{

out<<"a="<

}

int main()

{

abcobj(10,20,30);

cout<

return 0;

}

C++程序设计7单元

(2)

#include

class number

{

int n;

public:

number(int x):n(x){}

number& operator++(){++n;return *this;} //++前置自增

number& operator++(int){n++;return *this;} //++后置自增

friend number& operator--(number &o); //--前置自增

friend number& operator--(number o,int); //--后置自增,但o为临时变量,函数运行完内存消失,所以此重载无效

void display(){cout<<"this number is:"<

};

number& operator--(number &o){--o.n;return o;}

number& operator--(number o,int){o.n--;return o;}

int main()

{

number n1(10);

++ ++ ++n1; //先++n1.n,为n1.n 为11,在++11;得12;最后++12

n1.display();

n1++; //n1.n++为14

n1.display();

--n1; //--n1.n为13

n1.display();

n1-- -- --; //重载无效,n1依然为13;

n1.display();

return 0;

}

C++程序设计7单元

4.写出自定义字符串类所有函数的类外定义

#include

#include

class string

{

private:

char *buffer;

unsignedbuflen;

public:

string(); //构造函数

string(unsigned);

string(char);

string(const char*);

string(const string &); //赋值构造函数~string(); //析构函数

string& operator=(const string &right);

string& operator+=(const string &right); //运算符重载friend string operator+(const string &left,const string &right);//连接

string operator()(unsigned start,unsignedlen); //取子串

string string::substr(unsigned start,unsignedlen)const;

unsigned length()const;

char &operator[](unsigned index)const; //访问单个字符

void dplay(){ //输出

cout<<"len: "<

for(unsigned i=0;buffer[i]!='\0';i++)

cout<

cout<

}

};

string::string(){buflen=0;buffer=NULL;}

string::string(unsigned a){ //创造一个a+1大小的空间buflen=a;

buffer=new char[a+1];

for(unsigned i=0;i

buffer[i]='\0';

}

string::string(char a){ //储存一个字符buflen=1;

buffer=new char[2];

buffer[0]=a;

buffer[1]='\0';

}

string::string(const char *a){ //储存一个字符数组buflen=strlen(a);

buffer=new char[buflen+1];

for(unsigned i=0;i

buffer[i]=a[i];

buffer[i]='\0';

}

string::string(const string &a){ //复制构造函数buflen=a.length();

buffer=new char[buflen+1];

for(unsigned i=0;i<=buflen;i++)

buffer[i]=a.buffer[i];

}

string::~string(){

delete[]buffer;

buflen=0;

}

string& string::operator=(const string &right){ //=号的重载buflen=right.length();

delete[]buffer;

buffer=new char[buflen+1];

for(unsigned i=0;i<=length();i++)

buffer[i]=right.buffer[i];

return *this;

}

string& string::operator+=(const string &right){ //+=重载

unsigned b=right.length()+buflen;

char *a=new char[b+1];

for(unsigned i=0;i

a[i]=buffer[i];

for(;i

a[i]=right.buffer[i-buflen];

a[i]='\0';

delete[]buffer;

buffer=a;

buflen=b;

return *this;

}

string operator+(const string &left,const string &right){ //+重载string a;

a+=left;

a+=right;

return a;

}

string string::operator()(unsigned start,unsignedlen){ //取子串()重载stringa;unsigned i=0;

a.buflen=len;

a.buffer=new char[len+1];

while(len--)

{

a.buffer[i]=buffer[i+start-1];

i++;

}

a.buffer[i]='\0';

return a;

}

string string::substr(unsigned start,unsignedlen)const{ //成员函数取子串stringa;unsigned i=0;

a.buflen=len;

a.buffer=new char[len+1];

while(len--)

{

a.buffer[i]=buffer[i+start-1];

i++;

}

a.buffer[i]='\0';

return a;

}

unsigned string::length()const{return buflen;}

char& string::operator[](unsigned index)const{

if(index>=buflen){ //判断是否符合static char stc='\0';

returnstc;

}

else return buffer[index];

}

int main()

{

string s1("abcdef"),s2('x'),s3(s1),s4; //测试函数s3.dplay();

s4=s2;s4.dplay();

s4+=s1;s4.dplay();

s4=s1+s2;s4.dplay();

s4=s1(1,2);s4.dplay();

s4=s1.substr(3,3);s4.dplay();

s4=s1[5];s4.dplay();

return 0;

}

C++程序设计7单元