C++学习:多态(抽象类和接口)

C++学习:多态


简介:
在面向对象编程的语言中,封装,继承,多态这三个要素是必不可少的,而面向对象最主要的一点也就是面向接口编程,也就是需要先去从具体的业务中去抽象出类,然后在将类实例称具体对象.但是往往有时候,有些类是不能进行具体实例化的,如动物,电脑等,所以就将其抽象成了一种抽象类.
接口:接口并不是类,编写接口的方式和类很相似,但是它们属于不同的概念,一个类通过继承接口的方式,从而来继承接口的抽象方法.它是一类共同类型的东西抽象汇总出来的共有行为的过程.如:电视机,电脑等具有的行为是播放,开,关等.
在java中,所谓的多态就是不同的对象调用同一个方法的时候会产生不同的结果,多态的核心就是面向抽象类和接口编程当然,这种说法在C++中同样也适用,毕竟java来源于C++


友情提示

博主:章飞_906285288
博客地址:http://blog.csdn.net/qq_29924041


先来说多态的第一种形式,抽象类:

前一篇博客中简单介绍了虚函数,纯虚函数这两种函数,虚函数主要是用于常规继承,父类也是可以实例化的,纯虚函数则主要是用于抽象类和接口.

直接上一组代码:(电脑,台式机,平板,笔记本电脑)

/*
 * ===========================================================================
 *
 *       Filename:  abstractor.h
 *    Description:  
 *        Version:  1.0
 *        Created:  2017年06月25日 16时07分46秒
 *       Revision:  none
 *       Compiler:  gcc
 *         Author:   (), 
 *        Company:  
 *
 * ===========================================================================
 */

#ifndef __ABSTRACTOR_H__
#define __ABSTRACTOR_H__


class Computer
{  
  public:
    Computer();
    //虚析构函数
    virtual ~Computer();

    //纯虚函数
    virtual void printSystemType() const = 0; //纯虚函数,父类是不能够定义的,只能进行声明,在子类中进行定义
    //定义一下抽象类中的虚函数,是为了在调用过程中能够动态选择对象的过程
    virtual void setWidth(int width);
    virtual void setHeight(int height);
    virtual void setWeight(int weight);
    virtual void printInfo();
  private:
    int c_width;
    int c_height;
    int c_weight;
};

/* *
 *DesktopComputer 继承抽象类Computer
 * */
class DesktopComputer : public Computer
{
  public:
    DesktopComputer();
    ~DesktopComputer();
    //由于继承的是抽象类.而printSystemType是抽象方法,所以这个时候父类是声明的,但是子类需要进行定义
    void printSystemType() const;
  private:
};

/* *
 *NetBookComputer 继承抽象类 Computer
 * */
class NetBookComputer : public Computer
{
  public:
    NetBookComputer();
    ~NetBookComputer();
    void printSystemType() const;
  private:
};

/* *
 *PanelComputer 继承Computer
 * */
class PanelComputer : public Computer
{
  public:
    PanelComputer();
    ~PanelComputer();
    void printSystemType() const;
  private:

};

#endif
/*
 * ===========================================================================
 *
 *       Filename:  abstractor.cpp
 *    Description:  
 *        Version:  1.0
 *        Created:  2017年06月25日 16时20分35秒
 *       Revision:  none
 *       Compiler:  gcc
 *         Author:   (), 
 *        Company:  
 *
 * ===========================================================================
 */

#include<iostream>
#include"abstractor.h"
using namespace::std;

/* *
 *注意:
 在函数进行定义的时候,是可以不用讲virtural关键字写上的
 * */


Computer::Computer()
{
  cout<<"computer construtor"<<endl;
}

Computer::~Computer()
{
  cout<<"computer destructor"<<endl;
}

void Computer::setWeight(int weight)
{
  c_weight = weight;
}

void Computer::setWidth(int width)
{
  c_width = width;
}

void Computer::setHeight(int height)
{
  c_height = height;
}

void Computer::printInfo(){
  cout<<"c_height:"<<c_height<<endl;
  cout<<"c_width:"<<c_width<<endl;
  cout<<"c_weight:"<<c_weight<<endl;
}

DesktopComputer::DesktopComputer()
{
  cout<<"DesktopComputer constructor"<<endl;
}

DesktopComputer::~DesktopComputer()
{
  cout<<"DesktopComputer destructor"<<endl;
}

/* *
 *实现子类中的抽象方法(纯虚构函数)
 * */
void DesktopComputer::printSystemType() const
{
  cout<<"DesktopComputer system is Window or linux"<<endl;
}

NetBookComputer::NetBookComputer()
{
  cout<<"NetBookComputer constructor"<<endl;
}


NetBookComputer::~NetBookComputer()
{
  cout<<"NetBookComputer destructor"<<endl;
}

/* *
 *实现子类中的 抽象方法
 * */
void NetBookComputer::printSystemType() const{
  cout<<"NetBookComputer system is windows or linux"<<endl;
}

PanelComputer::PanelComputer()
{
  cout<<"PanelComputer constructor"<<endl;
}

PanelComputer::~PanelComputer()
{
  cout<<"PanelComputer destructor"<<endl;
}

/* *
 * 实现子类中的抽象方法
 * */
void PanelComputer::printSystemType() const
{
  cout<<"PanelComputer system is os or android"<<endl;
}
/*
 * ===========================================================================
 *
 *       Filename:  abstractorTest.cpp
 *    Description:  
 *        Version:  1.0
 *        Created:  2017年06月25日 16时31分39秒
 *       Revision:  none
 *       Compiler:  gcc
 *         Author:   (), 
 *        Company:  
 *
 * ===========================================================================
 */

#include<iostream>
#include"abstractor.h"
using namespace::std;



int main(int argc ,char* argv[]){

  //Computer *mComputer = new Computer();
  //编译的时候报错,主要是因为抽象类不能实例化

  //依赖抽象而不去依赖具体,父类的指针指向子类的对象
  Computer* desktopComputer = new DesktopComputer();
  desktopComputer->printSystemType(); 
  desktopComputer->setWidth(2564);
  desktopComputer->setHeight(1980);
  desktopComputer->setWeight(20);

  desktopComputer->printInfo();
  cout<<"----------------------"<<endl;

  Computer* netBookComputer = new NetBookComputer();
  netBookComputer->printSystemType();

  netBookComputer->setWidth(1920);
  netBookComputer->setHeight(1080);
  netBookComputer->setWeight(15);
  netBookComputer->printInfo();
  cout<<"----------------------"<<endl;
  Computer* panelComputer = new PanelComputer();
  panelComputer->printSystemType();

  panelComputer->setWidth(1080);
  panelComputer->setHeight(720);
  panelComputer->setWeight(10);
  panelComputer->printInfo();
  cout<<"----------------------"<<endl;

  delete desktopComputer;
  delete netBookComputer;
  delete panelComputer;
  return 0;
}

输出的结果为:

computer construtor
DesktopComputer constructor
DesktopComputer system is Window or linux
c_height:1980
c_width:2564
c_weight:20
----------------------
computer construtor
NetBookComputer constructor
NetBookComputer system is windows or linux
c_height:1080
c_width:1920
c_weight:15
----------------------
computer construtor
PanelComputer constructor
PanelComputer system is os or android
c_height:720
c_width:1080
c_weight:10
----------------------
DesktopComputer destructor
computer destructor
NetBookComputer destructor
computer destructor
PanelComputer destructor
computer destructor

从上面可以看到所有的都是依赖抽象,用父类的指针指向子类的对象
子类继承父类后,调用父类的同一个方法后,却产生了不同的结果,这就是多态度

关于抽象类的一点总结(不喜勿喷):

1:抽象类是在具体实体类基础上又再次抽象出来的类,不能进行实例化
2:抽象类中一般要有抽象方法,即纯虚函数 vitural void fun() const = 0; 
3:抽象方法(纯虚函数在抽象类中只能进行声明,却不能定义)
4:继承抽象类的子类必须要定义抽象类,当然可以空定义
5:抽象类是类,类的特点就是属性和方法(变量和函数)

接口(interface)

接口是抽象方法的集合,当然这类抽象方法的集合也是有规律的.

/*
 * ===========================================================================
 *
 *       Filename:  interface.h
 *    Description:  
 *        Version:  1.0
 *        Created:  2017年06月25日 16时07分46秒
 *       Revision:  none
 *       Compiler:  gcc
 *         Author:   (), 
 *        Company:  
 *
 * ===========================================================================
 */

#ifndef __INTERFACE_H__
#define __INTERFACE_H__

/* *
 *定义一个接口,这个接口里面仅仅包含的是抽象方法(纯虚函数),接口没有构造函数与析构函数
 * */
class IComputer
{  
  public:
    //要给接口添加一个虚的析构函数,否则子类无法析构
    virtual ~IComputer(){

    }
    virtual void playMedia() const = 0;
    virtual void playGame() const = 0;
};

/* *
 *DesktopComputer 实现IComputer,当然在C++中,也暂且叫继承吧
 * */
class DesktopComputer : public IComputer
{
  public:
    DesktopComputer();
    ~DesktopComputer();

    void playGame() const;
    void playMedia() const;

};

/* *
 *NetBookComputer 继承抽象类 IComputer
 * */
class NetBookComputer : public IComputer
{
  public:
    NetBookComputer();
    ~NetBookComputer();

    void playGame() const;
    void playMedia() const;
  private:
};

/* *
 *PanelComputer 继承IComputer
 * */
class PanelComputer : public IComputer
{
  public:
    PanelComputer();
    ~PanelComputer();

    void playGame() const;
    void playMedia() const;

};

#endif
/*
 * ===========================================================================
 *
 *       Filename:  abstractor.cpp
 *    Description:  
 *        Version:  1.0
 *        Created:  2017年06月25日 16时20分35秒
 *       Revision:  none
 *       Compiler:  gcc
 *         Author:   (), 
 *        Company:  
 *
 * ===========================================================================
 */

#include<iostream>
#include"interface.h"
using namespace::std;

/* *
 *注意:
 在函数进行定义的时候,是可以不用讲virtural关键字写上的
 * */



DesktopComputer::DesktopComputer()
{
  cout<<"DesktopComputer constructor"<<endl;
}

DesktopComputer::~DesktopComputer()
{
  cout<<"DesktopComputer destructor"<<endl;
}

void DesktopComputer::playGame() const{
  cout<<"DesktopComputer playGame"<<endl;
}

void DesktopComputer::playMedia() const{
  cout<<"DesktopComputer playMedia"<<endl;
}

NetBookComputer::NetBookComputer()
{
  cout<<"NetBookComputer constructor"<<endl;
}


NetBookComputer::~NetBookComputer()
{
  cout<<"NetBookComputer destructor"<<endl;
}


void NetBookComputer::playGame() const{
  cout<<"NetBookComputer playGame"<<endl;
}

void NetBookComputer::playMedia() const{
  cout<<"NetBookComputer playMedia"<<endl;
}

PanelComputer::PanelComputer()
{
  cout<<"PanelComputer constructor"<<endl;
}

PanelComputer::~PanelComputer()
{
  cout<<"PanelComputer destructor"<<endl;
}



void PanelComputer::playGame() const{
  cout<<"PanelComputer playGame"<<endl;
}

void PanelComputer::playMedia() const{
  cout<<"PanelComputer playMedia"<<endl;
}
/*
 * ===========================================================================
 *
 *       Filename:  abstractorTest.cpp
 *    Description:  
 *        Version:  1.0
 *        Created:  2017年06月25日 16时31分39秒
 *       Revision:  none
 *       Compiler:  gcc
 *         Author:   (), 
 *        Company:  
 *
 * ===========================================================================
 */

#include<iostream>
#include"interface.h"
using namespace::std;



int main(int argc ,char* argv[]){

  //Computer *mComputer = new Computer();
  //编译的时候报错,主要是因为抽象类不能实例化

  //依赖抽象而不去依赖具体,父类的指针指向子类的对象
  IComputer* desktopComputer = new DesktopComputer();
  desktopComputer->playMedia();
  desktopComputer->playGame();
  cout<<"----------------------"<<endl;

  IComputer* netBookComputer = new NetBookComputer();
  netBookComputer->playMedia();
  netBookComputer->playGame();
  cout<<"----------------------"<<endl;
  IComputer* panelComputer = new PanelComputer();

  panelComputer->playGame();
  panelComputer->playMedia();

  cout<<"----------------------"<<endl;


  delete desktopComputer;
  delete netBookComputer;
  delete panelComputer;
  return 0;
}

总结:(个人总结,不喜勿喷)

抽象类的总结:
1:带有纯虚函数的类叫做抽象类(注意与接口区分)
2:抽象类只能作为其他类的基类,处于继承层次的上层
3:抽象类不能用作参数类型,函数返回类型,或者显示转换(抽象类不能够进行实例化)
4:可以定义抽象类的指针和引用,此指针指向它的派生类,
5:抽象类一般既包括成员函数,又包括数据成员变量

接口总结:
1:接口中没有成员变量,只有虚的析构函数与纯虚函数
2:接口同抽象类一样也是不能进行实例化的
3:抽象类不能用作参数类型,函数返回类型,或者显示转换(抽象类不能够进行实例化)
4:可以定义抽象类的指针和引用,此指针指向它的派生类,

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值