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

1、建立结构体

 #include <iostream.h>
struct Point
{ int x;
int y;
}; void main ()
{
Point pt;
pt.x=;
pt.y=;
//cout << pt.x << endl << pt.y << endl; }
 

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

 #include <iostream.h>
struct Point
{
public:
int x;
int y;
void output ()
{
cout << x << endl << y << endl;
}
}; void main ()
{
Point pt;
pt.x=;
pt.y=;
//cout << pt.x << endl << pt.y << endl;
pt.output(); }

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

sttuct在c++中同用

 #include <iostream.h>
//struct Point
class Point
{
public:
int x;
int y;
void output ()
{
cout << x << endl << y << endl;
}
}; void main ()
{
Point pt;
pt.x=;
pt.y=;
//cout << pt.x << endl << pt.y << endl;
pt.output(); }

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

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

做一个方法--开车

过程:参数传递进来

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

收音机:打开-听

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

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

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

未初始化

 #include <iostream.h>
class Point
{
public:
int x;
int y;
void output ()
{
cout << x << endl << y << endl;
}
}; void main ()
{
Point pt;
//pt.x=5;
//pt.y=5;
//cout << pt.x << endl << pt.y << endl;
pt.output(); }

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

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

 #include <iostream.h>
class Point
{
public:
int x;
int y;
void init()
{
x=;
y=;
}
void output ()
{
cout << x << endl << y << endl;
}
}; void main ()
{
Point pt;
//pt.x=5;
//pt.y=5;
//cout << pt.x << endl << pt.y << endl;
pt.init();
pt.output(); }

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

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

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

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

 #include <iostream.h>
class Point
{
public:
int x;
int y;
Point()
{
x=;
y=;
}
void output ()
{
cout << x << endl << y << endl;
}
}; void main ()
{
Point pt;
//pt.x=5;
//pt.y=5;
//cout << pt.x << endl << pt.y << endl;
//pt.init();
pt.output(); }

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

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

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

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

  4)释放堆内存

 #include <iostream.h>
class Point
{
public:
int x;
int y;
Point()
{
x=;
y=;
}
~Point()
{ }
void output ()
{
cout << x << endl << y << endl;
}
}; void main ()
{
Point pt;
//pt.x=5;
//pt.y=5;
//cout << pt.x << endl << pt.y << endl;
//pt.init();
pt.output(); }

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

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

  

 #include <iostream.h>
class Point
{
public:
int x;
int y;
Point()
{
x=;
y=;
}
Point(int a, int b)
{
x=a;
y=b;
}
~Point()
{ }
void output ()
{
cout << x << endl << y << endl;
}
}; void main ()
{
Point pt(,);
//pt.x=5;
//pt.y=5;
//cout << pt.x << endl << pt.y << endl;
//pt.init();
pt.output(); }

  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   原因是   变量 不可以见性 只能改变形参

 #include <iostream.h>
class Point
{
public:
int x;
int y;
Point()
{
x=;
y=;
}
Point(int a, int b)
{
x=a;
y=b;
}
~Point()
{ }
void output ()
{
cout << x << endl << y << endl;
}
void output (int x,int y)
{
x=x;
y=y;
}
}; void main ()
{
Point pt(,);
pt.output(,);
pt.output(); }

解决方法一、this->指针
  

 #include <iostream.h>
class Point
{
public:
int x;
int y;
Point()
{
x=;
y=;
}
Point(int a, int b)
{
x=a;
y=b;
}
~Point()
{ }
void output ()
{
cout << x << endl << y << endl;
}
void output (int x,int y)
{
this->x=x;
this->y=y;
}
}; void main ()
{
Point pt(,);
pt.output(,);
pt.output(); }

解决方法二、

 #include <iostream.h>
class Point
{
public:
int x;
int y;
Point()
{
x=;
y=;
}
Point(int a, int b)
{
x=a;
y=b;
}
~Point()
{ }
void output ()
{
cout << x << endl << y << endl;
}
void output (int c,int d)
{
x=c;
y=d;
}
/* void output (int x,int y)
{
this->x=x;
this->y=y;
}*/
}; void main ()
{
Point pt(,);
pt.output(,);
pt.output(); }

小技巧:
    

最新文章

  1. Git使用详细教程(二)
  2. [Unreal]学习笔记001
  3. Bzoj1208 [HNOI2004]宠物收养所
  4. mac里git项目删除.DS_Store文件
  5. UVa 133,发放救济金
  6. Heilmeier&#39;s criteria
  7. cloudstack4.4新增功能前瞻
  8. win 10 和 CentOS 7 双系统安装
  9. 万众瞩目之下,ANGULAR 2终于正式发布啦!
  10. Go中局部全局变量的区分
  11. maven私库nexus2.11.4迁移升级到nexus3.12.0
  12. Python字符串练习
  13. 使用mysqldump以分隔文本格式转储数据
  14. 隐马尔可夫模型HMM(二)概率计算问题
  15. Python -- tabulate 模块,
  16. POJ 2259 - Team Queue - [队列的邻接表]
  17. 02: docker高级篇
  18. 深入margin
  19. C/C++ ASCII码表
  20. Leetcode 92

热门文章

  1. bootstrap modal动态加载内容
  2. HTML5-新API选择器
  3. 【MySQL】binlog_format以及binlog事务记录分析
  4. DEDECMS如何修改数据库密码以及忘记了后台密码怎么办
  5. Oracle笔记 二、常用dba命令行
  6. JS回车事件
  7. Android IOS WebRTC 音视频开发总结(六一)-- 大数据解密国内实时通讯行业开发现状
  8. ASP.NET数据绑定
  9. 基于Vivado HLS在zedboard中的Sobel滤波算法实现
  10. %SELECTALL