西安石油大学大二(上)C++面向对象程序设计课件各章习题 下载本文

内容发布更新时间 : 2024/12/23 10:14:56星期一 下面是文章的全部内容请认真阅读。

cout<<\

}

};

class Circle { Point center; //圆心位置

int radius; //半径

public: Circle(int cx,int cy, int r):center(cx,cy),radius(r) { cout<<\

}

~Circle() {cout<<\

};

void main() {

Circle c(3,4,5); }

3)

#include #include class Point { int x,y; public: Point(int x1=0, int y1=0) :x(x1), y(y1) { cout<<\

}

~Point() { cout<<\

}

};

class Text {

char text[100]; //文字内容 public:

Text(char * str) { strcpy(text,str); cout<<\ }

~Text() {cout<<\

};

31

class CircleWithText : public Point,public Text { public: };

void main() { }

CircleWithText cm(3,4,\

CircleWithText(int cx,int cy, char *msg): Point(cx,cy),Text(msg) { }

~CircleWithText() {cout<<\

cout<<\

(4)

#include class Date { public:

void SetDate(int y,int m,int d){ Year=y; Month=m; Day=d; } void PrintDate(){ cout<

Date(int y,int m,int d){SetDate(y,m,d);} protected:

int Year,Month,Day; };

class Time { public:

void SetTime(int h,int m,int s){ Houre=h; Minutes=m; Seconds=s;} void PrintTime()

{ cout<

Time(int h,int m,int s){SetTime(h,m,s);} protected:

int Houre, Minutes, Seconds; };

class Date_Time: public Date, public Time { public:

32

Date_Time( ):Date(),Time(){};

Date_Time(int y,int mo,int d,int h,int mi,int s): };

void main( ) {

Date_Time dt_a, dt_b(2002,10,1,6,0,0); dt_a.PrintDate_Time(); dt_b.SetTime(23,59,59); dt_b.PrintDate_Time(); dt_a.SetDate(2002,12,31); dt_a.PrintDate_Time(); }

Date(y,mo,d), Time(h,mi,s){}

void PrintDate_Time(){PrintDate();PrintTime();}

(5)

#include class Date { public:

Date(int y=2001,int m=1,int d=1){Year=y; Month=m; Day=d;} void PrintDate(){ cout<

int Year,Month,Day; };

class Time { public:

Time(int h=5,int m=30,int s=0){Houre=h; Minutes=m; Seconds=s;} void PrintTime()

{ cout<

int Houre, Minutes, Seconds; };

class Date_Time: public Date, public Time { public:

Date_Time( ){};

Date_Time(int y,int mo,int d,int h=0,int mi=0,int s=0):

33

Date(y,mo,d), Time(h,mi,s){}

void PrintDate_Time(){PrintDate();PrintTime();}

};

void main( ) {

Date_Time a, b(2002,10,1,6,20,0), c(2003,3,8,6,7); a.PrintDate_Time(); b.PrintDate_Time(); c.PrintDate_Time(); }

6)

#include class Base { public:

Base (int i,int j){ x0=i; y0=j;} void Move(int x,int y){ x0+=x; y0+=y;}

void Show(){ cout<<\private: int x0,y0; };

class Derived: private Base { public:

Derived(int i,int j,int m,int n):Base(i,j){ x=m; y=n;} void Show (){cout<<\ void Move1(){Move(2,3);} void Show1(){Base::Show();} private: int x,y; };

void main( ) {

Base b(1,2); b.Show();

34

Derived d(3,4,10,15); d.Move1(); d.Show(); d.Show1(); }

7)

#include class Point {

public:

void InitP(float x0=0, float y0=0) {X=x0;Y=y0;} void Move(float xf, float yf) {X+=xf;Y+=yf;} float GetX() {return X;} float GetY() {return Y;} private: float X,Y; };

class Rectangle: public Point {

public:

void InitR(float x, float y, float w, float h) { InitP(x,y);W=w;H=h;

}

void ZoomR(float k){W*=k,H*=k;} float GetH() {return H;} float GetW() {return W;} private: float W,H; };

void main() {

Rectangle rect;

rect.InitR(10,20,30,40);

cout<

<

35