我有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) 我有一个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都是可访问的.我知道这是一个微不足道的问题,但我在这里遇到了障碍.
我希望在基类(和接口)上定义一个方法,该方法接受派生类作为其参数。
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)
如果有人能告诉我界面是什么样子以及如何做到这一点......或者如果可能的话,我将永远感激不已
满怀期待
槊
有什么办法可以让我derived class访问 的private成员,base class同时能够derived在base 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) 因此,在我的代码中,我尝试将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) 假设我有一个抽象类
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) 初始化从其基类中转换的派生类的首选方法是什么?
请考虑以下情形:
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()函数,该函数将像构造函数那样执行必要的初始化.
虽然看起来很草率,但必须有一个更好/更清洁的方法.
非常感谢任何帮助和指导!
我有父类:
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的基本原理,但要坚持到这里.
我希望我的类实现一个接口,并从Auditable表中获取其他属性.我可以两个都做吗?我试图在这里做,但我的IDE中出现错误.
public partial class ObjectiveDetail : IEquatable<ObjectiveDetail>, AuditableTable
{
...
}
public abstract class AuditableTable : IAuditableTable
{
...
}
Run Code Online (Sandbox Code Playgroud) 我在编写一些代码时遇到困难。我不太确定在哪里以及如何编写构造函数和访问器。
\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\nusing 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) 我有一个特殊的要求,但无法找到解决方案.
class Base
{
public:
void func()
{
//access the member say 'var' of derived class
}
}
Run Code Online (Sandbox Code Playgroud)
derived-class ×11
inheritance ×8
c++ ×6
c# ×5
base-class ×2
.net ×1
arrays ×1
c++11 ×1
class ×1
constructor ×1
generics ×1
initializing ×1
interface ×1
oop ×1
stdvector ×1
unique-ptr ×1