如何初始化继承的值,并拥有抽象方法 C++?

huangapple go评论97阅读模式
英文:

How to initialize inherited values, and have abstract methods c++?

问题

这有点像一个问题中的多个问题。首先,C++ 中有没有一种方法可以初始化继承的值?换句话说,这就是我在 Java 中的意思:

  1. // Class X
  2. class X {
  3. public:
  4. int num;
  5. X(int num) : num(num) {}
  6. };
  7. // Class Y
  8. class Y : public X {
  9. public:
  10. Y() : X(5) {}
  11. };

我的另一个问题是,我如何在 C++ 中创建一个抽象方法?同样,Java 示例:

  1. // Class X
  2. class X {
  3. public:
  4. int num;
  5. X(int num) : num(num) {}
  6. virtual void use() = 0;
  7. };
  8. // Class Y
  9. class Y : public X {
  10. public:
  11. Y() : X(5) {}
  12. void use() override {
  13. }
  14. };

谢谢。

英文:

This is kinda multiple questions in a single question. First off, is there a way to initialize inherited values in C++? In other words, this is what I mean in java :

  1. // Class X
  2. public class X {
  3. int num;
  4. public X(int num) {
  5. this.num = num;
  6. }
  7. }
  8. //Class Y
  9. public class Y extends X{
  10. public Y() {
  11. super(5);
  12. }
  13. }

My other question is how would I create an abstract method in C++?
Again, java example :

  1. // Class x
  2. public abstract class X {
  3. int num;
  4. public X(int num) {
  5. this.num = num;
  6. }
  7. public abstract void use();
  8. }
  9. // Class y
  10. public class Y extends X{
  11. public Y() {
  12. super(5);
  13. }
  14. @Override
  15. public void use() {
  16. }
  17. }

Thanks.

答案1

得分: 2

首先:您想了解初始化列表。对于您的第一个代码片段,适当的代码应为

  1. class X {
  2. int num;
  3. public:
  4. X(int t_num) : num(t_num) { } // 我们可以使用初始化列表来设置成员变量
  5. };
  6. class Y : public X {
  7. public:
  8. Y() : X(5) { } // 在构造函数的主体中不需要有任何内容
  9. };

对于第二个问题,在C++中,您可以在不提供定义的情况下声明函数,这与Java中定义抽象函数非常相似。如果您的目的是进行运行时多态性,您还需要将它们声明为虚函数

  1. class X {
  2. int num;
  3. public:
  4. virtual void use() = 0; // = 0 表示在X中没有实现
  5. };
  6. class Y : public X {
  7. void use() override { } // 不需要重新声明虚函数
  8. // override 关键字不是必需的,但可以澄清语义
  9. };
英文:

First: You want to learn about initializer lists. For your first code snippet, the appropriate code would be

  1. class X {
  2. int num;
  3. public:
  4. X(int t_num) : num(t_num) { } // We can use initializer lists to set member variables
  5. };
  6. class Y : public X {
  7. public:
  8. Y() : X(5) { } // No need to have anything in the body of the constructor
  9. };

For the second, in C++ you can always declare functions without providing a definition, which is very similar to defining an abstract function in Java. If your intent is to do run-time polymorphism, you'll also want to make them virtual functions:

  1. class X {
  2. int num;
  3. public:
  4. virtual void use() = 0; // The = 0 indicates there's no implementation in X
  5. };
  6. class Y : public X {
  7. void use() override { } // No need to redeclare virtual
  8. // override keyword not necessary but can be clarifying
  9. };

huangapple
  • 本文由 发表于 2020年10月7日 07:34:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64235178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定