关于继承及其概念 [英] About Inheritance and its concepts

查看:52
本文介绍了关于继承及其概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

在c ++继承的上下文中,我在课堂上教过每个

派生类都是部分基类内容 - 部分新内容 ;。

此外,我被告知基类的构造函数用于初始化基类内容和
派生类构造函数

用于初始化新内容。


好​​的,这里是我困惑的地方。如果派生类是部分基础

内容 - 部分新内容,则关键字protected由于部分基类包含在派生的

类中,除了受保护之外,因此不需要
。机制。除非

公共数据成员和函数未包含在派生的

类中,否则这将是真的。那将意味着部分基类内容。只有

包含受保护的数据成员和受保护的基本功能

类。


如果这是真的,它会使没有意义使用基础

类的构造函数来初始化部分基类内容由于

这些内容中的某些内容可能不公开,因此不会被派生类继承的
因此不需要初始化



任何人都可以对此进行清理吗?

另外,关于受保护的关键字,在什么情况下我会使用

受保护的函数而不是非纯虚函数?

Hi all.
In the context of inheritance in c++, I was taught in class that every
derived class is "part base class content - part new content".
Moreover, I was taught that the constructor of the base class is used
to initialize base class content and that the derived class constructor
is used to initialize the new contents.

Ok, here is where I''m confused. If the derived class is part base
content - part new content, then the keyword "protected" becomes
unnecessary, since part of the base class is included in the derived
class apart from the "protected" mechanism. This would be true unless
public data members and functions are not included in the derived
class. Then that would mean that the "part base class content" only
contains protected data members and protected functions of the base
class.

If that were true though, it would make no sense to use the base
class''s constructor to initialize the "part base class content" since
some of this content might not be public and would then not be
inherited by the derived class and would therefore not require
initialization.

Can anyone clear me up on this?
Also, regarding the "protected" keyword, in what case would I use
protected functions rather than non-pure virtual functions?

推荐答案

注意:替换所有公共的出现通过公共和私人在上面的

帖子中。

note: replace all occurences of "public" by "public and private" in the
post above.


" p _ **** @ encs.concordia.ca" < pa ************ @ gmail.comwrote in message

news:11 ****************** ***@m73g2000cwd.googlegro ups.com
"p_****@encs.concordia.ca" <pa************@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com

大家好。

在c ++继承的上下文中,我在课堂上教过每个

派生类都是部分基类内容 - 部分新内容。

此外,我被教导使用基类的构造函数

初始化基类内容,派生类

构造函数用于初始化新内容。


好​​的,这里是我的地方我很困惑。如果派生类是部分基础

内容 - 部分新内容,则关键字protected由于部分基类包含在派生的

类中,除了受保护之外,因此不需要
。机制。除非

公共数据成员和函数未包含在派生的

类中,否则这将是真的。那将意味着部分基类内容。只有

包含受保护的数据成员和受保护的基本功能

类。


如果这是真的,它会使没有意义使用基础

类的构造函数来初始化部分基类内容由于

这些内容中的某些内容可能不公开,因此不会被派生类继承的
因此不需要初始化



有人可以对此进行清理吗?


此外,关于受保护的关键字,在什么情况下我会使用

受保护的函数而不是非纯虚函数?
Hi all.
In the context of inheritance in c++, I was taught in class that every
derived class is "part base class content - part new content".
Moreover, I was taught that the constructor of the base class is used
to initialize base class content and that the derived class
constructor is used to initialize the new contents.

Ok, here is where I''m confused. If the derived class is part base
content - part new content, then the keyword "protected" becomes
unnecessary, since part of the base class is included in the derived
class apart from the "protected" mechanism. This would be true unless
public data members and functions are not included in the derived
class. Then that would mean that the "part base class content" only
contains protected data members and protected functions of the base
class.

If that were true though, it would make no sense to use the base
class''s constructor to initialize the "part base class content" since
some of this content might not be public and would then not be
inherited by the derived class and would therefore not require
initialization.

Can anyone clear me up on this?
Also, regarding the "protected" keyword, in what case would I use
protected functions rather than non-pure virtual functions?



*所有*基类内容都成为任何派生类对象的一部分。

但是,并非所有基类内容都可能可以访问派生的

类函数(或类可以访问

派生对象的类外)。公共,私人和受保护的政府可以访问什么,

不包含的内容。


-

John Carson


John,这是有道理的。

所以,澄清:

1)我的错误是假设受保护的关键字表示

包含在派生类中。

2)基类的所有数据成员和函数都包含在

派生类中,无论他们的知名度如何。它们包含在

可见性中。


出现一个新问题:

如果声明了数据成员(类变量)作为私有的基础

类,当基类内容被复制到子类中时,那么

如何应用这些可见性?


使用下面的例子,


******************************



******************************

protected string lastName

private string firstName

公共字符串国籍

************ ******************


******************** *****************

教授(一般人)

************ *************************

私人字符串部门

****** ***************************************


教授会反对吗? "&的firstName QUOT;变量对他来说是私有的吗?那么

受保护关键字的目的是什么?


John Carson写道:
John, that makes sense.
So, to clarify:
1) My mistake was in assuming that the protected keyword indicates what
is included in a derived class.
2) All data members and functions of the base class are included in the
derived class, no matter their visibility. They are included with their
visibility.

A new question arises:
If a data member (class variable) is declared as private in the base
class, when the base class content is copied into the subclass, then
how do the visibilities apply?

Using the below example,

******************************
Person
******************************
protected string lastName
private string firstName
public string nationality
******************************

*************************************
Professor (Generalizes Person)
*************************************
private string department
*************************************

Will a Professor object''s "firstName" variable be private to him? Then
what is the purpose of the protected keyword?

John Carson wrote:

" p _ **** @ encs.concordia.ca" < pa ************ @ gmail.comwrote in message

news:11 ****************** ***@m73g2000cwd.googlegro ups.com
"p_****@encs.concordia.ca" <pa************@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com

大家好。

在c ++继承的上下文中,我在课堂上教过每个

派生类都是部分基类内容 - 部分新内容。

此外,我被教导使用基类的构造函数

初始化基类内容,派生类

构造函数用于初始化新内容。


好​​的,这里是我的地方我很困惑。如果派生类是部分基础

内容 - 部分新内容,则关键字protected由于部分基类包含在派生的

类中,除了受保护之外,因此不需要
。机制。除非

公共数据成员和函数未包含在派生的

类中,否则这将是真的。那将意味着部分基类内容。只有

包含受保护的数据成员和受保护的基本功能

类。


如果这是真的,它会使没有意义使用基础

类的构造函数来初始化部分基类内容由于

这些内容中的某些内容可能不公开,因此不会被派生类继承的
因此不需要初始化



任何人都可以对此进行清理吗?

另外,关于受保护的关键字,在什么情况下我会使用

受保护的函数而不是非纯虚函数?
Hi all.
In the context of inheritance in c++, I was taught in class that every
derived class is "part base class content - part new content".
Moreover, I was taught that the constructor of the base class is used
to initialize base class content and that the derived class
constructor is used to initialize the new contents.

Ok, here is where I''m confused. If the derived class is part base
content - part new content, then the keyword "protected" becomes
unnecessary, since part of the base class is included in the derived
class apart from the "protected" mechanism. This would be true unless
public data members and functions are not included in the derived
class. Then that would mean that the "part base class content" only
contains protected data members and protected functions of the base
class.

If that were true though, it would make no sense to use the base
class''s constructor to initialize the "part base class content" since
some of this content might not be public and would then not be
inherited by the derived class and would therefore not require
initialization.

Can anyone clear me up on this?
Also, regarding the "protected" keyword, in what case would I use
protected functions rather than non-pure virtual functions?



*所有*基类内容都成为任何派生类对象的一部分。

但是,并非所有基类内容都可以可以访问派生的

类函数(或类可以访问

派生对象的类外)。公共,私人和受保护的政府可以访问什么,

不包含的内容。


-

John Carson


*All* of the base class content becomes part of any derived class object.
However, not all of the base class content may be accessible to derived
class functions (or to functions outside the class that have access to a
derived object). Public, private and protected govern what is accessible,
not what is included.

--
John Carson


这篇关于关于继承及其概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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