首页 C 实验报告之静态成员、运算符重载

C 实验报告之静态成员、运算符重载

举报
开通vip

C 实验报告之静态成员、运算符重载题目1: 定义一个复数类,通过重载运算符: *,/,直接实现二个复数之间的乘除运算。编写一个完整的程序,测试重载运算符的正确性。要求乘法“*”用友元函数实现重载,除法“/”用成员函数实现重载。 源程序1 /*******************第1题*******************/ /******************单森汉*****************/ /******************2012-5-1*****************/ #include using std::cout; ...

C  实验报告之静态成员、运算符重载
题目1: 定义一个复数类,通过重载运算符: *,/,直接实现二个复数之间的乘除运算。编写一个完整的程序,测试重载运算符的正确性。要求乘法“*”用友元函数实现重载,除法“/”用成员函数实现重载。 源程序1 /*******************第1题*******************/ /******************单森汉*****************/ /******************2012-5-1*****************/ #include using std::cout; using std::endl; class Complex{ float Real, Image; public: Complex(float r=0,float i=0)    { Real=r;Image=i;} void Show() {cout <<"Real="< using std::cout; using std::endl; class Vect {private: float *p; int n; int x; int y; public :Vect(int x1,int y1,int num) { x = x1; y = y1; n = num; } Vect(){} ~Vect() {} public: friend Vect &operator - (Vect &a, Vect &b); Vect operator + (Vect &a) { if(a.n == n) { return Vect(a.x+x, a.y+y, n); } return a; } void show(){ cout<<"x="< using namespace std; class array { int *a; int lenth; public: array(int lenth=5) { a=new int[lenth]; this->lenth=lenth; } int & operator [](int i) { if(i=0) return a[i]; else cout<<"输入有误"<>a[i]; cout<<"输入数组b(5个元素):"<>b[i]; c=a+b; a=b; cout<<"数组a和数组b对应列相加后的新数组:"< using namespace std; class Hotel{ private: static int m_TotalCustNum; char* m_CustomerName; int m_CustomerNum; //通过m_TotalCustNum来定义每个顾客的编号 public: Hotel(char* a_Customer){ m_CustomerName=a_Customer; m_TotalCustNum++; //增加顾客 m_CustomerNum=m_TotalCustNum; } //记得,m_TotalCustNum++  和  m_CustomerNum按照m_TotalCustNum来生成 ~Hotel() { //delete m_CustomerName; } //记得  m_CudeletestomerName void Display() { cout<<"姓名:"< //包含源文件iostream using namespace std; //使用命名空间 class My //定义一个My类 { public: //定义My类的公共接口 My(int aa) //定义一个构造函数 { A=aa; //把aa的值赋给A B-=aa; //B-aa的结果赋给B } static void fun(My m); //声明静态函数fun private: //定义My类的私有数据 int A; //定义整型变量A static int B; //定义静态整型变量B }; void My::fun(My m) //定义My类中空类型的fun函数 { cout<<"A="< //包含源文件iostream using namespace std; //使用命名空间 class Student //定义一个Student类 { public: //定义类的公共接口 void scoretotalcount(float s); //声明函数 static float sum(); //声明一个静态单精度浮点型函数 static float average(); //声明一个静态单精度浮点型函数 private: //定义类的私有数据 static float total; //定义静态单精度浮点型变量total static float count; //定义静态单精度浮点型变量count float score; //定义单精度浮点型变量score }; void Student::scoretotalcount(float s) //定义类中scoretotalcount函数用来统计学生总分 { score=s; //把s的值赋给score count++; //人数自增 total+=score; //统计总分 } float Student::sum()//返回总分 { return total; //返回总分的值 } float Student::average() //求学生平均分 { return total/count; //返回总分除以总人数的值 } float Student::total=0; //使总分初始值为0 float Student::count=0; //使总人数初始值为0 int main() //主函数 { Student stud[20]; //定义一个存放学生数据的一维数组 int n; //定义一个整型变量n float s; //定义一个单精度浮点型变量s cout<<"input number of students:"; //提示输入学生人数 cin>>n; //输入学生人数 for (int i = 0; i < n; i++)//用for循环求学生总分 { cout<<"input score of No."<>s; //输入一个学生成绩 stud[i].scoretotalcount(s); //统计总分 } cout<<"total score :"< //包含iostream文件 #include //包含cstdio文件 using namespace std; //使用命名空间 class Ruler; class Book //创建一个Book类 { public: //定义Book类的公共接口 Book(float j) {weight =j;} //定义构造函数Book friend float totalWeight(Book &abook,Ruler &aruler); //声明友元函数 private: //定义Book类的私有数据 float weight;//定义一个单精度浮点型的变量weight }; class Ruler //创建一个Ruler类 { public: //定义Ruler类的公共接口 Ruler(float j ){ weight=j;} //定义构造函数Ruler friend float totalWeight(Book &abook ,Ruler &aruler); //声明友元函数 private: //定义Ruler类的私有数据 float weight; //定义一个单精度浮点型的变量weight }; float totalWeight (Book &abook ,Ruler &aruler) //定义友元函数totalWeight { return abook.weight+aruler.weight; //返回书质量加上尺子质量的值 } int main() //主函数 { float w; //定义一个单精度浮点型的变量w cout<<"input weight of book"<>w; //输入书的质量 Book b(w); //调用Book函数 cout<<"input weight of ruler"<>w;//输入尺子的质量 Ruler r(w); //调用Ruler函数 cout<<"Totalweight of the book and ruler is"<
本文档为【C 实验报告之静态成员、运算符重载】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_792768
暂无简介~
格式:doc
大小:55KB
软件:Word
页数:0
分类:工学
上传时间:2019-09-11
浏览量:24