封装篇

1 类与对象

访问限定:public:

char name[20];

Int  age;

  protected:

char name[20];

Int  age;

    Private:

char name[20];

Int  age;

 

2 对象实例化:

栈实例化:

Class Tv{

Public:

Int type;

Void power();

Void chagevol();

}

Int main(){

  Tv  tv;//Tv(class )  tv(对象名)

}

堆实例化:

Class Tv{

Public:

Int type;

Void power();

}

Int main(){

Tv *p = new Tv();

//todo

Delete p;

}

访问对象成员:

栈:

Tv tv;

tv.type=0;

tv.chagevol();

堆:

Tv *p = new Tv();

P->chagevol();

P->type=0;

Delete p;

P=NULL;

对象数组:

一样

 

Char[]数组: 表示字符,麻烦

所以,用string;

 

注意:

#include<string>

Using namespace std;

使用:

String name = “liuchang”;

String hobby(“football”);

Cout <<name<<hobby<<endl;

 

初始化方式:

1 string s1;        s1 是空串

2 string s2(“ABC”);  s2 ,并初始化s2

3 string s3(s2);     s3初始化为s2的一个副本  

4 string s4(n,”c”);   s4为字符cn个副本  string s4(3,”n”);  s4=”nnn”;

 

 

重中之重:数据的封装:

好处:  对传入参数限制。

可以任意操作参数(不提供设置方法,只提供得到数值的方法)

 

posted @ 2017-05-07 16:26  freebirds  阅读(92)  评论(0)    收藏  举报