#include<iostream>#include"head1.h"usingnamespace std;//源文件类外定义//一定加上类名作用域CStu::否则不识别/*CStu::CStu()//无参构造函数
{
age = 1234;
time = 88.65f;
}*/
CStu::CStu(int a,float b)//有参构造函数,此处定义不可写默认参数值{
age = a;
time = b;}int CStu::fun()//普通函数,注意类名作用域加在函数名fun之前!!!{
cout << age << endl;return0;}intmain(){
CStu stu;
CStu*stu1 =newCStu(88,78.39f);
cout << stu.age <<' '<< stu.time <<' '<< stu.fun()<< endl;
cout << stu1->age <<' '<< stu1->time<<' '<< stu1->fun()<< endl;system("pause");return0;}
类的类型转化
#include<iostream>usingnamespace std;classA{public:int a;virtualvoidfun(){}};classB:public A
{int b;void fun (){}};classCother{int c;};intmain(){
A *p;//父类
B *s=0;//子类
Cother * o=0;//其他类//p = static_cast<A*>(s);//o = static_cast<Cother*>(s);//“ = ”: 无法从“B * ”转换为“Cother *//o = (Cother*)s;强制类型转换不报错但不建议使用const A*q=0;
A*q1 =const_cast<A*>(q);//const_cast只能转化为原来的类型//B*q1 = const_cast<A*>(q);//无法从“A *”转换为“B *”//基类型到派生类型的强制转换需要 dynamic_cast 或 static_cast//A*q1 = const_cast<B*>(q); // “const_cast” : 无法从“const A *”转换为“B *” //dynamic_cast 父子类互相转化或者自己转自己
p =dynamic_cast<A*>(s);//子类直接转父类
s =dynamic_cast<B*>(p);//父类转子类必须是多态类型 即父类A中有virtual虚函数
A*p1 =dynamic_cast<A*>(p);//自己转自己//reinterpret_cast<type>(expression)用于危险类型的转换 如long转short等//内存一定要够用,一般底层编程用,代码移植性差,C3老湿不建议使用struct dat
{short a;short b;};long value =0xA224B118;
dat* h =reinterpret_cast<dat*>(&value);
cout<<hex<<h->a;system("pause");return0;}//注:新式转换比较智能,对于不合理的会有限制,老式的强制转换不会提示//尽量不使用类型转换,一定要用就用新式的!
内部类
#include<iostream>usingnamespace std;classOu{public:int a;Ou(){
a =122;}public:classIn{int b;public:In(){
b =88;}voidfun(){//cout << a << endl; 非静态成员引用必须与特定对象相对
Ou s1;
cout << s1.a << endl;}};public://外部类里创建一个内部类的对象
In s2;};intmain(){
Ou s3;
s3.s2.fun();system("pause");return0;}
#include<iostream>usingnamespace std;classCStu{private:int age;voidfun(){
age =12;
cout << age << endl;}friendvoidfun1();//需要声明//友元函数:针对private或protected类型权限,类内成员对于这个函数相当于publicfriendintmain();//不需要声明friendclassCTeach;//友元类:不相干的类2只能通过友元类来调用类1函数public://接口函数intGet(){return age;}voidSet(){
age =34;}};classCTeach{public:
CStu stu2;voidfun2(){
stu2.fun();}};voidfun1(){
CStu stu;
stu.fun();}intmain(){
CStu stu1;
stu1.Set();int a = stu1.Get();
cout << a << endl;
stu1.fun();fun1();
CTeach teach;
teach.fun2();system("pause");return0;}
多态虚函数
#include<iostream>usingnamespace std;classfather{public:virtualvoidshow(){
cout <<"我是father\n";}};classson:public father
{public:int a;virtualvoidshow()//不写也默认virtual{
cout <<"我是son"<< endl;}};classchild:public son
{public:int a;//virtual针对于函数成员,不针对变量成员voidshow(){
cout <<"我是child"<< endl;}};intmain(){
father * p =new son;//父类指针指向子类空间调用子类函数 多态:父类指针可以有多种状态
p->show();//父类show函数没有virtual的话调用父类show 有virtual的话调用子类show即虚函数的重写!
father * s =new child;
s->show();//三重继承的话父类virtual,子类也默认virtual调用孙子类show//虚函数函数名和参数列表以及返回值必须相同,否则无法满足以上调用顺序,按原来的方式调用
child ch;
father &q = ch;//父类对象的引用也可以调用虚函数
ch.show();
q.show();system("pause");return0;}/*注意:构造函数不能是虚函数而是内联函数!
内联inline函数不能是虚函数,也没有任何意义!
*/
协变
//虚函数函数名和参数列表以及返回值必须相同,否则无法满足以上调用顺序,按原来的方式调用
特殊情况
协变:返回值不同情况 重写虚函数返回类型有差异,且不是来自“Son::show”的协变
#include<iostream>usingnamespace std;classFather{public:virtual Father &show(){
cout <<"我是Father\n";return*this;}};classSon:public Father
{public:int a;virtual Son &show()//不写也默认virtual{
cout <<"我是Son"<< endl;return(*this);//一般加上小括号}};intmain(){
Father * x =new Son;
x->show();return0;}
虚继承
#include<iostream>usingnamespace std;classA//父类A叫做虚基类{public:int a;};classB:virtualpublic A
{public:};classC:virtualpublic A
{public:};classD:public B,public C
{public:};intmain(){
D p;
p.a;system("pause");return0;}//虚继承用来解决多继承中访问不明确的问题
虚析构
#include<iostream>usingnamespace std;classFather{public:virtual~Father()//多态中,如果释放父类指针,只会调用父类析构函数,加了虚析构就会子类父类都调用{
cout <<"father"<< endl;}};classSon:public Father
{public:~Son()//隐式加上virtual{
cout <<"son"<< endl;}};classWL{public:~WL(){
cout <<"WL"<< endl;}};intmain(){/*Father *q = new Son;
delete q;
*/
Father *z =new Son;delete z;//delete (WL*)z;//强制转化为WL类/*
delete特点
1.指针是谁的类型调用谁的析构函数!
2.申请的空间一定释放!对象不泄露,对象的成员申请的空间会被泄露
*/system("pause");return0;}