文档详情

c++5(C++程序的结构)讲解.ppt

发布:2017-04-16约2.47千字共57页下载文档
文本预览下载声明
*;*;*;*;*;*;*;*;*;*;*;*;*;*;*;*;void other(void) { static int a=2; static int b; // a,b为静态局部变量,具有全局寿命,局部可见。 //只第一次进入函数时被初始化。 int c=10; // C为局部变量,具有动态生存期, //每次进入函数时都初始化。 a=a+2; i=i+32; c=c+5; cout---OTHER---\n; cout i: i a: a b: b c: cendl; b=a; };运行结果: ---MAIN--- i: 1 a: 0 b: -10 c: 0 ---OTHER--- i: 33 a: 4 b: 0 c: 15 ---MAIN--- i: 33 a: 0 b: -10 c: 8 ---OTHER--- i: 75 a: 6 b: 4 c: 15;*;//时钟类成员函数实现 Clock::Clock() //构造函数 { Hour=0; Minute=0; Second=0; } void Clock::SetTime(int NewH, int NewM, int NewS) { Hour=NewH; Minute=NewM; Second=NewS; } void Clock::ShowTime() { coutHour:Minute:Secondendl; };Clock globClock; //声明对象globClock, //具有静态生存期,文件作用域 void main() //主函数 { coutFirst time output:endl; //引用具有文件作用域的对象: globClock.ShowTime(); //对象的成员函数具有类作用域 globClock.SetTime(8,30,30); Clock myClock(globClock); //声明具有块作用域的对象myClock coutSecond time output:endl; myClock.ShowTime(); //引用具有块作用域的对象 };程序的运行结果为: First time output: 0:0:0 Second time output: 8:30:30 ;*;*;*;*;*;Point::Point(Point p) { X=p.X; Y=p.Y; countP++; } int Point::countP=0; void main() { Point A(4,5); coutPoint A,A.GetX(),A.GetY(); A.GetC(); Point B(A); coutPoint B,B.GetX(),B.GetY(); B.GetC(); };*;*;*;Point::Point(Point p) { X=p.X; Y=p.Y; countP++; } int Point::countP=0; void main() //主函数实现 { Point A(4,5); //声明对象A coutPoint A,A.GetX(),A.GetY(); A.GetC(); //输出对象号,对象名引用 Point B(A); //声明对象B coutPoint B,B.GetX(),B.GetY(); Point::GetC(); //输出对象号,类名引用 };*;*;*;double Distance( Point a, Point b) { double dx=a.X-b.X; double dy=a.Y-b.Y; return sqrt(dx*dx+dy*dy); } int main() { Point p1(3.0, 5.0), p2(4.0, 6.0); double d=Distance(p1, p2); coutThe distance is dendl; return 0; };*;*;void B::Set(int i) { a.x=i; } void B::Display() { a.Display(); };*;*;*;*;*;*;void R::print() { coutR1:R2endl; } void R::print() const { coutR1;R2endl; } void main()
显示全部
相似文档