更改虚拟功能的访问说明符 [英] Changing access specifier for virtual function

查看:51
本文介绍了更改虚拟功能的访问说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码


#include< iostream>


class Base {

public:

virtual void say(){std :: cout<< "基地" <<的std :: ENDL; }

};


class派生:公共基地{

私人:

无效说( ){std :: cout<< "衍生QUOT; <<的std :: ENDL; }

};


int main(){

派生d;

Base * b =& d;

b->说();

返回0;

}


运行此程序会产生以下输出。


$ ./a.out

派生

$


我有两个问题:

1.以上代码是否合法?

2.如果虚拟功能在Base中是私有的,我可以吗

派生公开?


谢谢

解决方案

< blockquote> ./ a.out

派生





我有2个问题:

1.以上代码是否合法?

2.如果虚拟功能在Base中是私有的,我可以在

Derived中公开吗? br />

谢谢


dragoncoder写道:

考虑以下代码

#include< iostream>

班级Ba se {
public:
virtual void say(){std :: cout<< "基地" <<的std :: ENDL; }
};

类派生:公共基础{
私有:
void say(){std :: cout<< "衍生QUOT; <<的std :: ENDL; }
};

int main(){
Derived d;
Base * b =& d;
b-> say() ;
返回0;
}
运行此程序会产生以下输出。


Consider the following code

#include <iostream>

class Base {
public:
virtual void say() { std::cout << "Base" << std::endl; }
};

class Derived: public base {
private:
void say() { std::cout << "Derived" << std::endl; }
};

int main() {
Derived d;
Base* b = &d;
b->say();
return 0;
}

Running this program produces following output.

$ ./a.out
Derived
$

I have 2 questions:
1. Is the above code legal ?
2. If the virtual function is private in Base, can I make it public in
Derived ?

Thanks

解决方案

./a.out
Derived




I have 2 questions:
1. Is the above code legal ?
2. If the virtual function is private in Base, can I make it public in
Derived ?

Thanks


dragoncoder wrote:

Consider the following code

#include <iostream>

class Base {
public:
virtual void say() { std::cout << "Base" << std::endl; }
};

class Derived: public base {
private:
void say() { std::cout << "Derived" << std::endl; }
};

int main() {
Derived d;
Base* b = &d;
b->say();
return 0;
}

Running this program produces following output.


这篇关于更改虚拟功能的访问说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆