haihongyuan.com
海量文库 文档专家
全站搜索:
您现在的位置:首页 > 幼儿教育 > 幼儿读物幼儿读物

郭飞20124206199

发布时间:2013-11-04 12:02:41  

实验三 运算符重载

题目一:题目内容

建立一个字符串类名为String,至少写出三个构造函数,重载赋值等号=、加号+、关系等于==,另写出析构函数和其他的成员函数,如求字符串的长度、输出字符串等。在main()中定义String类对象测试所写函数是否正确。

一、程序

#ifndef STRING_H_INCLUDED

#define STRING_H_INCLUDED

#include <iostream>

using namespace std;

class String{

public :

//String();

String(char *str);

String(char *str,char *ch);

String& operator =(String &ch);

String& operator +(String &ch);

int operator ==(String &ch);

int strle();

~String();

void StuOutput();

private:

char *st;

char s[122];

};

#endif // STRING_H_INCLUDED

#include <iostream>

#include "String.h"

using namespace std;

int main()

{

int i;

char str[123]={"我是"},strt[233]={"guofei"},sb[122]={"fei"},eb[123]={"guizhousheng String a(str),b(str,strt),c(sb),d(eb);

cout<<"直接输出一个数的值"<<endl;

a.StuOutput();

cout<<"直接输出一个数的值"<<endl;

b.StuOutput();

cout<<"把一个数的值赋值给另一个: a=c"<<endl;

a=c;

a.StuOutput(); "};

cout<<"把一个数的值赋值给另一个:a=d"<<endl; a=d;

a.StuOutput();

cout<<"两数之间相加: a+c"<<endl;

a+c;

a.StuOutput();

i=a==c;

cout<<"比较两数之间是否相等: "<<i<<endl; return 0;

}

#include <iostream>

#include "String.h"

#include <string.h>

using namespace std;

String::String(char *str)

{

//delete [] st;

st=new char[strlen(str)+1];

strcpy(st,str);

strcpy(s,"guofei");

}

String::String(char *str,char *ch)

{

//delete [] st;

st=new char[strlen(str)+1];

strcpy(st,str);

strcpy(s,ch);

}

String &String:: operator =(String &ch)

{

delete [] this->st;

this->st=new char[strlen(ch.st)+1];

strcpy(this->st,ch.st);

return *this;

}

String &String:: operator +(String &ch)

{

strcat(this->st,ch.st);

return *this;

}

int String::operator ==(String &ch)

{

if(strcmp(this->st,ch.st)==0)

return 1;

return 0;

}

int String::strle()

{

return strlen(st);

}

String::~String()

{

delete [] this->st;

}

void String::StuOutput()

{

cout<<st<<'\t'<<s<<endl;

}

二、程序结果

三、实验总结

1 了解了运算符重载的思想;

2 掌握了运算符重载过程中参数的定义方式;

题目二:题目内容

定义一个复数类,将复数的乘除以运算用*、/表示,使用运算符重载函数,

将运算符重载为类的成员函数或友元函数。

一、程序

#ifndef COMPLEX_H_INCLUDED

#define COMPLEX_H_INCLUDED

#include <iostream>

using namespace std;

class Complex{

public :

Complex(double a=0,double b=0);

Complex& operator /(const Complex &c) const; Complex& operator *(const Complex &c) const; void complexOut();

private :

double real;

double imag;

};

#endif // COMPLEX_H_INCLUDED

#include <iostream>

#include "Complex.h"

using namespace std;

int main()

{

Complex b1(24,24),b2(12, 12),b3;

cout<<"b1=";

b1.complexOut();

cout<<"b2=";

b2.complexOut();

cout<<"b1/b2 "<<endl;

b3=b1/b2;

b3.complexOut();

cout<<"b1*b2 "<<endl;

b3=b1*b2;

b3.complexOut();

return 0;

}

#include <iostream>

#include "Complex.h"

using namespace std;

Complex ::Complex(double a,double b)

{

real=a;imag=b;

}

Complex& Complex::operator/(const Complex &c) const {

Complex c5;

double d=imag*imag+c.imag*c.imag;

c5.real=(real*c.real+imag*c.imag)/d;

c5.imag=(c.real*imag-real/c.imag)/d;

return c5;

}

Complex& Complex::operator*(const Complex &c) const {

Complex c4;

c4.real=real*c.real-imag*c.imag;

c4.imag=imag*c.real+real*c.imag;

return c4;

}

void Complex ::complexOut()

{

cout<<real<<"+"<<imag<<"y"<<endl;

}

二、程序结果

三、实验总结

1通过这次试验我学会了运算符重载的思想;

2掌握了运算符重载过程中参数的定义方式

网站首页网站地图 站长统计
All rights reserved Powered by 海文库
copyright ©right 2010-2011。
文档资料库内容来自网络,如有侵犯请联系客服。zhit326@126.com