相关疑难解决方法(0)

如何实现基类的虚方法,并在c#中的覆盖方法中获取基方法实现

我有一个抽象类A,它有部分实现的虚方法,它在类B中继承.我想在派生类中实现虚方法,我应该如何在派生类中获得基本实现.例如.

public abstract class A
{
    public int Sum { set; get; }
    public virtual void add(int a, int b)
    {
        Sum = a + b;
    }
}
class B : A
{
    public override void add(int a, int b)
    {
        // what should i write here
    }
}
Run Code Online (Sandbox Code Playgroud)

c#

3
推荐指数
1
解决办法
795
查看次数

在多重继承中从派生类函数调用适当的父类函数

假设我有以下类:

class Base {
  virtual void func() { cout << "func base" << endl; }
};

class A : virtual public Base {
  public:
  virtual void func() { cout << "func A" << endl; }
};

class B : virtual public Base {
  public:
  virtual void func() { cout << "func B" << endl; }
};

class C : public A, public B {
  C(bool is_a);
  public:
  virtual void func() { // Call appropriate parent's func here }
};
Run Code Online (Sandbox Code Playgroud)

我的要求是 …

c++ inheritance multiple-inheritance

3
推荐指数
1
解决办法
77
查看次数

如何正确地重载流操作符以在多态类中打印?

编辑:代码有一个错字,现在它编译但我仍然没有得到我想要的输出.

我试图重载流运营商std::cout,std::fstream等等,但我不能够正确处理多态性:我无法得到我想要看到的输出.我希望子类显示超类的内容,然后显示其内容,而超类只显示其内容.这可以通过在函数print_only_base()中引入一个新函数来实现Base,但我怀疑即使不添加新函数我也可以使代码工作.

我想得到什么

我有一个基类,它具有一些我希望在屏幕上显示的属性

Base base;
std::cout << base;
Run Code Online (Sandbox Code Playgroud)

有两类,AB从它继承Base.Base是一个多态类,AB在流式传输时显示不同的输出.

我希望类的对象Base只显示其输出,而类的对象A(相同的用于B)首先显示输出本身(如果将其视为实例)Base,然后将其输出视为A(或B).

在代码下面,我写了我期望的输出和我的(不工作)尝试.

#include <iostream>

class Base {
    virtual std::ostream & print(std::ostream stream) const {
        stream << "Base class output\n";
        return stream;
    }
public:
    friend std::ostream & operator<<(std::ostream & stream, const Base & obj) {
        return obj.print(stream);
    }
    virtual …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism operator-overloading

2
推荐指数
1
解决办法
65
查看次数

使用C++,如何从派生类方法调用基类方法并将其应用于作为参数传递的对象?

我无法弄清楚如何从派生类方法调用基类方法,但同时在作为参数传递的对象上应用此方法调用.

我的意思是:

class Animal
{ 
    virtual void eat(Animal& to_be_eaten) = 0;
 };

class Carnivores: public Animal
{ 
    virtual void eat(Animal& to_be_eaten) { /*implementation here*/}

};

class Wolf : public Carnivores
{ 
    virtual void eat(Animal& to_be_eaten)
    { /*call eat method(of Base class) of Base to_be_eaten here*/ }
 }
Run Code Online (Sandbox Code Playgroud)

我想到了这样的事情

 dynamic_cast<Carnivores&>(to_be_eaten).eat(*this) //and got a segmentation fault
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

谢谢!

新编辑::更新了代码

c++ virtual inheritance

0
推荐指数
1
解决办法
6405
查看次数

如何在c ++中从derive类调用基类方法?

可能重复:
有没有办法调用一个覆盖的对象的基类方法?(C++)

第一个问题是在java中调用super()构造函数与在c ++中首先初始化超类构造函数一样.

sub() : super(){}
Run Code Online (Sandbox Code Playgroud)


有没有办法像在java中一样在c ++中调用超类方法

恩.

public sub(){
super.someMethod(); 
Run Code Online (Sandbox Code Playgroud)

}

c++

0
推荐指数
1
解决办法
172
查看次数

调用超类方法运算符==

我正在经历从Java到C++的过渡,我正在尝试编写一个简单的程序.

有一个超类,Animal具有以下接口:

class Animal
{
public:
    Animal(int a, std::string n);

    bool operator==(Animal a);
private:
    int age;
    std::string name;
};
Run Code Online (Sandbox Code Playgroud)

它的子类Dog:

class Dog : public Animal
{
public:
    Dog(int a, std::string n, double w);

    bool operator==(Dog d);
private:
    double weight;

};
Run Code Online (Sandbox Code Playgroud)

我的问题是关于狗的operator==方法,比较2只狗.

动物operator==在下面.

bool Animal::operator==(Animal a) //overriding of operator ==
{
return (age==a.age) && (name==a.name);
}
Run Code Online (Sandbox Code Playgroud)

现在我想用Animal的方法编写Dog的版本.

就像我用Java做的那样:

boolean equals(Dog d){
    return (super.equals(d)) && (this.name==d.name); 
}
Run Code Online (Sandbox Code Playgroud)

我需要的是c ++相当于(super.equals(d)).如果它是一个具有普通名称的方法,那将很容易(Animal :: equals(d)),但我不知道如何为operator ==执行此操作,它具有不同的语法.

c++ inheritance g++

0
推荐指数
1
解决办法
1563
查看次数

双冒号混淆

为什么我可以在未定义的函数和类中使用双冒号,但不能在变量中使用??

例子:

#include <iostream>

using namespace std;

class Person{

    public:
        int age;
        string name();
};

int Person::age = 10; //It outputs an error

string Person::name(){ //It doesn't
    return "n";
}
Run Code Online (Sandbox Code Playgroud)

c++

-4
推荐指数
1
解决办法
3118
查看次数