C++实践环节代码.pdf

  1. 1、本文档共15页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
C实践环节代码

实践一:VC 集成环境:建Cpp1 动态分配内存: /*动态分配输出*/ #include iostream using namespace std; void main() { double *p; p=new double[3]; for(int i=0;i3;i++) cin*(p+i); for(i=0;i3;i++) cout*(p+i) ; delete p; } 实践二:使用函数模板: /*用函数模板实现3 个数值中按最小值到最大值排序*/ #include iostream using namespace std; template class T void max(T m1,T m2,T m3){ T temp; temp=m1; if (m1m2){ temp=m1;m1=m2;m2=temp; } if (m1m3){ temp=m1;m1=m3;m3=temp; } if (m2m3){ temp=m2;m2=m3;m3=temp; } coutm1,m2,m3; } void main(){ max(abf,abz,aba); coutendl; max(18,9,24); coutendl; max(2.01,2.38,2.33); coutendl; } 实践三:使用多文件编程: /*Point 使用(包含设计的)多文件编程示例*/ 头文件:CPP10.H 1 #if ! defined(CPP10_H) #define CPP10_H #includeiostream #includecmath using namespace std; class Point { double X,Y; public: Point(double=0,double=0); Point(Point); void Display() {coutX,Yendl;} double Distance(Point); double Getx() {return X;} double Gety() {return Y;} }; struct Cow { int Color; int Width; }; class Line { Point a,b; Cow cw; public: Line(Point,Point,Cow); void Display(); Line(Line); double Distance(); double Area(); }; #endif 实现类:CPP10.CPP #include cpp10.h Point::Point(double a,double b):X(a),Y(b) {} Point::Point(Pointa) 2 {X=a.X;Y=a.Y;} double Point::Distance(Pointa) { return sqrt((X-a.X)*(X-a.X)+(Y-a.Y)*(Y-a.Y)); } Line::Line(Pointa1,Pointa2,Cowa3):a(a1),b(a2),cw(a3) {} Line::Line(Lines):a(s.a),b(s.b),cw(s.cw) {} void Line::Display() { a.Display(); b.Display(); coutColor=cw.Color,Width=cw.Widthendl; } double Line::Distance() { double x=a

文档评论(0)

yan698698 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档