演示程序.docx
- 文档编号:29379085
- 上传时间:2023-07-22
- 格式:DOCX
- 页数:28
- 大小:40.12KB
演示程序.docx
《演示程序.docx》由会员分享,可在线阅读,更多相关《演示程序.docx(28页珍藏版)》请在冰豆网上搜索。
演示程序
第一章
面向过程与面向对象思想对比
#include
structcircus
{
doublera;
doublex;
doubley;
};
doublearea(circusc)
{
doubles;
s=3.14*c.ra*c.ra;
returns;
}
voidmain()
{
circusctemp;
printf("pleaseinputr:
\n");
scanf("%lf",&ctemp.ra);
doubles=0.0;
s=area(ctemp);
printf("area=%lf\n",s);
}
#include
classcircus
{
private:
doublera;
doublex;
doubley;
public:
doublearea()
{
doubles;
s=3.14*ra*ra;
returns;
}
voidsetr(doublerr)
{
ra=rr;
}
};
voidmain()
{
circusctemp;
//cin>>ctemp.r;
ctemp.setr
(2);
doubles=ctemp.area();
cout<
}
第三章
内联函数效率
#include
#include
#include
#include
inlineintadd()
{
intsum=0;
sum+=100;
returnsum;
}
voidmain()
{
SYSTEMTIMEsys;
GetLocalTime(&sys);
printf("%4d/%02d/%02d%02d:
%02d:
%02d.%03d星期%1d\n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);
for(inti=0;i<1000000000;i++)
add();
GetLocalTime(&sys);
printf("%4d/%02d/%02d%02d:
%02d:
%02d.%03d星期%1d\n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);
}
第四章
1
#include
#include
usingnamespacestd;
classclock
{
protected:
inth;
intm;
ints;
public:
voidsettime(inthh,intmm,intss);
voidshowtime();
};
inlinevoidclock:
:
settime(inthh,intmm,intss)
{
h=hh;
m=mm;
s=ss;
}
voidclock:
:
showtime()
{
cout< "< "< } voidmain() { clockc1,c2; cout< c1.settime(14,29,0); c1.showtime(); c2.settime(8,0,0); c2.showtime(); } 2组合类构造析构 #include #include #include usingnamespacestd; classpoint { private: intx; inty; voidsetx(intxx) { x=xx; } voidsety(intyy) { y=yy; } public: /*point() { x=0; y=0; }*/ point(intxx=0,intyy=0); point(constpoint&pt) { x=pt.x; y=pt.y; cout<<"copyconstructor..."< } voidsetxy(intxx,intyy) { setx(xx); sety(yy); } intgetx() { returnx; } intgety() { returny; } voidshowpoint() { cout<<"x="< } ~point() { cout<<"pointdes..."< } }; inlinepoint: : point(intxx,intyy) { x=xx; y=yy; cout<<"constructor..."< } classline { private: pointp1,p2; public: line(point&tp1,point&tp2): p1(tp1),p2(tp2) { //p1=tp1; //p2=tp2; cout<<"linecons..."< } line(line<emp) { p1=ltemp.p1; p2=ltemp.p2; cout<<"linecopycons..."< } doublegetlen() { returnsqrt(pow(p1.getx()-p2.getx(),2)+pow(p1.gety()-p2.gety(),2)); } ~line() { cout<<"linedes..."< } }; voidmain() { pointp1,p2; //pointp3=p1; p1.setxy(0,0); p2.setxy(1,1); linel12(p1,p2); cout< } 第六章 1 #include usingnamespacestd; intconstN=1; classdate { private: intyy,mm,dd; public: date(){} date(inty,intm,intd); intgety() { returnyy; } intgetm() { returnmm; } intgetd() { returndd; } voidset(inty,intm,intd) { yy=y; mm=m; dd=d; } }; date: : date(inty,intm,intd) { yy=y; mm=m; dd=d; } classstudent { private: intnumber; charname[10]; datebirthday; public: student() { cout<<"studentconstructor..."< } student(intn,charna[10],datebir): birthday(bir) { number=n; strcpy(name,na); cout<<"studentconstructor..."< } voidset(intn,char*na,datebir) { number=n; strcpy(name,na); birthday=bir; } voidprint() { cout<<"学号="< "< "< "< } ~student() { cout<<"studentdestructor..."< } }; voidmain() { students[N]; student*ps=&s[0]; inti; for(i=0;i { intn; charna[10]; inty,m,d; cout<<"请输入学号,姓名,年,月,日\n"; cin>>n>>na>>y>>m>>d; (ps+i)->set(n,na,date(y,m,d)); (*(ps+i)).print(); } } 2 #include usingnamespacestd; classdate{ private: intyy,mm,dd; public: date() { } date(inty,intm,intd) { yy=y; mm=m; dd=d; } date(date&rd); intgety() { returnyy; } intgetm() { returnmm; } intgetd() { returndd; } voidset(inty,intm,intd) { yy=y; mm=m; dd=d; } ~date() { } }; date: : date(date&rd) { yy=rd.yy; mm=rd.mm; dd=rd.dd; } classstudent { private: intnumber; charname[10]; datebirthday; public: student(intn,charna[10],datebir): number(n),birthday(bir) { strcpy(name,na); } student(intn,charna[10],inty,intm,intd): birthday(y,m,d) { number=n; strcpy(name,na); } voidset(intn,char*na,datebir) { number=n; strcpy(name,na); birthday=bir; } voidprint() { cout<<"number="< } }; voidmain() { students1(20140101,"zhang",1995,1,1); s1.print(); intn; charna[10]; inty,m,d; cin>>n>>na>>y>>m>>d; s1.set(n,na,date(y,m,d)); s1.print(); } 3 #include usingnamespacestd; intconstN=2; classdate{ private: intyy,mm,dd; public: date() { } date(intyy,intmm,intdd) { this->yy=yy; this->mm=mm; this->dd=dd; } date(date&rd); intgety() { returnyy; } intgetm() { returnmm; } intgetd() { returndd; } voidset(inty,intm,intd) { yy=y; mm=m; dd=d; } ~date() { } }; date: : date(date&rd) { yy=rd.yy; mm=rd.mm; dd=rd.dd; } classstudent { private: intnumber; charname[10]; datebirthday; public: student() { dated1; d1.set(1990,1,1); number=1; strcpy(name,"zhang"); birthday=d1; } student(intn,charna[10],datebir): number(n),birthday(bir) { strcpy(name,na); } student(intn,charna[10],inty,intm,intd): birthday(y,m,d) { number=n; strcpy(name,na); } voidset(intn,char*na,datebir) { number=n; strcpy(name,na); birthday=bir; } voidprint() { cout<<"number="< } studentgets() { return*this; } }; voidmain() { students1; (s1.gets()).print(); students2; (s2.gets()).print(); } 2多继承 #include usingnamespacestd; classbed { private: intweight; public: bed(intw) { weight=w; cout<<"bedconstructor..."< } bed(bed&b) { weight=b.weight; cout<<"bedcopyconstructor..."< } voidsleep() { cout<<"sleep..."< } intgetw() { returnweight; } voidsetw(intw) { weight=w; } ~bed() { cout<<"beddestructor..."< } }; classsofa { private: intweight; public: sofa(intw) { weight=w; cout<<"sofaconstructor..."< } sofa(sofa&s) { weight=s.weight; cout<<"sofacopyconstructor..."< } intgetw() { returnweight; } voidsetw(intw) { weight=w; } voidwatchtv() { cout<<"watchtv..."< } ~sofa() { cout<<"sofadestructor..."< } }; classsleepsofa: publicsofa,publicbed { public: sleepsofa(intw1,intw2): sofa(w2),bed(w1) { cout<<"sleepsofaconstructor..."< } sleepsofa(sleepsofa&s): bed(s.bed: : getw()),sofa(s.sofa: : getw()) { cout<<"sleepsofacopyconstructor..."< } voidfoldout() { cout<<"foldout..."< } ~sleepsofa() { cout<<"sleepsofadestructor..."< } }; voidmain() { sleepsofasf(200,300); sf.sleep(); sf.watchtv(); sf.foldout(); sf.bed: : setw(500); sf.sofa: : setw(1000); cout< : getw()< cout< : getw()< } 3继承和组合类: 构造顺序,基类,对象成员,派生类;析构顺序相反 #include #include usingnamespacestd; classdate { private: intyy,mm,dd; public: date(){ cout<<"dateconstructor...noparameters..."< date(date&d) { yy=d.yy; mm=d.mm; dd=d.dd; cout<<"datecopyconstructor...withparameters..."< } date(inty,intm,intd) { yy=y; mm=m; dd=d; cout<<"dateconstructor...withparameters..."< } ~date(){cout<<"datedestructor..."< }; classperson { private: stringname; stringid; public: person(stringna,stringid1) { name=na; id=id1; cout<<"personconstructor...withparameters..."< } ~person() { cout<<"persondestructor..."< } }; classstudent: publicperson { private: intnum; datebirthday; public: student(stringna,stringid1,intn,inty,intm,intd): person(na,id1),birthday(y,m,d) { num=n; cout<<"studentconstructor...withparameters..."< } ~student() { cout<<"studentdestructor..."< } }; voidmain() { studentst("zhang","1234567890123456",20141001,1995,5,1); } 3sleepsofa #include usingnamespacestd; classbed { private: intweight; public: bed(intw) { weight=w; cout<<"bedconstructor..."< } bed(bed&b) { weight=b.weight; cout<<"bedcopyconstructor..."< } intgetw() { returnweight; } voidsetw(intw) { weight=w; } voidsleep() { cout<<"sleep..."< } ~bed() { cout<<"beddestructor..."< } }; classsofa { private: intweight; public: sofa(intw) { weight=w; cout<<"sofaconstructor..."< } sofa(sofa&s) { weight=s.weight; cout<<
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 演示 程序