标签: derived-class

C++:友元函数,派生类

我有2个类,基类是"Port",派生类是"VintagePort".据我所知,如果我使用基类的引用或指针到派生类的对象,它会自动找到正确的方法,而不是引用或指针,但完全对象(如果方法是虚拟的).

在我的情况下,你可以看到这两个类都有友元函数"operator <<".但是看起来当我使用指针作为基类时,它只从基类调用函数.如果我使用"cout << VintagePort"它可以正常工作.我的问题:它是否正常工作或我应该在代码中修复一些东西?

std::ostream& operator<<(std::ostream& os, const Port& p)
{
os << p.brand << ", " << p.style << ", " << p.bottles << endl;
return os;
}

std::ostream& operator<<(std::ostream& os, const VintagePort& vp)
{
os << (const Port &) vp;
cout << ", " << vp.nickname << ", " << vp.year << endl;
return os;
}




VintagePort vp1;
VintagePort vp2("Gallo", "lekko brazowy", 50, "Blaze", 1990);
VintagePort vp3(vp2);

Port* arr[3];
arr[0] = &vp1;
arr[1] = &vp2;
arr[2] = …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance derived-class

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

无法访问派生类中的公共属性

我有一个abstract class由另一个类继承的如下:

public abstract class Employee
{
        public string name{ get; set; }
        public string age { get; set; }
}

public class OtherEmployee : Employee
    {
        public OtherEmployee()
        {
        }

        public string specialField{ get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

这就是我正在做的事情,不知怎的,没有完成它:

Employee otherEmployee= new OtherEmployee();
otherEmployee.specialField = "somevalue";
Run Code Online (Sandbox Code Playgroud)

我没有获得访问权限specialField,而所有属性Employee都是可访问的.我知道这是一个微不足道的问题,但我在这里遇到了障碍.

c# inheritance abstract-class derived-class

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

C# 基类/接口,具有接受派生类作为参数的通用方法

我希望在基类(和接口)上定义一个方法,该方法接受派生类作为其参数。

IE

abstract class Base : IBase
{
    public void CloneMeToProvidedEntity(??? destination) {};
}

public class Derived : Base
{
     public override void CloneMeToProvidedEntity(Derived destination)
     {
         blah blah ....
     }
}  
Run Code Online (Sandbox Code Playgroud)

如果有人能告诉我界面是什么样子以及如何做到这一点......或者如果可能的话,我将永远感激不已

满怀期待

c# generics inheritance derived-class

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

C++如何创建创建派生类对象的基类,并访问基类的私有成员?

有什么办法可以让我derived class访问 的private成员,base class同时能够derivedbase class内部的方法上创建对象base class

像这样的东西:

class Derived;

class Base
{
public:
    void func()
    {
        // I want to create the derived obj here
        Derived derived;
        derived.func();
    }
public:
    int mBase = 5;
};

class Derived : public Base
{
public:
    void func()
    {
        // I need to have access to the private members of Base inside this method
        mBase = 6; 
    }
};
Run Code Online (Sandbox Code Playgroud)

错误如下:

Error: derived …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance base-class derived-class

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

将派生类对象添加到基类的向量&lt;unique_ptr&gt;

因此,在我的代码中,我尝试将unique_ptr对象添加到从derived类到vector基类的对象。我收到此错误:

E0304 没有重载函数的实例“std::vector<_Ty, _Alloc>::push_back [with _Ty=std::unique_ptr<Organism, std::default_delete<Organism>>, _Alloc=std::allocator<std::unique_ptr <Organism, std::default_delete<Organism>>>]" 与参数列表匹配

基类的代码(如果您需要更多,请告诉我,尽量少写代码):

vector<unique_ptr<Organism>>  World::generate_organisms(int act_level)
{
    vector<unique_ptr<Organism>> organism_list = get_vector();
    coordinates sheep_pos(10, 2);
    //getting error in next line
    organism_list.push_back(make_unique<Sheep>(sheep_pos, *this));

    return organism_list;
}
Run Code Online (Sandbox Code Playgroud)

派生类的代码:

.h文件

class Sheep : Organism
{
    Sheep( coordinates organism_pos, World* world);
};
Run Code Online (Sandbox Code Playgroud)

.cpp文件

Sheep::Sheep( coordinates organism_pos, World* act_world)
    :
    Organism(organism_pos, act_world)
{
    this->armor = 0;
    this->damage = 2;
    this->health = 10;
    this->vigor = 10;
}
Run Code Online (Sandbox Code Playgroud)

c++ inheritance derived-class unique-ptr c++11

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

创建派生类的 std::vector

假设我有一个抽象类

class AbstractClass {
public:
    virtual int get() const = 0;
};
Run Code Online (Sandbox Code Playgroud)

和两个不同的派生类

class DerivedClassA : public AbstractClass {
public:
    int get() const override { return 1; }
};

class DerivedClassB : public AbstractClass {
public:    
    int get() const override { return 2; }
};
Run Code Online (Sandbox Code Playgroud)

我想将std::vectorAbstract Classed 传递给给定的函数:

int f(const std::vector<std::shared_ptr<AbstractClass> >& classes) { ... }
Run Code Online (Sandbox Code Playgroud)

我正在做的是这样的:

int main () {
    std::vector<std::shared_ptr<AbstractClass> > _classes;
    std::shared_ptr<AbstractClass> _derivedA = std::make_shared<DerivedClassA>();
    _classes.push_back(_derivedA);
    std::shared_ptr<AbstractClass> _derivedB = std::make_shared<DerivedClassB>();
    _classes.push_back(_derivedB);
    std::cout << f(_classes) << …
Run Code Online (Sandbox Code Playgroud)

c++ derived-class stdvector

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

初始化派生类的成员(C++)

初始化从其基类中转换的派生类的首选方法是什么?

请考虑以下情形:

    class A{
        public:
           A();
           ~A();
    }

    class B : public A{
        public:
           B() {m_b = 0.0;};
           ~B();
           float GetValue(){return m_b;};

        private: 
           float m_b;
    }


    A* a = new A;
    B* b = static_cast<B*>(a);

    float val = b->GetValue();   // This was never initialized because it was not constructed
Run Code Online (Sandbox Code Playgroud)

我目前的解决方案是手动调用Initialize()函数,该函数将像构造函数那样执行必要的初始化.

虽然看起来很草率,但必须有一个更好/更清洁的方法.

非常感谢任何帮助和指导!

c++ constructor derived-class initializing

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

为什么受保护的成员只能通过派生类的方法访问

我有父类:

class clsTestParent
    {
        protected int x;

        public void Foo()
        {
            x = 10;
        }

    }
Run Code Online (Sandbox Code Playgroud)

我有Derrived Class如下:

class clsDerivedTest : clsTestParent
    {

            x = 10;
            Foo();

    }
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为我遇到两个错误:

类,结构或接口成员声明中的标记'='无效
方法必须具有返回类型

但是当我尝试将它们与派生类中的方法一起使用时,上述语句工作正常,如下所示:

 class clsDerivedTest : clsTestParent
    {


        public void myTestMethod()
        {
            x = 10;
            Foo();
        }
}
Run Code Online (Sandbox Code Playgroud)

为什么受保护的var或方法只能通过使用派生类方法访问,但不能在类中直接访问.

我甚至尝试通过创建对象来访问受保护的成员,如下所示:

clsDerivedTest objDerivedTest = new clsDerivedTest();
            objDerivedTest.x = 10;
Run Code Online (Sandbox Code Playgroud)

但是再次获得保护级别的错误.我有var作为保护,所以为什么派生类的对象不能访问它?

我需要清除OOP的基本原理,但要坚持到这里.

.net c# oop inheritance derived-class

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

如何使类既实现接口又从其他类继承

我希望我的类实现一个接口,并从Auditable表中获取其他属性.我可以两个都做吗?我试图在这里做,但我的IDE中出现错误.

public partial class ObjectiveDetail : IEquatable<ObjectiveDetail>, AuditableTable
{
    ...
}

public abstract class AuditableTable : IAuditableTable
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

c# interface derived-class interface-implementation

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

车辆对象数组 - C#

我在编写一些代码时遇到困难。我不太确定在哪里以及如何编写构造函数和访问器。

\n\n

我要做的活动是这样的:

\n\n

编写 3 个派生类以允许用户输入三种类型车辆及其属性的详细信息。

\n\n

\xe2\x80\xa2 汽车(品牌、型号、年份、车身类型)

\n\n

\xe2\x80\xa2 飞机(制造商、型号、年份、noEngines、发动机类型)

\n\n

\xe2\x80\xa2 船(品牌、型号、年份、长度、船体类型)

\n\n

第四类是基类Vehicle,包含共享的属性和方法

\n\n

将所有属性设置为私有(在派生类中)或受保护(在基类中),并为每个属性编写访问器方法。

\n\n

为每个派生类编写 2 个构造函数。一种不带参数,另一种接受派生类中的属性值作为参数。

\n\n

编写一个名为 Fleet.cs 的控制台应用程序,它创建并显示每种车辆类型 2 个

\n\n

到目前为止我的代码如下:

\n\n
using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\n\nnamespace ConsoleApplication5\n{\n    class Vehicle\n    {\n        static void Main(string[] args)\n        {\n        }\n\n        class Car\n        {\n            protected string make\n            {\n                get\n                {\n                    return make;\n                }\n                set\n                {\n                    make = value;\n                }\n            }\n\n            protected string model\n            {\n                get\n                {\n                    return model;\n                }\n                set\n                {\n                    model …
Run Code Online (Sandbox Code Playgroud)

c# arrays inheritance derived-class

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

在基类方法中访问派生类成员

我有一个特殊的要求,但无法找到解决方案.

class Base
{
public:
    void func()
    {
       //access the member say 'var' of derived class
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 在我们的例子中强制要求所有来自base的派生类都有成员'var'.
  2. 派生类的名称可以是任何名称.

c++ inheritance class base-class derived-class

-3
推荐指数
2
解决办法
1847
查看次数