C++
This presentation is the property of its rightful owner.
Sponsored Links
1 / 27

C++ 面向对象程序设计 PowerPoint PPT Presentation


  • 102 Views
  • Uploaded on
  • Presentation posted in: General

C++ 面向对象程序设计. 第 十 讲. 构造函数 -- 特殊公用成员函数. 作用 : 创造类对象时自动调用构造函数给类对象赋初值 特征 : 1 )函数名:与类名相同 2 )无返回类型 无返回值 3 )公用成员 4 )用户可以自己定义,但不能调用 默认构造函数 : 如果程序中未定义出,则系统自动产生出一个缺省形式的构造函数. 构造函数举例. class watch { public: watch(int initH,int initM,int initS); // 构造函数

Download Presentation

C++ 面向对象程序设计

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


C

C++


C

--

:

:

1

2

3

4

:


C

class watch

{ public:

watch(int initH,int initM,int initS); //

void SetTime(int NewH,int NewM, int NewS); void ShowTime();

private:

int Hour, Minute, Second;

};


C

//

watch::watch(int initH,int initM,int initS)

{ Hour = initH;

Minute= initM;

Second = initS;

}


C

//

void main(void)

{ watch myWatch(05,06,07);//

watch watch01(03,04,05);

myWatch.SetTime(); //

myWatch.ShowTime(); cout<<endl;

myWatch.SetTime(8,30,30);

myWatch.ShowTime(); cout<<endl;

watch01.ShowTime(); cout<<endl;

}


C

  • ;

  • watch::watch()

    {

    }


C

class watch

{ public:

watch(int initH=0,int initM=0,int initS=0);

//

void SetTime(int NewH, int NewM, int NewS);

void ShowTime();

private:

int Hour, Minute, Second;

} ;


C

void main(void)

{

watch myWatch;//

watch watch01(8,7); //

watch watch01(03,04,05);

}


C

class

{ public :

//

&//

...

}

:: &//

{ }


C

class Location

{

public:

Location(int xx=0,int yy=0){X=xx; Y=yy;}

Location(Location & p);

int GetX() {return X;}

int GetY() {return Y;}

private:

int X,Y;

};


C

Location::Location (Location &p)

{

X=2*p.X;

Y=2*p.Y;

cout<<""<<endl;

}


C

int main(void)

{ Location C //

Location A(1,2);

Location B(A); //

cout<<B.GetX()<<<< B.GetY() <<endl;

return 0;

}


C

void f(Location p)

{ cout<<p.GetX()<<endl; }

int main()

{ Location A(3,4);

f(A); //

return 0;

}


C

Apf

6


C

Location g()

{ Location A(1,2);

return A; //

}

int main()

{ Location B;

B=g();

return 0;

}


C

  • &

  • {

  • }//


C

  • Circle::Circle(float r)

    {radius=r}

  • Circle::Circle(floatr):radius(r){}


C

  • class A

    {

    int i;

    float f;

    public:

    A(int X,float Y) :i(X),f(Y){};

    };

  • class A

    {

    int i;

    float f;

    public:

    A(int X,float Y)

    {i=X; f=Y;};

    };


C

  • :

    1

    2

    3

    4

  • ~{ }


C

#include "iostream.h"

class box

{ public:

box(int l , int w , int h) ;

~box( ) {cout<<"!"<<endl;}

double volume();

double surface_area();

private:

int length,width,height;

} ;

box::box(int l , int w , int h)

{ length=l; width=w; height=h; }

double box::volume( )

{ return length*width*height; }

double box::surface_area( )

{ return 2*length*width+4*length*height; }


C

void main( )

{ box ob(8,6,3);

cout<<

<<ob.volume()<<endl;

cout<<"

<<ob.surface_area()<<endl;

}


C

2

#include "iostream.h"

#include "string.h"

class string

{ public:

string(char s[ ]) { strcpy(str,s);

cout<<"! <<endl; }

~string( ) {cout<<"!"<<endl;};

void print( ) { cout<<str<<endl; }

operator const char *( );

private:

char str[20];

};

string::operator const char *( )

{ return str; }


C

void main( )

{ char *s1,*s2;

cout<<"Please input two string:"<<endl;

s1=new char[20];

s2=new char[20];

cin>>s1>>s2;

string o1(s1),o2(s2);

o1.print( );

o2.print( );

if(strcmp(o1,o2)==0)

cout<<o1<<" is equal to "<<o2<<endl;

else

cout<<o1<<" is not equal to "<<o2<<endl;

}


C

12

  • :static

    class file {

    //

    public:

    static int filecount ;

    //

    }


C

class file {

//

public:

static int getfilecount()

{return filecount;}

//

}


C

int file:filecount=0;


C

  • void main()

    {

    file file1;

    cout<<file::filecount ;

    //

    }


  • Login