C++经典语法与应用,类的编写与应用,构造与析构函数,函数的重载,类的继承,函数覆盖,基类与派生类的构造函数、析构函数先后调用顺序,如何在派生类构造函数中向基类的构造函数传递参数,this成员变量,类型转换的内幕,虚拟函数与多态性,引用和指针的变量的区别与共同处。
VC工程的编译原理与过程,将工程中不同的类拆分到不同的原文件中,每一个类由一个.h和.cpp文件共同完成,头文件重复定义问题的解决,培养了学员良好的编程习惯,也为以后分析MFC Appwizard生成的工程奠定了良好基础。
C++的标准输入输出流
C++中提供了一套输入输出流类的对象,它们是cin 、cout和cerr,对应c语言中的三个文件指针stdin、stdout、stderr,分别指向终端输入、终端输出和标准出错输出(也从终端输出)。
cin与>>一起完成输入操作,cout、cerr与<<一起完成输出与标准错误输出。
利用cin和cout比C语言中的scanf和printf要方便得多,cin和cout可以自动判别输入输出数据类型而自动调整输入输出格式,不必像scanf和printf那样一个个由用户指定。使用cin,cout不仅方便,而且减少了出错的可能性。
对于输出来说,我们像以上方式调用就可以了,对于输入来说,我们以如下方式调用即可:
int i; cin>>i;
注意箭头的方向。在输出中我们还使用endl(end of line),表示换行,注意最后一个是字符‘l’,而不是数字1,endl相当于C语言的'\n',表示输出一个换行。
#include <iostream> using namespace std; //定义结构体 //struct Point class Point { public://表示所有数据成员都可以在外部访问 //private:私有,对外数据成员都不可访问 //protected:子类和父类之间的访问 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(); }
构造函数
1、构造函数最重要的作用是创建对象本身
2、C++规定,每个类必须有一个构造函数,没有构造函数,就不能创建任何对象
3、C++又规定,如果一个类没有提供任何的构造函数,则C++提供一个默认的构造函数(由C++编译器提供),这个默认的构造函数是一个不带参数的构造函数,它只负责创建对象,而不做任何的初始化工作
4、只要一个类定义了一个构造函数,不管这个构造函数是否是带参数的构造函数,C++就不再提供默认的构造函数。也就是说,如果为一个类定义了一个带参数的构造函数,还想要无参数的构造函数,则必须自己定义
#include <iostream> using namespace std; //定义结构体 //struct Point class Point { public://表示所有数据成员都可以在外部访问 int x; int y; /* void init() { x=0; y=0; };*/ Point()//构造函数,用于创建对象本身 { x=0; y=0; } void output() { cout<<x<<endl<<y<<endl; } }; void main() { Point pt;//实例化一个对象 //pt.init(); //pt.x=5; //pt.y=5; //cout<<pt.x<<endl<<pt.y<<endl; pt.output(); }
析构函数
1、当一个对象生命周期结束时,其所占有的内存空间就要被回收,这个工作就由析构函数来完成。
2、析构函数是“反向”的构造函数,析构函数不允许有返回值,更重要的是析构函数不允许带参数,并且一个类中只能有一个析构函数。
3、析构函数的作用正好与构造函数相反,对象超出其作用范围,对应的内存空间被系统收回或被程序用delete删除时,析构函数被调用。
4、根据析构函数的这种特点,我们可以在构造函数中初始化对象的某些成员变量,给其分配内存空间(堆内存),在析构函数中释放对象运行期间所申请的资源。
#include <iostream> using namespace std; class Point { public://表示所有数据成员都可以在外部访问 int x; int y; /* void init() { x=0; y=0; };*/ Point() { x=0; y=0; } ~Point()//析构函数,生命周期结束的时候调用这个函数进行空间释放,系统自动完成 { } void output() { cout<<x<<endl<<y<<endl; } }; void main() { Point pt;//实例化一个对象 pt.output(); }
函数的重载
重载构成的条件:函数的参数类型、参数个数不同,才能构成函数的重载。
分析以下两种情况,是否构成函数的重载。
第一种情况:(1)void output();
(2)int output();
只有返回值不同的两个函数不能构成函数的重载
第二种情况:(1)void output(int a,int b=5);
(2)void output(int a);
带缺省值也是不能构成函数重载
this指针
1、this指针是一个隐含的指针,它是指向对象本身,代表了对象的地址
2、一个类所有的对象调用的成员函数都是同一代码段。那么成员函数又是怎么识别属于同一对象的数据成员呢?
原来,在对象调用pt.output(10,10)时,成员函数除了接受2个实参外,还接受到了一个对象s的地址。这个地址被一个隐含的形参this指针所获取,它等同于执行this=&pt
所有对数据成员的访问都隐含地被加上前缀this->。例如:x=0; 等价于 this->x=0
在以后的MFC编程中,如果在成员函数中想调用同类中的某个成员,可以使用VC++提供的自动列出成员函数功能,使用this->,VC++将列出该类中的所有成员,我们可以从列表中选择我们想调用的成员。
自动列出成员函数功能,可以提高编写速度,减少拼写错误。我们经常不能完全记住某个函数的完整拼写,但却能够从列表中辨别出该函数,自动列出成员函数的功能在这时就显得更加有用了。
事实上,在各种IDE编程环境中,我们通常都不可能记住也没有必要记住所有的函数,只要将常用的函数记住,其他不常用的函数只要记住其大概的写法和功能,在调用该函数时可以从自动列出成员函数中选取,这样可以大大节省我们的学习时间。
我们不用花费大量的时间去死记硬背许多函数,利用自动列出成员函数功能和帮助系统,就能够在编程时顺利地使用这些函数,等用的次数多了,也就在不知不觉中完全掌握了这些函数。
#include <iostream> using namespace std; class Point { public://表示所有数据成员都可以在外部访问 int x; int y; /* void init() { x=0; y=0; };*/ Point() { x=0; y=0; } 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(3,3);//实例化一个对象 pt.output(5,5); pt.output(); }
类的继承
在C++中,给我们提供了一种重要的机制,就是继承。 理解继承是理解面向对象程序设计的关键。
#include<iostream> using namespace std; class Animal { public: void eat() { cout<<"animal eat"<<endl; } void sleep() { cout<<"animal sleep"<<endl; } void breathe() { cout<<"animal breathe"<<endl; } }; class Fish:public Animal//Animal是基类(父类),Fish是派生类(子类) { }; void main() { Animal an; an.eat(); Fish fh; fh.sleep(); }
访问权限
#include<iostream> using namespace std; class Animal { public: void eat() { cout<<"animal eat"<<endl; } protected://保护,对于子类是可以访问,在外部不可以被访问 void sleep() { cout<<"animal sleep"<<endl; } private://私有方法,在子类中不可以访问 void breathe() { cout<<"animal breathe"<<endl; } }; class Fish:public Animal//Animal是基类(父类),Fish是派生类(子类) { void test() { sleep(); //breathe(); } }; void main() { Animal an; an.eat(); Fish fh; //fh.sleep();//保护的方法不能在外部访问 }
类的继承访问特性
#include<iostream> using namespace std; class Animal { public: Animal() { cout<<"animal construct"<<endl; } ~Animal() { cout<<"animal decconstruct"<<endl; } void eat() { cout<<"animal eat"<<endl; } void sleep() { cout<<"animal sleep"<<endl; } void breathe() { cout<<"animal breathe"<<endl; } }; class Fish:public Animal { public: Fish() { cout<<"fish construct"<<endl; } ~Fish() { cout<<"fish decconstruct"<<endl; } }; void main() { Fish fh; }
输出结果:
函数的覆盖
函数的覆盖是发生在父类与子类之间
#include<iostream> using namespace std; class Animal { public: Animal(int height,int weight) { cout<<"animal construct"<<endl; } ~Animal() { cout<<"animal decconstruct"<<endl; } void eat() { cout<<"animal eat"<<endl; } void sleep() { cout<<"animal sleep"<<endl; } void breathe() { cout<<"animal breathe"<<endl; } }; class Fish:public Animal { public: Fish():Animal(400,300),a(1) { //cout<<"fish construct"<<endl; } ~Fish() { //cout<<"fish decconstruct"<<endl; } void breathe() { Animal::breathe();//::表示作用域标识符,表示这个函数是属于哪个类的 cout<<"fish bubble"<<endl; } private: const int a; }; void main() { Fish fh; fh.breathe(); }
输出结果:
animal construct
animal breathe
fish bubble
animal decconstruct
函数的多态
多态性 当C++编译器在编译的时候,发现Animal类的breathe()函数是虚函数,这个时候C++就会采用迟绑定(late binding)的技术,在运行时,依据对象的类型(在程序中,我们传递的Fish类对象的地址)来确认调用的哪一个函数,这种能力就做C++的多态性。
#include<iostream> using namespace std; class Animal { public: Animal(int height,int weight) { //cout<<"animal construct"<<endl; } void eat() { cout<<"animal eat"<<endl; } void sleep() { cout<<"animal sleep"<<endl; } virtual void breathe() //多态性:virtual 定义虚函数 会采用迟绑定,子类如果有就调用子类的,子类如果没有就调用父类的 { cout<<"animal breathe"<<endl; } }; class Fish:public Animal { public: Fish():Animal(400,300),a(1) { //cout<<"fish construct"<<endl; } ~Fish() { //cout<<"fish decconstruct"<<endl; } void breathe() { //Animal::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); }
纯虚函数 virtual void breathe()=0;
指被表明为不具体实现的虚成员函数
纯虚函数让类先具有操作的名称而没有具体的内容,让派生类在继承的时候,再具体给出定义