大概花了六天的时间整理的
整理的孙鑫老师的第二讲
这个寒假我从C++中的类是什么都不知道 一步一步走过来的
寒假50多天过的非常充实
以后还打算学习WIN32汇编 Windows驱动 如果真能坚持下来 每次我都会好好整理
C++的opp思想 类 继承 模板 封装 多态 到具体的诸如构造函数 析构函数 虚函数 纯虚函数 等等吧 很全面
编译环境 vc6.0
新建 win32 console application 选择空工程 插入 C++ source file 下面例子全部编译通过 自己也发出来 也经常来复习吧 孙鑫老师讲的很好 将C++的特性体现的淋漓尽致 后面如果还有经典的例子会继续补充
最后的@14的封装没整理好 暂且放一下
注意包含头文件
#include“iostream.h”
#include“stdio.h”
注意头文件包含时候用"" 与<>的区别
注意所有的标点符号输入均应该在英文下
代码阅读软件推荐用 notepad++
//project1
//@1定义结构体
#include<iostream.h>
struct Point //传说中的结构体的形式
{
int x;
int y;
};
/*在main函数内部,我们利用这个结构体定义了一个结构体的变量叫pt*/
void main()
{
Point pt;
pt.x=5;
pt.y=5;
cout<<pt.x<<endl<<pt.y<<endl; /*endl相当于 /n 即回车换行*/
};
//@2在@1基础上在结构体里增加一个函数由他来负责输出功能
#include<iostream.h>
struct Point
{
int x;
int y;
void output() //函数()后面是定义变量{}后面是定义变量的值以及变量是如何实现的 c++结构体中是可以有函数的我们可以将一些操作封装到结构体内部的成员函数当中使用的时候只需调用这些成员函数
{
cout<<x<<endl<<y<<endl;
};
};
/*在main函数内部,我们利用这个结构体定义了一个结构体的变量叫pt*/
void main()
{
Point pt;
pt.x=5;
pt.y=5;
//cout<<pt.x<<endl<<pt.y<<endl; /*endl相当于 /n 即回车换行*/
pt.output();
};
//@3将struct换成class
#include<iostream.h>
//struct Point
class Point//结构体是一种特殊的类 它是用struct所定义的一种默认(可修改)是public的类 public private protected 。class如果不声明默认为private
{
public:
int x;
int y;
void output() //函数()后面是定义变量{}后面是定义变量的值以及变量是如何实现的 c++结构体中是可以有函数的我们可以将一些操作封装到结构体内部的成员函数当中使用的时候只需调用这些成员函数
{
cout<<x<<endl<<y<<endl;
};
};
/*在main函数内部,我们利用这个结构体定义了一个结构体的变量叫pt*/
void main()
{
Point pt;
pt.x=5;
pt.y=5;
//cout<<pt.x<<endl<<pt.y<<endl; /*endl相当于 /n 即回车换行*/
pt.output();
};
//@4让程序有默认初始化值
#include<iostream.h>
//struct Point
class Point//结构体是一种特殊的类 它是用struct所定义的一种默认(可修改)是public的类 public private protected 。class如果不声明默认为private
{
public:
int x;
int y;
void init()
{
x=0; //此处不可以是intx=0 否则依然是非常大的数值
y=0;
};
void output() //函数()后面是定义变量{}后面是定义变量的值以及变量是如何实现的 c++结构体中是可以有函数的我们可以将一些操作封装到结构体内部的成员函数当中使用的时候只需调用这些成员函数
{
cout<<x<<endl<<y<<endl;
};
};
/*在main函数内部,我们利用这个结构体定义了一个结构体的变量叫pt*/
void main()
{
Point pt;
//pt.x=5;
//pt.y=5;
//cout<<pt.x<<endl<<pt.y<<endl; /*endl相当于 /n 即回车换行*/
pt.init();//pt.init();必须先一步比pt.output调用否则依然输出很大的值
pt.output();
};
//@5用构造函数修改程序不调用init且让函数有初始化值(使用构造函数 为了保证唯一性 构造函数用类名 没有返回值)
#include<iostream.h>//F10step over 单步 这个例子下两个断点 检验执行main的时候跳转到Point
//struct Point
class Point//结构体是一种特殊的类 它是用struct所定义的一种默认(可修改)是public的类 public private protected 。class如果不声明默认为private
{
public:
int x;
int y;
Point()//这就是传说中的构造函数的形式
{
x=0;
y=0;
};
void output() //函数()后面是定义变量{}后面是定义变量的值以及变量是如何实现的 c++结构体中是可以有函数的我们可以将一些操作封装到结构体内部的成员函数当中使用的时候只需调用这些成员函数
{
cout<<x<<endl<<y<<endl;
};
};
/*在main函数内部,我们利用这个结构体定义了一个结构体的变量叫pt*/
void main()
{
Point pt;
pt.output();
};
//@6析构函数 析构函数和构造函数都没有返回值(所以不用void定义 void用来定义一个函数没有返回值) 它用的也是类名 下两个断点证明程序结束之后跳转到析构函数释放程序占用的内存 F10step over
#include<iostream.h>//F10step over 单步 这个例子下两个断点 检验执行main的时候跳转到Point
//struct Point
class Point//结构体是一种特殊的类 它是用struct所定义的一种默认(可修改)是public的类 public private protected 。class如果不声明默认为private
{
public:
int x;
int y;
Point()//这就是传说中的构造函数的形式
{
x=0;
y=0;
};
~Point()//传说中析构函数的形式
{
};
void output() //函数()后面是定义变量{}后面是定义变量的值以及变量是如何实现的 c++结构体中是可以有函数的我们可以将一些操作封装到结构体内部的成员函数当中使用的时候只需调用这些成员函数
{
cout<<x<<endl<<y<<endl;
};
};
/*在main函数内部,我们利用这个结构体定义了一个结构体的变量叫pt*/
void main()
{
Point pt;
pt.output();
};
//@7函数的重载 定义三个断点看编译器是用哪个Point
#include<iostream.h>//F10step over 单步 这个例子下两个断点 检验执行main的时候跳转到Point
//struct Point
class Point//结构体是一种特殊的类 它是用struct所定义的一种默认(可修改)是public的类 public private protected 。class如果不声明默认为private
{
public:
int x;
int y;
Point()//这就是传说中的构造函数的形式
{
x=0;
y=0;
};
Point(int a,int b)//两个Point构成了函数的重载
{
x=a; //不可以为int x=a; 否则输出的又是很大的数
y=b;
};
~Point()//传说中析构函数的形式
{
};
void output() //函数()后面是定义变量{}后面是定义变量的值以及变量是如何实现的 c++结构体中是可以有函数的我们可以将一些操作封装到结构体内部的成员函数当中使用的时候只需调用这些成员函数
{
cout<<x<<endl<<y<<endl;
};
};
/*在main函数内部,我们利用这个结构体定义了一个结构体的变量叫pt*/
void main()
{
Point pt(3,3);
pt.output();
};
//@8 做一个输出函数的重载函数
#include<iostream.h>//F10step over 单步 这个例子下两个断点 检验执行main的时候跳转到Point
//struct Point
class Point//结构体是一种特殊的类 它是用struct所定义的一种默认(可修改)是public的类 public private protected 。class如果不声明默认为private
{
public:
int x;
int y;
Point()//这就是传说中的构造函数的形式
{
x=0;
y=0;
};
Point(int a,int b)//两个Point构成了函数的重载
{
x=a;
y=b;
};
~Point()//传说中析构函数的形式
{
};
void output() //函数()后面是定义变量{}后面是定义变量的值以及变量是如何实现的 c++结构体中是可以有函数的我们可以将一些操作封装到结构体内部的成员函数当中使用的时候只需调用这些成员函数
{
cout<<x<<endl<<y<<endl;
};
void output(int x,int y)
{
x=x;
y=y;
};
};
/*在main函数内部,我们利用这个结构体定义了一个结构体的变量叫pt*/
void main()
{
Point pt(3,3);
pt.output(5,5);
pt.output();
};
//@9继续例子8用this->指针实现在定义类的时候对x,y进行初始化以便后面赋值
#include<iostream.h>//F10step over 单步 这个例子下两个断点 检验执行main的时候跳转到Point
//struct Point
class Point//结构体是一种特殊的类 它是用struct所定义的一种默认(可修改)是public的类 public private protected 。class如果不声明默认为private
{
public:
int x;
int y;
Point()//这就是传说中的构造函数的形式
{
x=0;
y=0;
};
Point(int a,int b)//两个Point构成了函数的重载
{
x=a;
y=b;
};
~Point()//传说中析构函数的形式
{
};
void output() //函数()后面是定义变量{}后面是定义变量的值以及变量是如何实现的 c++结构体中是可以有函数的我们可以将一些操作封装到结构体内部的成员函数当中使用的时候只需调用这些成员函数
{
cout<<x<<endl<<y<<endl;
};
void output(int x,int y)
{
this->x=x;
this->y=y;
};
};
/*在main函数内部,我们利用这个结构体定义了一个结构体的变量叫pt*/
void main()
{
Point pt(3,3);
pt.output(5,5);
pt.output();
};
//project2
//@1继承
#include<iostream.h>
class Animal//这就是传说中的类的格式 和struct的格式是一样的
{
public:
void eat()
{
cout<<"animal eat"<<endl;
};
void sleep()
{
cout<<"animal sleep"<<endl;
};
void breathe()
{
cout<<"animal breathe"<<endl;
};
};
class Fish : public Animal//:可以贴在public身上
{
};
void main()
{
Animal an;
an.eat();
Fish fh;
fh.sleep();
};
//@2protected
#include<iostream.h>
class Animal//这就是传说中的类的格式 和struct的格式是一样的
{
public:
void eat()
{
cout<<"animal eat"<<endl;
};
protected://在子类中可以访问 在外部不可以访问 private只可以在自己类的内部被访问
void sleep()
{
cout<<"animal sleep"<<endl;
};
void breathe()
{
cout<<"animal breathe"<<endl;
};
};
class Fish : public Animal//:可以贴在public身上
{
void test()
{
sleep();
breathe();
};
};
void main()
{
Animal an;
an.eat();
Fish fh;
fh.sleep();
};
//@3在Animal中增加构造函数 看看是Animal还是Fish构造函数先调用
#include<iostream.h>
class Animal//这就是传说中的类的格式 和struct的格式是一样的
{
public:
Animal()
{
cout<<"animal construct"<<endl; //去掉这三行fish construct也会输出
};
void eat()
{
cout<<"animal eat"<<endl;
};
void sleep()
{
cout<<"animal sleep"<<endl;
};
void breathe()
{
cout<<"animal breathe"<<endl;
};
};
class Fish : public Animal//:可以贴在public身上
{
public:
Fish()
{
cout<<"fish construct"<<endl;
};
};
void main()
{
Fish fh;
};
//@4析构函数Animal与Fish调用的顺序 构造函数父类先构造再子类构造析构时候相反
#include<iostream.h>
class Animal//这就是传说中的类的格式 和struct的格式是一样的
{
public:
Animal()
{
cout<<"animal construct"<<endl; //去掉这三行fish construct也会输出
};
~Animal()
{
cout<<"animal deconstruct"<<endl;
};
void eat()
{
cout<<"animal eat"<<endl;
};
void sleep()
{
cout<<"animal sleep"<<endl;
};
void breathe()
{
cout<<"animal breathe"<<endl;
};
};
class Fish : public Animal//:可以贴在public身上
{
public:
Fish()
{
cout<<"fish construct"<<endl;
};
~Fish()
{
cout<<"fish deconstruct"<<endl;
};
};
void main()
{
Fish fh;
};
//@5在Animal上加个参数 继承父类的带参数的构造函数
#include<iostream.h>
class Animal//这就是传说中的类的格式 和struct的格式是一样的
{
public:
Animal(int weight,int height)//加上参数之后编译不通过 因为Fish本身无函数 Animal被加入了函数
{
cout<<"animal construct"<<endl; //去掉这三行fish construct也会输出
};
~Animal()
{
cout<<"animal deconstruct"<<endl;
};
void eat()
{
cout<<"animal eat"<<endl;
};
void sleep()
{
cout<<"animal sleep"<<endl;
};
void breathe()
{
cout<<"animal breathe"<<endl;
};
};
class Fish : public Animal//:可以贴在public身上
{
public:
Fish():Animal(400,300)//向子类传递带参数的父类的构造函数的一种方法
{
cout<<"fish construct"<<endl;
};
~Fish()
{
cout<<"fish deconstruct"<<endl;
};
};
void main()
{
Fish fh;
};
//@6继承自己内部的const常量
#include<iostream.h>
class Animal//这就是传说中的类的格式 和struct的格式是一样的
{
public:
Animal(int weight,int height)//加上参数之后编译不通过 因为Fish本身无函数 Animal被加入了函数
{
cout<<"animal construct"<<endl; //去掉这三行fish construct也会输出
};
~Animal()
{
cout<<"animal deconstruct"<<endl;
};
void eat()
{
cout<<"animal eat"<<endl;
};
void sleep()
{
cout<<"animal sleep"<<endl;
};
void breathe()
{
cout<<"animal breathe"<<endl;
};
};
class Fish : public Animal//:可以贴在public身上
{
public:
Fish():Animal(400,300),a(1) //向子类传递带参数的父类的构造函数的一种方法
{
cout<<"fish construct"<<endl;
};
~Fish()
{
cout<<"fish deconstruct"<<endl;
};
private:
const int a;
};
void main()
{
Fish fh;
};
//@7 fish bubble 创建子类的breathe看先继承父类还是子类的breathe 函数的覆盖
#include<iostream.h>
class Animal//这就是传说中的类的格式 和struct的格式是一样的
{
public:
Animal(int weight,int height)//加上参数之后编译不通过 因为Fish本身无函数 Animal被加入了函数
{
// cout<<"animal construct"<<endl; //去掉这三行fish construct也会输出
};
~Animal()
{
// cout<<"animal deconstruct"<<endl;
};
void eat()
{
cout<<"animal eat"<<endl;
};
void sleep()
{
cout<<"animal sleep"<<endl;
};
void breathe()
{
cout<<"animal breathe"<<endl;
};
};
class Fish : public Animal//:可以贴在public身上
{
public:
Fish():Animal(400,300),a(1) //向子类传递带参数的父类的构造函数的一种方法
{
// cout<<"fish construct"<<endl;
};
~Fish()
{
// cout<<"fish deconstruct"<<endl;
};
/* void breathe() // 当注释起void breathe() 子类会从父类继承 但一旦定义子类的breathe即使没有输出依然从自己这里继承
// {
// cout<<"fish bubble"<<endl;
}; */
private:
const int a;
};
void main()
{
Fish fh;
fh.breathe();
};
//@8 孩子结婚既要轿车又要花轿 breathe既要bubble 又要Animal的breathe
#include<iostream.h>
class Animal//这就是传说中的类的格式 和struct的格式是一样的
{
public:
Animal(int weight,int height)//加上参数之后编译不通过 因为Fish本身无函数 Animal被加入了函数
{
// cout<<"animal construct"<<endl; //去掉这三行fish construct也会输出
};
~Animal()
{
// cout<<"animal deconstruct"<<endl;
};
void eat()
{
cout<<"animal eat"<<endl;
};
void sleep()
{
cout<<"animal sleep"<<endl;
};
void breathe()
{
cout<<"animal breathe"<<endl;
};
};
class Fish : public Animal//:可以贴在public身上
{
public:
Fish():Animal(400,300),a(1) //向子类传递带参数的父类的构造函数的一种方法
{
// cout<<"fish construct"<<endl;
};
~Fish()
{
// cout<<"fish deconstruct"<<endl;
};
void breathe() // 当注释起void breathe() 子类会从父类继承 但一旦定义子类的breathe即使没有输出依然从自己这里继承
{
Animal::breathe();
cout<<"fish bubble"<<endl;
};
private:
const int a;
};
void main()
{
Fish fh;
fh.breathe();
};
//@9定义全局函数并加入Animal的指针 没看懂(现在懂点了 全局函数是在类之外的)
#include<iostream.h>
class Animal//这就是传说中的类的格式 和struct的格式是一样的
{
public:
Animal(int weight,int height)//加上参数之后编译不通过 因为Fish本身无函数 Animal被加入了函数
{
// cout<<"animal construct"<<endl; //去掉这三行fish construct也会输出
};
~Animal()
{
// cout<<"animal deconstruct"<<endl;
};
void eat()
{
cout<<"animal eat"<<endl;
};
void sleep()
{
cout<<"animal sleep"<<endl;
};
void breathe()
{
cout<<"animal breathe"<<endl;
};
};
class Fish : public Animal//:可以贴在public身上
{
public:
Fish():Animal(400,300),a(1) //向子类传递带参数的父类的构造函数的一种方法
{
};
void breathe() // 当注释起void breathe() 子类会从父类继承 但一旦定义子类的breathe即使没有输出依然从自己这里继承
{
cout<<"fish bubble"<<endl;
};
private:
const int a;
};
void fn(Animal *pAn) //定义了一个全局的函数 暂时认为定义全局函数的时候一定要放到最后 (有待查证)定义的全局函数已经跳出了类的{}
{
pAn->breathe();
};
void main()
{
Fish fh;//产生fh对象的时候首先要构造Animal this->指针指向的首地址既是fh对象的地址又是Animal的地址
Animal *pAn;
pAn=&fh;
fn(pAn);
};
//@10调用Fish对象的breathe 使用虚函数 c++的多态性 多态性一句话如果在基类相应的函数前面加上一个virtual子类有的调用子类的子类没有的调用父类的(前提条件是传递的是子类的地址)
#include<iostream.h>
class Animal//这就是传说中的类的格式 和struct的格式是一样的
{
public:
Animal(int weight,int height)//加上参数之后编译不通过 因为Fish本身无函数 Animal被加入了函数
{
// cout<<"animal construct"<<endl; //去掉这三行fish construct也会输出
};
~Animal()
{
// cout<<"animal deconstruct"<<endl;
};
void eat()
{
cout<<"animal eat"<<endl;
};
void sleep()
{
cout<<"animal sleep"<<endl;
};
virtual void breathe() //如果只是虚函数即使定义了virtual 但是如果子类没有定义子类的breathe 依然会从加了virtual的breathe这里继承 但是如果把父类的breathe注释起来由于要继承父类子类即使有breathe也会由于找不到父类的breathe报错
{
cout<<"animal breathe"<<endl;
};
};
class Fish : public Animal//:可以贴在public身上
{
public:
Fish():Animal(400,300),a(1) //向子类传递带参数的父类的构造函数的一种方法
{
};
void breathe() // 当注释起void breathe() 子类会从父类继承 但一旦定义子类的breathe即使没有输出依然从自己这里继承
{
cout<<"fish bubble"<<endl;
};
private:
const int a;
};
void fn(Animal *pAn) //定义了一个全局的函数 暂时认为定义全局函数的时候一定要放到最后 (有待查证)定义的全局函数已经跳出了类的{}
{
pAn->breathe();
};
void main()
{
Fish fh;
Animal *pAn;
pAn=&fh;
fn(pAn);
};
//@11c++中的纯虚函数 是为了将来不好确定由子类继承的函数而给出的一个名字空壳
#include<iostream.h>
class Animal//这就是传说中的类的格式 和struct的格式是一样的
{
public:
Animal(int weight,int height)//加上参数之后编译不通过 因为Fish本身无函数 Animal被加入了函数
{
// cout<<"animal construct"<<endl; //去掉这三行fish construct也会输出
};
~Animal()
{
// cout<<"animal deconstruct"<<endl;
};
void eat()
{
cout<<"animal eat"<<endl;
};
void sleep()
{
cout<<"animal sleep"<<endl;
};
virtual void breathe()=0; //当改为纯虚函数的时候必须把{}里面的东西注释掉而且必须子类的breathe保留才可以
/*{
cout<<"animal breathe"<<endl;
}; */
};
class Fish : public Animal//:可以贴在public身上
{
public:
Fish():Animal(400,300),a(1) //向子类传递带参数的父类的构造函数的一种方法
{
};
void breathe() // 当注释起void breathe() 子类会从父类继承 但一旦定义子类的breathe即使没有输出依然从自己这里继承
{
cout<<"fish bubble"<<endl;
};
private:
const int a;
};
void fn(Animal *pAn) //定义了一个全局的函数 暂时认为定义全局函数的时候一定要放到最后 (有待查证)定义的全局函数已经跳出了类的{}
{
pAn->breathe();
};
void main()
{
Fish fh;
Animal *pAn;
pAn=&fh;
fn(pAn);
};
//@12引用的概念 引用就是一个别名 区别指针
int a=6;
int &b=a;
b=5;
int c=7;
b=c;
//@13把一个函数的实现拿到类的定义外部来
#include<iostream.h>
class Animal//这就是传说中的类的格式 和struct的格式是一样的
{
public:
Animal(int weight,int height)//加上参数之后编译不通过 因为Fish本身无函数 Animal被加入了函数
{
// cout<<"animal construct"<<endl; //去掉这三行fish construct也会输出
};
~Animal()
{
// cout<<"animal deconstruct"<<endl;
};
void eat();
void sleep()
{
cout<<"animal sleep"<<endl;
};
virtual void breathe()=0; //当改为纯虚函数的时候必须把{}里面的东西注释掉而且必须子类的breathe保留才可以
/*{
cout<<"animal breathe"<<endl;
}; */
};
void Animal::eat()
{
};
class Fish : public Animal//:可以贴在public身上
{
public:
Fish():Animal(400,300),a(1) //向子类传递带参数的父类的构造函数的一种方法
{
};
void breathe() // 当注释起void breathe() 子类会从父类继承 但一旦定义子类的breathe即使没有输出依然从自己这里继承
{
cout<<"fish bubble"<<endl;
};
private:
const int a;
};
void fn(Animal *pAn) //定义了一个全局的函数 暂时认为定义全局函数的时候一定要放到最后 (有待查证)定义的全局函数已经跳出了类的{}
{
pAn->breathe();
};
void main()
{
Fish fh;
Animal *pAn;
pAn=&fh;
fn(pAn);
};
//@14封装 类的定义以及函数原型的声明放到头文件中 类中成员函数的实现放到源文件中
//Animal.h
class Animal//这就是传说中的类的格式 和struct的格式是一样的
{
public:
Animal(int weight,int height)
void eat();
void sleep();
virtual void breathe();
};
//Animal.cpp
Animal::Animal() //先写构造函数 没有返回值
{};
void Animal::eat()
{
cout<<"animal eat"<<endl;
};
void Animal::sleep()
{
cout<<"animal sleep"<<endl;
};
void Animal::breathe()
{
cout<<"animal breathe"<<endl;
};
//@15解决头文件重复包含问题 Fish.h再一次包含了Animal.h造成了重复包含 使用预编译指令符 当我们设计完一个类的时候不管怎样都应该加上预编译指令符
//编译时候发生的错误很好查找 链接时候产生的错误不好整
#ifndef POINT_H_H_H
#define POINT_H_H_H
class Point
{
};
#endif
#ifndef POINT_H_H_H
#define POINT_H_H_H
class Point
{
};
#endif
void main()
{
Point pt;
};
[课程]Linux pwn 探索篇!