Object mode (2)---Object Lessons ---part 2

3.The difference between "struct" and "class"

 in language "C", struct will conbine the data:

ex:

typedef struct _Point3d

{

long _x;

long _y;

long _z;

int (*getx)();

//....

}Point3d;

So, why using a new keyword "class" in Cplusplus?

1.for C++, it need to be campatibility C.

2.class is defined for polymorphism,override,template.

3.C++ is for oop.

 

4.An object Distinction.

class zooAnimal{

//...

};

 

class Bear:public zooAnimal{

//....

protected:

void rotate();

virtual void dance();

int cell_book;

};

Bear b;

zooAnimal *pz = &b;

Bear *pb = &b;

zooAnimal zz = b;

pz->cell_book ?// the zooAnimal has not  this member!

pb->cell_book  //ok

zz.cell_book  //the zooAnimal has not this member!

pz->dance()? //polymorphism

From below we can get:

I.the difference with pointer/reference  & instance.

zooAnimal zz = b;

no mater what "b" is. It now will be sicked to be a "zooAnimal".

zooAnimal *pz = &b;

pointer/reference don't known the realy instance of b.

so, pz may be pointer an "Bear" or others. it depends the address what it point.

pz->dance() will showing the polymorphism, if need. 

this will calling: Bear::dance();

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2014-03-25 15:22  手机从业者  阅读(89)  评论(0)    收藏  举报