温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

继承抽象类

发布时间:2020-05-17 09:39:15 来源:网络 阅读:315 作者:神迹难觅 栏目:编程语言

#ifndef VIRTUAL1

#define VIRTUAL1

#include<iostream>

using namespace std;

class Number{

public:

Number(int i){ x = i; }

virtual void show() = 0;

protected:

int x;

};

class dec_type :public Number{//这里必须公有继承,否则派生类对象做实参无法传递给基类的

//引用对象。

public:

dec_type(int i) :Number(i){}

void show(){

cout << dec << x<<endl;

}

};

class hex_type:public Number{

public:

hex_type(int i) :Number(i){}

void show(){

cout << hex << x<<endl;

}

};

class oct_type :public Number{

public:

oct_type(int i) :Number(i){}

void show(){

cout << oct << x<<endl;

}

};

#endif


#include"vitual1.h"

void fun(Number &n){//抽象类可以做引用

n.show();

}

int main(){

oct_type oc(50);

fun(oc);//派生类对象做参数传给基类的引用

system("pause");

return 0;

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI