derived-class

What is the difference between a child of a parent class and the derived of a base class in VB.NET or C#?

时光总嘲笑我的痴心妄想 提交于 2019-12-04 03:38:55
After asking the question Call a method that requires a derived class instance typed as base class in VB.NET or C# on Stack Overflow, I was informed that I had used the wrong terms when asking the question. I had used "parent" and "child" where I should have used "base" and "derived" instead. I have been unable to find a good description of the difference. This is what I know (or think I know) so far: A parent class contains the child class. Where as a derived class inherits from a base class. They are similar because the child (or derived) can access the parents (or base) properties and

Why can't we use a constructor with parameter in derived classes

元气小坏坏 提交于 2019-12-04 02:07:35
问题 Why is this not possible? I get the following compiler-error when instantiating "DerivedClass" with a constructor-parameter: 'GenericParameterizedConstructor.DerivedClass' does not contain a constructor that takes 1 argument But calling a very similar method works. Why? class Program { static void Main(string[] args) { // This one produces a compile error // DerivedClass cls = new DerivedClass("Some value"); // This one works; DerivedClass cls2 = new DerivedClass(); cls2.SomeMethod("Some

How to Get Base Class Instance from a Derived Class

主宰稳场 提交于 2019-12-04 00:11:32
I don't know if this is possible, but I am trying to get the Base Class instance from a Derived Class. In C#, I can use the base keyword to access properties and methods of the Base Class (of course), but I want to use base itself. Attempting to do so results in a "Use of keyword 'base' is not valid in this context" error. Example Code public class SuperParent { public int SPID; public SuperParent() { } } public class SubChild : SuperParent { public SubChild(int pSPID) { base.SPID = pSPID; } public int BaseSPID { get { SuperParent sp = base; return sp.SPID; } } } If you're working with an

Error : base class constructor must explicitly initialize parent class constructor

拟墨画扇 提交于 2019-12-03 14:38:58
问题 I am new to c++. When I try to compile the code below , I get this error constructor for 'child' must explicitly initialize the base class 'parent' which does not have a default constructor child::child(int a) { here is my class #include<iostream> using namespace std; class Parent { public : int x; Parent(int a); int getX(); }; Parent::Parent(int a) { x = a; } int Parent::getX() { return x; } class Child : public Parent { public: Child(int a); }; Child::Child(int a) { x = a; } int main(int n

How could an instance of the base class hold an instance of the derived class?

☆樱花仙子☆ 提交于 2019-12-03 12:20:39
I have been a .Net coder (can not say I am a programmer) for 2 years. There is one question that I can not understand for years, that is how could an instance of the base class hold an instance of the derived class? Suppose we have two classes: class BaseClass { public A propertyA; public B propertyB; } class DerivedClass :BaseClass { public C propertyC; } How could this happen: BaseClass obj = new DerivedClass () I mean, the memory model of BaseClass , has no space for the newly added propertyC, so how could it still hold the value of propertyC? On the other side, how could this can not

How do derived class constructors work in python?

╄→尐↘猪︶ㄣ 提交于 2019-12-03 04:48:17
I have the following base class: class NeuralNetworkBase: def __init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs): self.inputLayer = numpy.zeros(shape = (numberOfInputs)) self.hiddenLayer = numpy.zeros(shape = (numberOfHiddenNeurons)) self.outputLayer = numpy.zeros(shape = (numberOfOutputs)) self.hiddenLayerWeights = numpy.zeros(shape = (numberOfInputs, numberOfHiddenNeurons)) self.outputLayerWeights = numpy.zeros(shape = (numberOfHiddenNeurons, numberOfOutputs)) now, I have a derived class with the following code: class NeuralNetworkBackPropagation(NeuralNetworkBase): def _

Error : base class constructor must explicitly initialize parent class constructor

ぐ巨炮叔叔 提交于 2019-12-03 04:27:05
I am new to c++. When I try to compile the code below , I get this error constructor for 'child' must explicitly initialize the base class 'parent' which does not have a default constructor child::child(int a) { here is my class #include<iostream> using namespace std; class Parent { public : int x; Parent(int a); int getX(); }; Parent::Parent(int a) { x = a; } int Parent::getX() { return x; } class Child : public Parent { public: Child(int a); }; Child::Child(int a) { x = a; } int main(int n , char *argv[]) { } Why I am getting this error ? How can I resolve it ? Thanks in advance The parent

Delete virtual function from a derived class

二次信任 提交于 2019-12-03 02:25:39
问题 I have a virtual base class function which should never be used in a particular derived class. Is there a way to 'delete' it? I can of course just give it an empty definition but I would rather make its attempted use throw a compile-time error. The C++11 delete specifier seems like what I would want, but class B { virtual void f(); }; class D : public B { virtual void f() = delete; //Error }; won't compile; gcc, at least, explicitly won't let me delete a function that has a non-deleted base

Why is 'virtual' optional for overridden methods in derived classes?

蹲街弑〆低调 提交于 2019-12-03 02:03:29
When a method is declared as virtual in a class, its overrides in derived classes are automatically considered virtual as well, and the C++ language makes this keyword virtual optional in this case: class Base { virtual void f(); }; class Derived : public Base { void f(); // 'virtual' is optional but implied. }; My question is: What is the rationale for making virtual optional? I know that it is not absolutely necessary for the compiler to be told that, but I would think that developers would benefit if such a constraint was enforced by the compiler. E.g., sometimes when I read others' code I

Delete virtual function from a derived class

妖精的绣舞 提交于 2019-12-02 15:55:33
I have a virtual base class function which should never be used in a particular derived class. Is there a way to 'delete' it? I can of course just give it an empty definition but I would rather make its attempted use throw a compile-time error. The C++11 delete specifier seems like what I would want, but class B { virtual void f(); }; class D : public B { virtual void f() = delete; //Error }; won't compile; gcc, at least, explicitly won't let me delete a function that has a non-deleted base version. Is there another way to get the same functionality? It is not allowed by the standard, however