1、C+语言程序设计实验报告 10C+语言程序设计实验报告基础题一实验任务:定义一个哺乳动物Mammal类,再由此派生出狗Dog类,二者都定义 Speak()成员函数,基类中定义为虚函数,定义一个Dog类的对象,调用Speak函数,观察运行结果。代码: #include using namespace std; class Animal public: virtual void Speak(); ; void Animal:Speak() coutAnimal中的SPEAK()函数被调用; class Dog:virtual public Animal public: virtual void S
2、peak(); ; void Dog:Speak() coutDog中的SPEAK()函数被调用endl; void main() Dog Dog1; Dog1.Speak(); 结果:基础题二实验任务:对Point类重载+(自增)、-(自减)运算符代码: #include using namespace std; class Point public: Point(int x=0,int y=0)this-x=x;this-y=y; void show(); Point& operator+(); Point operator+(int); Point& operator-(); Point
3、 operator-(int); private: int x,y; ; void Point:show() cout该点坐标为(x,y)endl; Point& Point:operator+() x+; y+; return *this; Point Point:operator+(int) Point old=*this; +(*this); return old; Point& Point:operator-() x-; y-; return *this; Point Point:operator-(int) Point old=*this; -(*this); return old;
4、 int main() Point myPoint(1,3); cout第一次输出 ; myPoint.show(); coutShow myPoint+: ; (myPoint+).show(); coutShow +myPoint: ; (+myPoint).show(); coutShow myPoint-: ; (myPoint-).show(); coutShow -myPoint: ; (-myPoint).show(); return 0; 结果:提高题一实验任务:设计一个字符串类MyString,具有构造函数、析构函数、拷贝构造函数,重载运算符+,尽可能地完善它,使之能满足各种
5、需要。代码:#include #include class person public: person(char *pn); person(person &p); person(); private: char *pname; ; person:person(char *pn) cout构造函数: pnendl; pname=new charstrlen(pn)+1; if(pname!=0) strcpy(pname,pn); person:person(person &p) cout拷贝 p.pname 到新的堆空间n; pname=new charstrlen(p.pname)+1; i
6、f(pname!=0) strcpy(pname,p.pname); person:person() cout析构函数: pnameendl; pname0=0; delete pname; void main() person p1(jack); person p2(p1); 结果:提高题二实验任务:声明一个Shape抽象类,再此基础上派生出Rectangle和Circle类,二者都有GetArea()函数计算对象的面积,GetPerim()函数计算对象的周长。代码:#includeusing namespace std;#define n 3.14class Shape public: v
7、irtual void GetArea()=0; virtual void GetPerim()=0;class Rectangle:public Shapepublic: Rectangle(int a,int b)this-a=a;this-b=b; void GetArea(); void GetPerim();private: int a,b;void Rectangle:GetArea()cout矩形面积为a*bendl; void Rectangle:GetPerim()cout矩形周长为a)+(2*this-b)c=c; void GetArea(); void GetPerim
8、();private: int c;void Circle:GetArea()cout圆形面积为c*c*nendl; void Circle:GetPerim()cout圆形周长为c)GetArea(); void fun1(Shape *ptr) ptr-GetPerim(); void main() Rectangle obj(3,4); Circle obj1(2); fun(&obj); fun1(&obj); fun(&obj1); fun1(&obj1); 结果:选做题一实验任务:编写一个程序,计算正方体、球体和圆柱体的表面积和体积。(要求:抽象出一个公共基类作为虚基类,运用虚函数
9、的知识设计程序)代码:#includeusing namespace std;#define n 3.14class Shujupublic: virtual void biaomianji()=0; virtual void tiji()=0;class zhengfangti:public Shujupublic: zhengfangti(int a)this-a=a; void biaomianji(); void tiji();private: int a ;void zhengfangti:biaomianji()cout正方形的表面积为6*a*aendl;void zhengfan
10、gti:tiji()cout正方形的体积为a*a*ab=b; void biaomianji(); void tiji();private: int b ;void qiuti:biaomianji()cout球体的表面积为4*n*b*bendl;void qiuti:tiji()cout球体的体积为(4.0/3.0)*n*b*b*br=r;this-d=d; void biaomianji(); void tiji();private: int r,d;void yuanzhuti:biaomianji()cout圆柱体的表面积为2*n*r*r+2*n*r*dendl;void yuanzh
11、uti:tiji()cout圆柱体的体积为n*r*r*dbiaomianji();void fun1(Shuju *ptr)ptr-tiji();void main()zhengfangti zheng(2);qiuti qiu(3);yuanzhuti yuanzhu(4,5);fun(&zheng);fun1(&zheng);fun(&qiu);fun1(&qiu);fun(&yuanzhu);fun1(&yuanzhu);结果:选做题二实验任务:编写一个分数类fraction ,其分子、分母为整数,通过重载运算符+、-、*、/ ,实现该类数据之间的四则运算,要求计算结果为最简分数,如(7
12、/3)*(9/14)=(3/2)4*(7/8)=(7/2)代码:#include using namespace std;class fractionpublic: fraction(int n=1,int d=1) num=n; den=d; divideZero(); ; fraction:divideZero()if (0 = den)cout 输入非法,分母为零! endl;return true;return false; fraction& operator=(const fraction& other) num=other.num; den=other.den; return *
13、this; fraction operator+(const fraction& other) fraction res; res.num=num*other.den+den*other.num; res.den=den*other.den; return res; fraction operator-(const fraction& other) fraction res; res.num=num*other.den-den*other.num; res.den=den*other.den; return res; fraction operator*(const fraction& oth
14、er) return fraction(num*other.num,den*other.den); fraction operator/(const fraction& other) return fraction(num*other.den,den*other.num); void display() coutnum/denendl; private: int num,den;void main() fraction a(2,5); fraction b(a); fraction c; b=fraction(3,7); c=a+b; c.display(); c=a-b; c.display(); c=a*b; c.display(); c=a/b; c.display();结果:
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1