This presentation is the property of its rightful owner.
Sponsored Links
1 / 83

程序设计实习 第二讲 类和 对象 ( 续) PowerPoint PPT Presentation


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

程序设计实习 第二讲 类和 对象 ( 续). 内容提要. 构造 函数 , 析 构 函数 , 各种 构造函数和析构函数的调用 时机 静态成员 常量对象和常量方法 成员对象和封闭类 const 成员 和引用成员 友元 this 指针. 构造 函数 ( constructor ) 成员 函数的一 种。 - 名字 与类名 相同,可以 有 参数,不能 有返回值 (void 也 不行 ) 。 - 作用 是对对象进行初始化 ,如给成员变量赋初值 。 - 如果 定义类时没写构造函数,则编译器生成一个缺省的无参数的构造函数。缺省构造函数无参数,什么也不做。.

Download Presentation

程序设计实习 第二讲 类和 对象 ( 续)

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


2636262

(


2636262

  • ,,

  • const

  • this


2636262

  • constructor

    • -(void)

    • -

    • -


2636262


2636262

class Complex {

private :

double real,imag;

public:

void Set( double r,doublei);

}; //

Complex c1; //

Complex * pc = new Complex; //


2636262

class Complex {

private :

double real,imag;

public:

Complex( double r, double i = 0);

};

Complex::Complex( double r,doublei)

{

real = r; imag = i;

}

Complex c1; // error,

Complex * pc = new Complex; // error,

Complex c1( 2); // OK

Complex c1(2,4), c2(3,5);

Complex * pc = new Complex(3,4);


2636262

  • .

  • class Complex {

  • private :

  • double real,imag;

  • public:

  • void Set( double r,doublei);

  • Complex( double r,doublei );

  • Complex (double r);

  • Complex ( Complex c1, Complex c2);

  • };

  • Complex::Complex( double r,doublei)

  • {

  • real = r; imag = i;

  • }


2636262

Complex::Complex( double r)

{

real = r; imag = 0;

}

Complex::Complex ( Complex c1, Complex c2);

{

real = c1.real+c2.real;

imag = c1.imag+c2.imag;

}

Complex c1( 3) , c2 (1,0), c3( c1,c2);

// c1 = { 3,0}, c2 = {1,0}, c3 = { 4, 0};


2636262

  • publicprivate.

  • class CSample{

  • private:

  • CSample() {

  • }

  • };

  • main(){

  • CSampleObj; //err. private

  • }


2636262

class CSample {

int x;

public:

CSample() {

cout<< "Constructor 1 Called" << endl;

}

CSample(int n ) {

x = n;

cout<< "Constructor 2 Called" << endl;

}

};


2636262

void main(){

CSamplearray1[2];

cout << "step1"<<endl;

CSamplearray2[2] = {4,5};

cout << "step2"<<endl;

CSamplearray3[2] = {3};

cout << "step3"<<endl;

CSample * array4 = new CSample[2];

//delete,

}


2636262

Constructor 1 Called

Constructor 1 Called

step1

Constructor 2 Called

Constructor 2 Called

step2

Constructor 2 Called

Constructor 1 Called

step3

Constructor 1 Called

Constructor 1 Called


2636262

class Test {

public:

Test( int n) { } //(1)

Test( int n, int m) { } //(2)

Test() { } //(3)

};

Test array1[3] = { 1, Test(1,2) };

// (1),(2),(3)

Test array2[3] = { Test(2,3), Test(1,2) ,1};

// (2),(2),(1)

Test * pArray[3] = { new Test( 4), new Test(1,2) };

//(1),(2)


2636262

  • (

  • X::X( X& ),

  • class Complex {

  • private :

  • double real,imag;

  • };

  • Complex c1; //

  • Complex c2(c1);//

  • // c2c1


2636262

  • class Complex {

  • public :

  • double real,imag;

  • Complex(){}

  • Complex( Complex & c ) {real = c.real;

  • imag = c.imag;

  • cout << Copy Constructor called;

  • }

  • };

  • Complex c1;

  • Complex c2(c1);//

  • // Copy Constructor called


2636262

  • X::X( X )

  • class CSample {

  • CSample( CSample c ) {

  • } //

  • };


2636262

  • class C1{

  • public:

  • C1() {

  • }

  • };

  • C1c1,c2(c1);


2636262

  • a.

  • Complex c2(c1);

  • Complex c2 = c1;

  • b. A A

  • void f( A a ) {

  • a.x = 1;

  • };

  • A aObj;

  • f ( aObj) ; // A

  • // 


2636262

c. AA:

A f( ) {

A a;

return a; // A,

//A(a);

}

int main( ) {

A b;

b = f();

return 0;

}


2636262

class CMyclass {

public:

int n;

CMyclass() {};

CMyclass( CMyclass & c) { n = 2 * c.n ; }

};

int main() {

CMyclass c1,c2;

c1.n = 5; c2 = c1; CMyclass c3(c1);

cout <<"c2.n=" << c2.n << ",";

cout <<"c3.n=" << c3.n << endl;

return 0;

}

c2.n=5,c3.n=10


2636262

  • void fun(CMyclassobj_ ) {

  • cout << "fun" << endl;

  • }

  • CMyclass &

  • const

  • void fun(const CMyclass & obj) {

  • // obj

  • }


2636262

  • destructors)

  • .

    • ~


2636262

class CString{

private :

char * p;

public:

CString() {

p = new char[10];

}

~ CString() ;

};

CString::~CString()

{

delete [] p;

}


2636262

  • class Ctest {

  • public:

  • ~Ctest() { cout<< "destructor called" << endl; }

  • };

  • int main () {

  • Ctest array[2];

  • cout << "End Main" << endl;

  • return 0;

  • }

  • End Main

  • destructor called

  • destructor called


2636262

  • delete

  • delete

  • Ctest * pTest;

  • pTest = new Ctest; //

  • delete pTest; //

  • ------------------------------------------------------------------

  • pTest= new Ctest[3]; //3

  • delete [] pTest; //3

  • newdelete []

  • delete()


2636262

class CMyclass {

public:

~CMyclass() { cout << "destructor" << endl; }

};

CMyclassobj;

CMyclass fun(CMyclasssobj ) { ////

return sobj; //

}

int main(){

obj = fun(obj); //

return 0; //

}

destructor

destructor

destructor


2636262


2636262

// chunk

chunk ck1,ck2; //main

void MyFunction() {

static chunk ck; //

chunk tmpCk; //tmpCk

....// tmpCk

}

int main () {

chunk tmpCk2; chunk * pCk;

MyFunction ();

pCk = new chunk; //

....

delete pCk ; //

} //main tmpCk2

// ck ,ck1, ck2


2636262

class Demo {

int id;

public:

Demo(inti)

{

id = i;

printf( "id=%d, Construct\n", id);

}

~Demo()

{

printf( "id=%d, Destruct\n", id);

}

};


2636262

id=1,Construct

id=4,Construct

main

id=5,Construct

id=5,Destruct

id=2,Construct

id=3,Construct

fun

id=3,Destruct

endmain

id=4,Destruct

id=2,Destruct

id=1,Destruct

Demo d1(1);

void fun(){

static Demo d2(2);

Demo d3(3);

printf( "fun \n");

}

int main (){

Demo d4(4);

printf( "main \n");

{ Demo d5(5); }

fun();

printf( "endmain \n");

return 0;

}


2636262

#include <iostream>

using namespace std;

class CMyclass {

public:

CMyclass() {};

CMyclass( CMyclass & c)

{

cout << "copy constructor" << endl;

}

~CMyclass() { cout << "destructor" << endl; }

};


2636262

void fun(CMyclassobj_ )

{

cout << "fun" << endl;

}

CMyclass c;

CMyclass Test( )

{

cout << "test" << endl;

return c;

}

intmain(){

CMyclassc1;

fun( c1);

Test();

return 0;

}

copy constructor

fun

destructor //

test

copy constructor

destructor //

destructor //

destructor //


2636262

  • static

  • public

class CRectangle

{

private:

int w, h;

staticintnTotalArea;

staticintnTotalNumber;

public:

CRectangle(intw_,int h_);

~CRectangle();

static void PrintTotal();

};

Crectangle r;


2636262

  • 1

    ::: CRectangle::PrintTotal(); 2

    . r.PrintTotal();

    -> CRectangle * p = &r;

    p->PrintTotal();

    (3).CRectangle& ref = r;

    int n = ref.nTotalNumber;

    ::


2636262

  • sizeof

  • class CMyclass {

  • int n;

  • static int s;

  • };

  • sizeof( CMyclass ) 4


2636262


2636262

class CRectangle

{

private:

intw, h;

static intnTotalArea;

static intnTotalNumber;

public:

CRectangle(intw_,int h_);

~CRectangle();

static void PrintTotal();

};


2636262

CRectangle::CRectangle(intw_,int h_)

{

w = w_;

h = h_;

nTotalNumber ++;

nTotalArea += w * h;

}

CRectangle::~CRectangle()

{

nTotalNumber --;

nTotalArea -= w * h;

}

void CRectangle::PrintTotal()

{

cout << nTotalNumber << "," << nTotalArea << endl;

}


2636262

intCRectangle::nTotalNumber = 0;

intCRectangle::nTotalArea = 0;

// //

int main()

{

CRectangle r1(3,3), r2(2,2);

//cout << CRectangle::nTotalNumber; // Wrong ,

CRectangle::PrintTotal();

r1.PrintTotal();

return 0;

}

2,13

2,13


2636262

  • void CRectangle::PrintTotal()

  • {

  • cout<< w << "," << nTotalNumber << "," <<

  • nTotalArea<< endl; //wrong

  • }

  • CRectangle r;

  • r.PrintTotal(); //

  • CRetangle::PrintTotal(); //w


2636262

class CRectangle

{

private:

int w, h;

static intnTotalArea;

static intnTotalNumber;

public:

CRectangle(intw_,int h_);

~CRectangle();

static void PrintTotal();

};


2636262

CRectangle::CRectangle(intw_,int h_)

{

w = w_;

h = h_;

nTotalNumber ++;

nTotalArea += w * h;

}

CRectangle::~CRectangle()

{

nTotalNumber --;

nTotalArea -= w * h;

}

void CRectangle::PrintTotal()

{

cout << nTotalNumber << "," << nTotalArea << endl;

}


2636262

intCRectangle::nTotalNumber = 0;

intCRectangle::nTotalArea = 0;

// //

int main()

{

CRectangle r1(3,3), r2(2,2);

//cout << CRectangle::nTotalNumber; // Wrong ,

CRectangle::PrintTotal();

r1.PrintTotal();

return 0;

}

2,13

2,13


2636262

  • CRectangleCRectangleCRectangle(CRectangleCRectangle)

  • nTotalNumber nTotalArea nTotalNumber nTotalArea

  • CRectangle

  • CRectangle :: CRectangle(CRectangle & r )

  • {

  • w = r.w; h = r.h;

  • nTotalNumber ++;

  • nTotalArea += w * h;

  • }


2636262

  • Complex c1,c2;

  • c1= c2; // c1c2


2636262

    • ()


2636262

  • Complex c1c2(2),c3(3,5);

  • Complex * pc1 = new Complex ;

    Complex * pc2 = new Complex(3,4)

  • :

    Test array1[3] = { 1, Test(1,2) };

    Test * pArray[3] = { new Test( 4), new Test(1,2) };


2636262

,,;

,;

,;

;

Complex c2(c1); Complex c2 = c1;

,;


2636262

;

;

,,;

,;

;

~(){ }


2636262

  • ;

    • delete

  • ;


2636262

    • : ;

    • main: main;

    • : ;

    • :;

    • :;


2636262

    • : ;

    • main: main;

    • : ;

    • :;

    • :;

    • .


2636262

  • bug C++

  • Dev C++VS 2008


2636262

  • #include <iostream>

  • using namespace std;

  • class A {

  • public:

  • int x;

  • A(int x_):x(x_) {

  • cout<< x << " constructor called" << endl;

  • }

  • ~A() { cout << x << " destructor called" << endl; }

  • };

  • A c(2);

  • int main( ) {

  • return 0;

  • }


2636262

  • vc6

  • 2 constructor called

  • DEVVS2008

  • 2 constructor called

  • 2 destructor called

  • vc6bug


2636262

class A {

public:

int x;

A(int x_):x(x_) {

cout<< x << " constructor called" << endl;

}

A(const A & a ) {//devconst

x = 2 + a.x;

cout << "copy called" << endl;

}

~A() { cout << x << " destructor called" << endl; }

};

A b(10);

A f( )

{

return b;

}

int main( ) {

A a(1);

a = f();

return 0;

}

10 constructor called

1 constructor called

copy called

12 destructor called

12 destructor called

10 destructor called


2636262

class A {

public:

int x;

A(int x_):x(x_) {

cout<< x << " constructor called" << endl;

}

A(const A & a ) {//devconst

x = 2 + a.x;

cout << "copy called" << endl;

}

~A() { cout << x << " destructor called" << endl; }

};

A f( )

{

A b(10);

return b;

}

int main( ) {

A a(1);

a = f();

return 0;

}

//devVC6VS2008

devb

1 constructor called

10 constructor called

10 destructor called

10 destructor called


2636262

  • ()


2636262

class Complex {

public: float real, imag;

Complex( double f ) {

cout << "FloatConstructor called" << endl;

real = f ; imag = 0;

}

Complex( inti = 0 ) {

cout << "IntConstructor called" << endl;

real = i; imag = 0;

}

~Complex() {

cout << "Destructor called" << endl;

}

};


2636262

Complex c1 ; // Complex(0) c1 = { 0, 0 };

Complex c2 = 2; // Complex(2) c2= {2, 0};

Complex c3 = 2.5; // Complex(2.5) c3 = {2.5, 0};

int main ()

{

cout << "c1=" << c1.real << "+" << c1.imag << "i"

<< endl;

cout<< "c2=" << c2.real << "+" << c2.imag << "i"

<< endl;

cout<< "in main" << endl;

c1 = 4;

Complex c4[2]={4, Complex(10)};

cout<< "end main" << endl;

return 0;

}


2636262

IntConstructor called

IntConstructor called

FloatConstructor called

c1=0+0i

c2=2+0i

in main

IntConstructor called

Destructor called //

IntConstructor called

IntConstructor called

end main

Destructor called

Destructor called

Destructor called

Destructor called

Destructor called


2636262

  • const

  • 1)

    • const intMAX_VAL = 23

    • const string SCHOOL_NAME = Peking University

    • 2)

    • intn,m;

    • const int * p = & n;

    • * p = 5; //

    • n = 4; //ok

    • p = &m; //ok

    • const int * p1; int * p2;

    • p1 = p2; //ok

    • p2 = p1; //error

    • p2 = (int * ) p1; //ok


2636262

  • const

    • void MyPrintf( const char * p )

    • {

    • strcpy( p,"this"); //

    • printf("%s",p); //ok

    • }

    • 3)

    • int n;

    • const int & r = n;

    • r = 5; //error

    • n = 4; //ok


2636262

  • const

  • class Sample {

  • private :

  • int value;

  • public:

  • void SetValue() {

  • }

  • };

  • const Sample Obj; //

  • Obj.SetValue (); // const (


2636262

  • const

  • class Sample {

  • private :

  • int value;

  • public:

  • void func() { };

  • void SetValue() const {

  • value = 0; // wrong

  • func(); //wrong

  • }

  • };

  • const Sample Obj;

  • Obj.SetValue (); //


2636262

  • const

  • class Sample {

  • private :

  • int value;

  • public:

  • void SetValue() const;

  • };

  • void Sample::SetValue() const { //const

  • //

  • cout << value;

  • }


2636262

  • const

    • :

    • const T &

    • void PrintfObj( const Sample & o )

    • {

    • o.func(); //error

    • o.SetValue(); //ok

    • Sample & r = (Sample &) o;//

    • r.func(); //ok

    • }


2636262

  • const,


2636262

class CTest {

private :

int n;

public:

CTest() { n = 1 ; }

intGetValue() const { return n ; }

intGetValue() { return 2 * n ; }

};

int main() {

const CTest objTest1;

CTest objTest2;

cout << objTest1.GetValue() << "," <<

objTest2.GetValue() ;

return 0;

}

1 2


2636262

  • enclosing)

  • class base1 {

  • private:

  • inti;

  • public :

  • base1 ( ) { i = 0; }

  • };

  • class base2 {

  • private:

  • inti;

  • public :

  • base2 (int n ) { i = n ; }

  • };


2636262

class Big {

private:

int n;

base1 b1; //

base2 b2, b3; //

public:

Big ( ) : b2(1),b3(2) {

}

};

Big b; //


2636262

class Big {

private:

int n;

base1b1;

base2b2,b3;

public:

Big (int n ) : b2(n),b3(2) {

}

};

Big b(5); //


2636262


2636262

class CIn1 {

public:

CIn1(int n) { cout << "CIn1contructor" << endl; }

~CIn1() { cout << "CIn1 destructor" << endl; }

};

class CIn2 {

public:

CIn2( int n ) { cout << "CIn2 contructor" <<

endl;}

~CIn2() { cout << "CIn2 destructor" << endl; }

};


2636262

class COut {

private:

CIn2objCIn2; CIn1objCIn1;

public:

COut( ):objCIn1(1), objCIn2(2)

{

cout << "COutcontructor" << endl;

}

~COut() {

cout << "COut destructor" << endl;

}

};

int main(){

COutobjCOut; return 0;

}


2636262

CIn2 constructor

CIn1 constructor

COut constructor

COut Destructor

CIn1 Destructor

CIn2 Destructor


2636262


2636262

  • const

  • const

  • class example {

  • private :

  • const int num;

  • int & ret;

  • int value;

  • public:

  • example( intn,int f) :num(n),ret(f),value(4)

  • {}

  • };


2636262

friends)

1)

class Thing {

private:

int data;

public :

friend void SetData( Thing & t, intnValue);

};

Thing t1;

void SetData( Thing & t, intnValue)

{

t.data = nValue;

t1.data = nValue;

}


2636262

  • ()

  • class B {

  • public:

  • void function();

  • };

  • class A {

  • friend void B::function();

  • };


2636262

2): ABAB

class rooster; // rooter

class Chicken {

private:

int data;

public :

friend class rooster;

};

class rooster {

private :

Chicken ck;

public:

void SetCk ( ) { ck.data = 1 ; }

};


2636262


2636262

  • this

  • this this

  • class Complex {

  • float real,imag;

  • public:

  • Complex * ReturnAddress ( ) {

  • return this;

  • }// c.ReturnAddress () & c

  • float ReturnReal() {

  • return this -> real;//return real

  • }

  • };

  • * this


  • Login