C语言程序设计实验报告10.docx

上传人:b****8 文档编号:11337241 上传时间:2023-02-27 格式:DOCX 页数:15 大小:90.05KB
下载 相关 举报
C语言程序设计实验报告10.docx_第1页
第1页 / 共15页
C语言程序设计实验报告10.docx_第2页
第2页 / 共15页
C语言程序设计实验报告10.docx_第3页
第3页 / 共15页
C语言程序设计实验报告10.docx_第4页
第4页 / 共15页
C语言程序设计实验报告10.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

C语言程序设计实验报告10.docx

《C语言程序设计实验报告10.docx》由会员分享,可在线阅读,更多相关《C语言程序设计实验报告10.docx(15页珍藏版)》请在冰豆网上搜索。

C语言程序设计实验报告10.docx

C语言程序设计实验报告10

C++语言程序设计实验报告

基础题一

实验任务:

定义一个哺乳动物Mammal类,再由此派生出狗Dog类,二者都定义Speak()成员函数,基类中定义为虚函数,定义一个Dog类的对象,调用Speak函数,观察运行结果。

代码:

#include

usingnamespacestd;

classAnimal

{

public:

virtualvoidSpeak();  

};

voidAnimal:

:

Speak()

{

cout<<"Animal中的SPEAK()函数被调用";

}

classDog:

virtualpublicAnimal

{

public:

virtualvoidSpeak();  

};

voidDog:

:

Speak()

{

cout<<"Dog中的SPEAK()函数被调用"<

}

voidmain()

{

DogDog1;

Dog1.Speak();

}

结果:

基础题二

实验任务:

对Point类重载++(自增)、--(自减)运算符

代码:

#include

usingnamespacestd;

classPoint

{

public:

Point(intx=0,inty=0){this->x=x;this->y=y;}

voidshow();

Point&operator++();

Pointoperator++(int);

Point&operator--();

Pointoperator--(int);

private:

intx,y; 

};

voidPoint:

:

show()

{

cout<<"该点坐标为"<<"("<

}

Point&Point:

:

operator++()

{

x++;

y++;

return*this;

}

PointPoint:

:

operator++(int)

{

Pointold=*this;

++(*this);

returnold;

}

Point&Point:

:

operator--()

{

x--;

y--;

return*this;

}

PointPoint:

:

operator--(int)

{

Pointold=*this;

--(*this);

returnold;

}

intmain()

{

PointmyPoint(1,3);

cout<<"第一次输出";

myPoint.show();

cout<<"ShowmyPoint++:

";

(myPoint++).show();

cout<<"Show++myPoint:

";

(++myPoint).show();

cout<<"ShowmyPoint--:

";

(myPoint--).show();

cout<<"Show--myPoint:

";

(--myPoint).show();

return0;

}

结果:

提高题一

实验任务:

设计一个字符串类MyString,具有构造函数、析构函数、拷贝构造函数,重载运算符+,尽可能地完善它,使之能满足各种需要。

代码:

#include 

#include 

classperson 

{

public:

 

person(char*pn); 

person(person&p); 

~person(); 

private:

 

char*pname; 

};

person:

:

person(char*pn) 

{

cout<<"构造函数:

"<

pname=newchar[strlen(pn)+1]; 

if(pname!

=0) 

strcpy(pname,pn); 

}

person:

:

person(person&p) 

{

cout<<"拷贝"<

pname=newchar[strlen(p.pname)+1]; 

if(pname!

=0) 

strcpy(pname,p.pname); 

}

person:

:

~person() 

{

cout<<"析构函数:

"<

pname[0]='\0'; 

deletepname; 

}

voidmain() 

{

personp1("jack"); 

personp2(p1);

}

结果:

提高题二

实验任务:

声明一个Shape抽象类,再此基础上派生出Rectangle和Circle类,二者都有GetArea()函数计算对象的面积,GetPerim()函数计算对象的周长。

代码:

#include

usingnamespacestd;

#definen3.14

classShape

{

public:

virtualvoidGetArea()=0;

virtualvoidGetPerim()=0;

};

classRectangle:

publicShape

{

public:

Rectangle(inta,intb){this->a=a;this->b=b;}

voidGetArea();

voidGetPerim();

private:

inta,b;

};

voidRectangle:

:

GetArea()

{

cout<<"矩形面积为"<

}

voidRectangle:

:

GetPerim()

{

cout<<"矩形周长为"<<(2*this->a)+(2*this->b)<

}

classCircle:

publicShape

{

public:

Circle(intc){this->c=c;}

voidGetArea();

voidGetPerim();

private:

intc;

};

voidCircle:

:

GetArea()

{

cout<<"圆形面积为"<

}

voidCircle:

:

GetPerim()

{

cout<<"圆形周长为"<<(2*n*this->c)<

}

voidfun(Shape*ptr)

{

ptr->GetArea();

}

voidfun1(Shape*ptr)

{

ptr->GetPerim();

}

voidmain()

{

Rectangleobj(3,4);

Circleobj1

(2);

fun(&obj);

fun1(&obj);

fun(&obj1);

fun1(&obj1);

}

结果:

选做题一

实验任务:

编写一个程序,计算正方体、球体和圆柱体的表面积和体积。

(要求:

抽象出一个公共基类作为虚基类,运用虚函数的知识设计程序)

代码:

#include

usingnamespacestd;

#definen3.14

classShuju

{

public:

virtualvoidbiaomianji()=0;

virtualvoidtiji()=0;

};

classzhengfangti:

publicShuju

{

public:

zhengfangti(inta){this->a=a;}

voidbiaomianji();

voidtiji();

private:

inta;

};

voidzhengfangti:

:

biaomianji()

{

cout<<"正方形的表面积为"<<6*a*a<

}

voidzhengfangti:

:

tiji()

{

cout<<"正方形的体积为"<

}

classqiuti:

publicShuju

{

public:

qiuti(intb){this->b=b;}

voidbiaomianji();

voidtiji();

private:

intb;

};

voidqiuti:

:

biaomianji()

{

cout<<"球体的表面积为"<<4*n*b*b<

}

voidqiuti:

:

tiji()

{

cout<<"球体的体积为"<<(4.0/3.0)*n*b*b*b<

}

classyuanzhuti:

publicShuju

{

public:

yuanzhuti(intr,intd){this->r=r;this->d=d;}

voidbiaomianji();

voidtiji();

private:

intr,d;

};

voidyuanzhuti:

:

biaomianji()

{

cout<<"圆柱体的表面积为"<<2*n*r*r+2*n*r*d<

}

voidyuanzhuti:

:

tiji()

{

cout<<"圆柱体的体积为"<

}

voidfun(Shuju*ptr)

{

ptr->biaomianji();

}

voidfun1(Shuju*ptr)

{

ptr->tiji();

}

voidmain()

{

zhengfangtizheng

(2);

qiutiqiu(3);

yuanzhutiyuanzhu(4,5);

fun(&zheng);

fun1(&zheng);

fun(&qiu);

fun1(&qiu);

fun(&yuanzhu);

fun1(&yuanzhu);

}

结果:

选做题二

实验任务:

编写一个分数类fraction,其分子、分母为整数,通过重载运算符+、-、*、/,实现该类数据之间的四则运算,要求计算结果为最简分数,如

(7/3)*(9/14)=(3/2)

4*(7/8)=(7/2)

代码:

#include

usingnamespacestd;

classfraction{

public:

fraction(intn=1,intd=1){

num=n;

den=d;

divideZero();

};

fraction:

:

divideZero()

{

if(0==den)

{

cout<<"输入非法,分母为零!

"<

returntrue;

}

returnfalse;

}

fraction&operator=(constfraction&other){

num=other.num;

den=other.den;

return*this;

}

fractionoperator+(constfraction&other){

fractionres;

res.num=num*other.den+den*other.num;

res.den=den*other.den;

returnres;

}

fractionoperator-(constfraction&other){

fractionres;

res.num=num*other.den-den*other.num;

res.den=den*other.den;

returnres;

}

fractionoperator*(constfraction&other){

returnfraction(num*other.num,den*other.den);

}

fractionoperator/(constfraction&other){

returnfraction(num*other.den,den*other.num);

}

voiddisplay()

{

cout<

}

private:

intnum,den;

};

voidmain()

{

fractiona(2,5);

fractionb(a);

fractionc;

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