2.1孙鑫C++

0、vc++6.0 工程---win32控制台程序            文件---c++

1、建立结构体

 1 #include <iostream.h>
 2 struct Point
 3 {
 4 
 5     int x;
 6     int y;
 7 };
 8 
 9 void main ()
10 {
11 Point pt;
12 pt.x=5;
13 pt.y=5;
14 //cout << pt.x << endl << pt.y << endl;
15 
16 }
View Code
 

2、在结构体中,建立方法函数输出,主函数调用

 1 #include <iostream.h>
 2 struct Point
 3 {
 4 public:
 5     int x;
 6     int y;
 7     void output ()
 8     {
 9         cout << x << endl << y << endl;
10     }
11 };
12 
13 void main ()
14 {
15 Point pt;
16 pt.x=5;
17 pt.y=5;
18 //cout << pt.x << endl << pt.y << endl;
19 pt.output();
20 
21 }
View Code

3,struct 于 class 是一样的:  区别---struct默认都为public  class都为privit

sttuct在c++中同用

 1 #include <iostream.h>
 2 //struct Point
 3 class Point
 4 {
 5 public:
 6     int x;
 7     int y;
 8     void output ()
 9     {
10         cout << x << endl << y << endl;
11     }
12 };
13 
14 void main ()
15 {
16 Point pt;
17 pt.x=5;
18 pt.y=5;
19 //cout << pt.x << endl << pt.y << endl;
20 pt.output();
21 
22 }
View Code


4,类:有类似功能的整体,,,对像:整体中的个体

观念的转变:过程--对象

做一个方法--开车

    过程:参数传递进来

    对象:把汽车启动 设置为汽车的属性  ---只要调用汽车就可以开走了

收音机:打开-听

   过程:调节音量函数--收音机是参数,(先做方法-后对操作对象)

   对象:收音机类---里面对象--开---调节音量  (直接来操作类--里面包含)

5、初始化--不初始化 会输出随机数

未初始化

 1 #include <iostream.h>
 2 class Point
 3 {
 4 public:
 5     int x;
 6     int y;
 7     void output ()
 8     {
 9         cout << x << endl << y << endl;
10     }
11 };
12 
13 void main ()
14 {
15 Point pt;
16 //pt.x=5;
17 //pt.y=5;
18 //cout << pt.x << endl << pt.y << endl;
19 pt.output();
20 
21 }
View Code

初始化后----init是方法---point是对象  对象包含实现的方法

C过程:先方法 函数---传递参数 -对象 来实现

 1 #include <iostream.h>
 2 class Point
 3 {
 4 public:
 5     int x;
 6     int y;
 7     void init()
 8     {
 9         x=0;
10         y=0;
11     }
12     void output ()
13     {
14         cout << x << endl << y << endl;
15     }
16 };
17 
18 void main ()
19 {
20 Point pt;
21 //pt.x=5;
22 //pt.y=5;
23 //cout << pt.x << endl << pt.y << endl;
24 pt.init();
25 pt.output();
26 
27 }
View Code

6、构造函数--内存创建分配 ---帮助完成初始化(定义对象时,自动进行初始化)
    1)初始化

  2)创建对象本身--分配内存

       每个类必须有一个构造函数,若果没有就不能创建类----(如果没有构造函数C++编译器会  自动创建变量,但是不能够赋初值)

  3)如果定义了一个构造函数,编译器不再提供

 1 #include <iostream.h>
 2 class Point
 3 {
 4 public:
 5     int x;
 6     int y;
 7     Point()
 8     {
 9         x=0;
10         y=0;
11     }
12     void output ()
13     {
14         cout << x << endl << y << endl;
15     }
16 };
17 
18 void main ()
19 {
20 Point pt;
21 //pt.x=5;
22 //pt.y=5;
23 //cout << pt.x << endl << pt.y << endl;
24 //pt.init();
25 pt.output();
26 
27 }
View Code

7、析构函数----内存释放回收

  1)一个对象生命周期结束时,回收空间

  2)析构函数不能有返回值

  3)只能有一个析构该函数

  4)释放堆内存

 1 #include <iostream.h>
 2 class Point
 3 {
 4 public:
 5     int x;
 6     int y;
 7     Point()
 8     {
 9         x=0;
10         y=0;
11     }
12     ~Point()
13     {
14     
15     }
16     void output ()
17     {
18         cout << x << endl << y << endl;
19     }
20 };
21 
22 void main ()
23 {
24 Point pt;
25 //pt.x=5;
26 //pt.y=5;
27 //cout << pt.x << endl << pt.y << endl;
28 //pt.init();
29 pt.output();
30 
31 }
View Code

8、函数重载:两个构造函数--带参于不带参数同时存在
   更具不同参数 来选择 执行那个函数

    1)如   无参数输出为0    有参数输出为1

  

 1 #include <iostream.h>
 2 class Point
 3 {
 4 public:
 5     int x;
 6     int y;
 7     Point()
 8     {
 9         x=0;
10         y=0;
11     }
12     Point(int a, int b)
13     {
14         x=a;
15         y=b;
16     }
17     ~Point()
18     {
19     
20     }
21     void output ()
22     {
23         cout << x << endl << y << endl;
24     }
25 };
26 
27 void main ()
28 {
29 Point pt(3,3);
30 //pt.x=5;
31 //pt.y=5;
32 //cout << pt.x << endl << pt.y << endl;
33 //pt.init();
34 pt.output();
35 
36 }
View Code

  2)其他函数依然能构成函数的重载

条件:参数不同 或参数的个数不同

 第一种:不行

       void output();

     int   output();

第二种:不行

    void output(int a,int b=0);

    void output(int a);

8、隐含指针

  1)隐含指针---代表指向本对象本身  

  2)pt.output(5,5); 不接收到形参,还接收到了对象的地址,

  

下面输出的不是想要的 5 5 而是 3 3   原因是   变量 不可以见性 只能改变形参

 1 #include <iostream.h>
 2 class Point
 3 {
 4 public:
 5     int x;
 6     int y;
 7     Point()
 8     {
 9         x=0;
10         y=0;
11     }
12     Point(int a, int b)
13     {
14         x=a;
15         y=b;
16     }
17     ~Point()
18     {
19     
20     }
21     void output ()
22     {
23         cout << x << endl << y << endl;
24     }
25     void output (int x,int y)
26     {
27         x=x;
28         y=y;
29     }
30 };
31 
32 void main ()
33 {
34 Point pt(3,3);
35 pt.output(5,5);
36 pt.output();
37 
38 }
View Code

解决方法一、this->指针
  

 1 #include <iostream.h>
 2 class Point
 3 {
 4 public:
 5     int x;
 6     int y;
 7     Point()
 8     {
 9         x=0;
10         y=0;
11     }
12     Point(int a, int b)
13     {
14         x=a;
15         y=b;
16     }
17     ~Point()
18     {
19     
20     }
21     void output ()
22     {
23         cout << x << endl << y << endl;
24     }
25     void output (int x,int y)
26     {
27         this->x=x;
28         this->y=y;
29     }
30 };
31 
32 void main ()
33 {
34 Point pt(3,3);
35 pt.output(5,5);
36 pt.output();
37 
38 }
View Code

解决方法二、

 1 #include <iostream.h>
 2 class Point
 3 {
 4 public:
 5     int x;
 6     int y;
 7     Point()
 8     {
 9         x=0;
10         y=0;
11     }
12     Point(int a, int b)
13     {
14         x=a;
15         y=b;
16     }
17     ~Point()
18     {
19     
20     }
21     void output ()
22     {
23         cout << x << endl << y << endl;
24     }
25     void output (int c,int d)
26     {
27         x=c;
28         y=d;
29     }
30 /*    void output (int x,int y)
31     {
32         this->x=x;
33         this->y=y;
34     }*/
35 };
36 
37 void main ()
38 {
39 Point pt(3,3);
40 pt.output(5,5);
41 pt.output();
42 
43 }
View Code

小技巧:
    

原文地址:https://www.cnblogs.com/TFH-FPGA/p/3192249.html