正在加载图片...
10.1。1C++面向对的程序实例 r3C++程序实例3 ≈【例10.3】“+”运算符重载。(程序名为1103cpp。) #include<iostream. h> 据言程设计 class rectangle ∥定义一个矩形类 dpublic rectangle(float len=0, float wid=0)∥构造函数 i length=len width=wid float GetAreao{ return length* width;}∥成员函数(计算面积) rectangle operator+( rectangle a2)∥将"+"运算符重载 rectangle a ∥用于两个矩形对象相加 60 a length-length a width=width+a2. Get Area(/length return rectangle(a length, a width) 环 境 返回本节目录【例10.3】“+”运算符重载。(程序名为l10_3.cpp。) #include<iostream.h> class rectangle //定义一个矩形类 {public: rectangle(float len=0,float wid=0) //构造函数 { length=len; width=wid; } float GetArea(){return length*width;} //成员函数(计算面积) rectangle operator +(rectangle a2) //将"+"运算符重载 { rectangle a; //用于两个矩形对象相加 a.length=length; a.width=width+a2.GetArea()/length; return rectangle(a.length,a.width); } 10.1.1 C++面向对象的程序实例 《 C 语 言 程 序 设 计 》(Visual C++ .6 0 环 境 ) 返回本节目录 3.C++程序实例3
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有