- 一个基类可以派生多个派生类
#include<bits/stdc++.h>
using namespace std;
class Base{
private:
int x;
public:
Base():x(0){}
Base(int x){
this->x=x;
cout<<"Base cons!"<<endl;
}
void Print()
{
cout<<"x="<<x<<endl;
}
~Base(){
cout<<"Base des!"<<endl;
}
};
class DA:public Base{
private:
int y;
public:
DA(){
y=0;
cout<<"DA cons!"<<endl;
}
DA(int i,int j):Base(i)
{
y=j;
cout<<"DA cons!"<<endl;
}
void Print()
{
Base::Print();
cout<<"y="<<y<<endl;
}
~DA()
{
cout<<"DA des!"<<endl;
}
};
class DB:public Base{
private:
int z;
public:
DB(){
z=0;
cout<<"DB cons!"<<endl;
}
DB(int i):Base(i+2)
{
z=i;
cout<<"DB cons!"<<endl;
}
void Print()
{
Base::Print();
cout<<"z="<<z<<endl;
}
~DB()
{
cout<<"DB des!"<<endl;
}
};
int main()
{
DA *pda=new DA(3,4);
DB *pdb=new DB(3);
pda->Print();
pdb->Print();
delete pda;
delete pdb;
return 0;
}
- 可将基类的数据成员设置为protected权限
——在派生类的公有方法中可直接访问基类的受保护数据
#include<bits/stdc++.h>
using namespace std;
class Base{
protected:
int x,y;
public:
Base(){
x=0;
y=0;
cout<<"Base cons!"<<endl;
}
Base(int x,int y){
this->x=x;
this->y=y;
cout<<"Base cons!"<<endl;
}
~Base(){
cout<<"Base des!"<<endl;
}
};
class D1:public Base{
private:
int z;
public:
D1(){
z=0;
cout<<"D1 cons!"<<endl;
}
D1(int i):Base(i+1,i+2)
{
z=i;
cout<<"D1 cons!"<<endl;
}
void Print()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"z="<<z<<endl;
}
~D1(){
cout<<"D1 des!"<<endl;
}
};
int main()
{
D1 *pd=new D1(1);
pd->Print();
delete pd;
return 0;
}
- 类的成员函数也可在类内声明,类外实现
i:在类外实现时,在方法名前一定加上类作用域
ii:在类外实现的方法,一定要在类内声明
#include<bits/stdc++.h>
using namespace std;
class Myclass{
private:
int x,y;
public:
Myclass(){
x=0;
y=0;
}
Myclass(int,int);
void Print();
};
Myclass::Myclass(int x,int y)
{
this->x=x;
this->y=y;
}
void Myclass::Print()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
int main()
{
Myclass *p=new Myclass(2,3);
p->Print();
delete p;
}
-
多态性:一个操作,返回不同
i:静态联编:在编译阶段系统就要确定主调和被调的调用关系
——所有的非虚方法调用,均为静态联编
ii:动态联编:在程序运行时,才确定方法调用 -
虚函数:
i:是类的成员函数
ii:定义: virtual 数据类型 方法名(实参)
{
//
}
只有在继承中才可使用虚函数
怎么使用(多态性):
a》基类和派生类有同名,同参,同返回,同访问权限的成员方法
b》基类的同名方法被虚化,派生类的同名方法将自动被虚化
c》基类指针指向派生类对象
结论:通过基类指针调用同名方法时,系统采用动态联编,调用派生类的同名方法
#include<bits/stdc++.h>
using namespace std;
class Base{
public:
virtual void fun()
{
cout<<"Base fun()"<<endl;
}
};
class D1:public Base{
public:
void fun()
{
cout<<"D1 fun()"<<endl;
}
};
class D2:public Base{
public:
void fun()
{
cout<<"D2 fun()"<<endl;
}
};
int main()
{
Base *pb=new Base();
pb->fun();
delete pb;
pb=new D1();
pb->fun();
delete pb;
return 0;
}
-
空虚函数
virtual 数据类型 方法名(形参)
{ }
空虚函数有函数体,函数体为空,但可以调用 -
纯虚函数
virtual 数据类型 方法名(形参)=0;
纯虚函数不可调用 -
抽象类:含有纯虚函数的类
i:抽象类一定是基类
ii:抽象类不可创建对象
#include<bits/stdc++.h>
using namespace std;
class Base{//抽象类
public:
virtual void fun()=0;//纯虚函数
};
class D1:public Base{
public:
void fun()
{
cout<<"D1 fun()"<<endl;
}
};
class D2:public Base{
public:
void fun()
{
cout<<"D2 fun()"<<endl;
}
};
int main()
{
//Base *pb=new Base();//编译出错
Base *pb=new D1();
pb->fun();//D1::fun();
delete pb;
pb=new D2();
pb->fun();//D2::fun();
delete pb;
return 0;
}
封装客户端
#include<bits/stdc++.h>
using namespace std;
class Base{//抽象类
public:
virtual void fun()=0;//纯虚函数
};
class D1:public Base{
public:
void fun()
{
cout<<"D1 fun()"<<endl;
}
};
class D2:public Base{
public:
void fun()
{
cout<<"D2 fun()"<<endl;
}
};
class My{
private:
Base *pb;
public:
My(Base *t){
pb=t;
}
void fun(){
pb->fun();
}
};
int main()
{
D2 *d2=new D2();
My *pm=new My(d2);
pm->fun();
return 0;
}