继承和多态性
因此,我现在正在 C++ 类中学习继承,并尝试阐明所有术语。我了解受保护和私有之间的区别。但是,当你将某个功能设为好友时,这不就等于将其公开吗?
他们用来教授继承的例子是形状,所以:
class shape{
struct circle
struct rectangle
struct line
}
像这样,说和最后有什么区别
class circle: shape
class circle: public shape
class circle: private shape (don't actually know if this one is possible)
,关键字 virtual 意味着/做什么/用于什么?谢谢
So I'm learning about inheritance in my c++ class now and trying to put all the terminology to light. I understand the difference between protected and private. But when you make a function a friend isn't that the same thing as making it public?
The examples they are using to teach inheritance is shapes, so:
class shape{
struct circle
struct rectangle
struct line
}
Something like that, what is the difference between saying
class circle: shape
class circle: public shape
class circle: private shape (don't actually know if this one is possible)
And finally, what does the keyword virtual mean/do/used for? Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先也是最重要的,你应该拿起一本好的 C++ 书籍并阅读其中的概念:
The Definitive C++ 书籍和指南列表
为了让您开始简要介绍您的问题:
您的第一个示例是组合而不是继承的示例.
继承是IS关系。
组合是HAS关系。
在示例中,
Circle
是Shape
的一种类型。等同于:
对于类,默认访问说明符默认为
Private
。这就是私有继承。circle
类私有地派生自shape
类。是公有继承,类
circle
公开派生自类shape
。此 C++-常见问题解答应该是了解基础知识的好读物:
什么是访问说明符?我应该继承私有的、受保护的还是公共的?
但是当你将一个函数设为友元时,这不是与将其公开为同一件事吗?
当您将函数设置为类的
friend
时,访问说明符不再适用于该函数。该函数可以访问该类的protected
以及private
成员。但这仅限于该函数。关键字
virtual
的含义/用途/用途是什么?关键字
virtual
用于实现动态/运行时多态性。这是一个需要解释的广泛术语,因此最重要的是您从书中阅读并理解这个概念。如果您在理解任何具体内容时仍然遇到问题,请返回此处并随时在此处提出具体问题。
First and foremost you should pick up a good C++ book and read the concepts:
The Definitive C++ book and guide list
To get you started little brief about your Questions:
Your first example is an example of Composition not Inheritance.
Inheritance is IS A relationship.
Composition is HAS A relationship.
In the example,
Circle
IS A type ofShape
.is same as:
For a class, Default Access specifier is
Private
by default. This is Private Inheritance. Classcircle
privately derives from classshape
.Is Public Inheritance, Class
circle
publically derives from classshape
.This C++-Faq should be a good read for understanding the basics:
What are access specifiers? Should I inherit with private, protected or public?
But when you make a function a friend isn't that the same thing as making it public?
When you make a function as
friend
of an class, Access specifiers no longer apply to that function. That function can access,protected
as well asprivate
members of that class.But this is only limited to that function.what does the keyword
virtual
mean/do/used for?The keyword
virtual
is used to implement Dynamic/Run-Time Polymorphism.It is an broad term to be explained as such, So it is upmost important that you read and understand the concept from an book. If you still face problems in understanding anything specific about it,Come back here and feel free to ask an Specific Question here.
是允许程序员创建一个新类的过程,该新类是现有类的特化,然后,新类称为现有类的派生类,现有类称为新创建类的基类。
Is the process that allows programmers to create a new class that is a specialization of the existing class, and then, the new class is called the Derived class of the existing class and the existing class is called the base of the new created class.
继承是指新的类接管旧的现有类,多态是指在程序中引用某个类作为新引入的类的基础。
Inheritance is when a new class take over the old existing class and Polymorphism is when you refer a certain class in program as a base of the new introduced class.