继承和多态性

发布于 2024-12-12 20:45:42 字数 405 浏览 1 评论 0原文

因此,我现在正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野稚 2024-12-19 20:45:44

首先也是最重要的,你应该拿起一本好的 C++ 书籍并阅读其中的概念:

The Definitive C++ 书籍和指南列表


为了让您开始简要介绍您的问题:

您的第一个示例是组合而不是继承的示例.
继承是IS关系。
组合是HAS关系。

在示例中,Circle Shape 的一种类型。


class circle: shape 

等同于:

class circle: private shape 

对于类,默认访问说明符默认为 Private。这就是私有继承circle 类私有地派生自 shape 类。

在私有继承中,基类的所有公共和受保护成员都成为派生类的私有成员。

class circle: public 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 of Shape.


class circle: shape 

is same as:

class circle: private shape 

For a class, Default Access specifier is Private by default. This is Private Inheritance. Class circle privately derives from class shape.

In Private Inheritance all the public and protected members of the Base class become Private members of the Derived class.

class circle: public shape 

Is Public Inheritance, Class circle publically derives from class shape.

In Public Inheritance the public members of Base class become Public members of Derived class and protected members of the Base class become Protected members of the Derived class.

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 as private 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.

坠似风落 2024-12-19 20:45:44

是允许程序员创建一个新类的过程,该新类是现有类的特化,然后,新类称为现有类的派生类,现有类称为新创建类的基类。

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.

三生池水覆流年 2024-12-19 20:45:44

继承是指新的类接管旧的现有类,多态是指在程序中引用某个类作为新引入的类的基础。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文