当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

上海交通大学:《C++程序设计》第八章 类与对象(1/2)(王群慧)

资源类别:文库,文档格式:PPT,文档页数:17,文件大小:90.5KB,团购合买
一、数据与功能的分离 二、需要正确的信息传递
点击下载完整版文档(PPT)

程序设计基础(C++) 面向过程与面向对象

1 程序设计基础(C++) 面向过程与面向对象

字符串处理(面向过程) int maino char s1[20]=hello" s2[]="world"i cout0)//字符串比较 strcat(s1,s2)P/字符串相加 cout≤≤s1<≤endI return o

2 字符串处理(面向过程) int main() { char s1[20]="hello", s2[]="world"; cout0) //字符串比较 ... strcat(s1, s2); //字符串相加 cout<<s1<<endl; return 0; }

字符串处理(面向对象) int maino string si( hello"), s2( world )i couts2)//字符串比较 s1+=s2F//字符串相加 cout <<s1<<enda return o }

3 字符串处理(面向对象) int main() { string s1("hello"), s2("world"); couts2) //字符串比较 .... s1+=s2; //字符串相加 cout<<s1<<endl; return 0; }

复数及其运算(面向过程) 定义复数类型 struct Complext double imagi double real Ln」

4 复数及其运算(面向过程) 定义复数类型 struct Complex{ double imag; double real; }

复数及其运算(面向过程) /复数加运算 Complex addcomplex (Complex &c1, Complex &c2) Complex Ci c real =cl, real +c2, real; Cimag=climag+c2 imagi return Ci

5 复数及其运算(面向过程) //复数加运算 Complex addComplex(Complex &c1, Complex &c2) { Complex c; c.real =c1.real +c2.real; c.imag=c1.imag+c2.imag; return c; }

复数及其运算(面向过程) /复数输出 void outcomplex( const Complex&c//向量表示 cout<<(<<Creak<<,"<<.imag<< )\n

6 复数及其运算(面向过程) //复数输出 void outComplex(const Complex &c)//向量表示 { cout<<"("<<c.real<<", "<<c.imag<<")\n"; }

复数及其运算(面向过程) int maino Complex o1={11,12},c2={21,22}c3; cout<< c1: outComplex(cli cout<< c2=ioutComplex(c2) C3=add complex(cl, c2)i cout<<c1+c2=i outComplex (c3); C3=sub Complex(c1 C2); cout<< c1-c2=i outComplex (c3);

复数及其运算(面向过程) int main() { Complex c1={1.1, 1.2}, c2={2.1, 2.2}, c3; cout<<"c1="; outComplex(c1); cout<<"c2="; outComplex(c2); c3=addComplex(c1, c2); cout<<"c1+c2="; outComplex(c3); c3=subComplex(c1, c2); cout<<"c1-c2="; outComplex(c3); 。。。 }

复数及其运算(面向对象) int maino Complex c111,12),c2(2122),c3; cout≤<"c1="<<c1<≤end cout<< c2="<<c2<<endl c3=c1+c2 cout < c1+c2="<<c3<<endl C3=c1-C2I cout<<"c1-c2="<<c3≤≤end

复数及其运算(面向对象) int main() { Complex c1(1.1, 1.2), c2(2.1, 2.2), c3; cout<<"c1="<<c1<<endl; cout<<"c2="<<c2<<endl; c3=c1+c2; cout<<"c1+c2="<<c3<<endl; c3=c1-c2; cout<<"c1-c2="<<c3<<endl; 。。。 }

时钟及其功能(面向过程) /时钟类型 struct clock int Hour, minute, second Si

9 时钟及其功能(面向过程) //时钟类型 struct Clock { int Hour, Minute, Second; };

时钟及其功能(面向过程) void showTime( const clock&c//显示时间 cout<<c hour<<:<<c Minute<< <c seco nd<<end Clock setTime(int h,intm,ints)//设置时间 Clock Ci C Hour=h; c Minute=m; c second=si return Cr }

10 时钟及其功能(面向过程) void showTime(const Clock &c)//显示时间 { cout<<c.Hour<<":"<<c.Minute<<":"<<c.Seco nd<<endl; } Clock setTime(int h, int m, int s)//设置时间 { Clock c; c.Hour=h; c.Minute=m; c.Second=s; return c; }

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共17页,试读已结束,阅读完整版请下载
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有