inheritance

Calling a Base class method from Child class c++

你。 提交于 2019-12-24 09:19:05
问题 i have 4 classes Product and MultiBuyProduct (which is a child of product) which are used to work out price, and a shopping cart which you can add to which calls functions from MultiBuyProduct gets their price and prints a receipt to console, and Amount which is a class that takes 2 ints and does some calculations on them eg add subtract and then returns a formatted price. from my main.cpp i call MultiBuyProduct p2("Wine", Amount(10,0), 2, 10); ShoppingCart SC; SC.add(&p2 ,2, true); below

How to prevent .NET base class usage?

大兔子大兔子 提交于 2019-12-24 09:14:37
问题 I'm working on a project where I need to extend WebClient for a custom implementation, MyWebClient . I also need to make sure that MyWebClient is the only version that's implemented by any developer on the project and no one uses the base WebClient class. Is it possible to prevent the usage of WebClient , considering it's not code that I have access to? 回答1: Depending on the features of WebClient you need, you could consider implementing MyWebClient as proxy that exposes only the methods you

c++ base class contain instance of a derived class

泄露秘密 提交于 2019-12-24 08:57:44
问题 I was wondering if someone could explain to me how I might be able to implement something similar to this: namespace advanced_cpp_oop { class A { B b; }; class B : public A { }; } int main() { } Where an instance of a base class can contain an instance of a derived class? When the above code is compiled the following error is generated: g++ advanced_cpp_oop.cpp advanced_cpp_oop.cpp:8:5: error: ‘B’ does not name a type The (almost) equivalent Java code which does compile is: public class

Save a Child for a existent Parent

梦想与她 提交于 2019-12-24 08:49:01
问题 I have a model with some inherits and it is using nhibernate to persisti on a Database. The nhibernate mapping with fluent nhibernate is working fine, but I have a scenario where I need to save a child for a existent parent. My model looks like this: public class Item { public long Id { get; set; } public string Name { get; set; } // other properties } public class ItemCommercial : Item { public decimal Value { get; set; } // other properties } In my Database, the respective tables are

How to reference derived objects in a vector of pointers to base class objects?

☆樱花仙子☆ 提交于 2019-12-24 08:19:49
问题 I'm trying to build a chess game in C++. I have a base class gamePiece and a derived class rook. My original idea was to create a vector of gamePiece objects and insert all of the different types of gamePieces (rooks, queens, pawns) inside there. As I found out in my last question, I can't do that -- the gamePiece vector only takes base class (i.e. gamePiece) objects. However, smart pointers and other techniques were suggested. I will try that soon. But I'm still curious as to why the

Inheritance (Work with Collection.Generic)

末鹿安然 提交于 2019-12-24 08:16:35
问题 I have: class A : IHelp class B : IHelp Then I want to do such thing like: List<A> alist = new List<A>(); List<IHelp> hList = (List<IHelp>) alist; // Error!!! I am a beginner programmer. I will be very grateful to you for detailed answer. Thanks for help! 回答1: You cannot do a cast like this, because it will violate the constraints of the original list. Suppose this works like you said (it actually gives an error, but assume it works): List<A> aList = new List<A>(); List<IHelp> hList = aList;

Override class of parameter in method if it extends that of parameter used in abstract method

…衆ロ難τιáo~ 提交于 2019-12-24 08:01:12
问题 Suppose I have the following four classess, out of which two are abstract and two are concrete: Abstract1 , Concrete1 , Abstract2 , Concrete2 . Concrete1 extends Abstract1 and Concrete2 extends Abstract2 . Suppose I have the following four classess, out of which two are abstract and two are concrete: AbstractProp , ConcreteProp , AbstractClass , ConcreteClass . ConcreteProp extends AbstractProp and ConcreteClass extends AbstractClass . The code for AbstractClass would look like the following:

Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic?

拜拜、爱过 提交于 2019-12-24 07:47:48
问题 I'm a bit confused about how Java generics handle inheritance / polymorphism. Assume the following hierarchy - Animal (Parent) Dog - Cat (Children) So suppose I have a method doSomething(List<Animal> animals) . By all the rules of inheritance and polymorphism, I would assume that a List<Dog> is a List<Animal> and a List<Cat> is a List<Animal> - and so either one could be passed to this method. Not so. If I want to achieve this behavior, I have to explicitly tell the method to accept a list of

Can I implement an interface in a C# in an .h file?

偶尔善良 提交于 2019-12-24 07:38:24
问题 I have an interface that is used by several classes and the interface implementation is the same for all the classes. I would make it a base abstract class, but then it would stop the deriving classes from inhering from another class later on, which I need. So instead of reimplementing the same interface in all the classes, can I somehow implement the interface in one place, like an .h file and include the .h file in the cs file so the class "implements" the interface. By the way, the

How to get the class Type in a base class static method in .NET?

杀马特。学长 韩版系。学妹 提交于 2019-12-24 07:37:16
问题 All I want to do is similar to: public abstract class BaseClass { public static Type GetType() { return typeof(BaseClass); } } But when I derive it public class DerivedClass : BaseClass { } I want to get Assert.AreEqual(typeof(DerivedClass), DerivedClass.GetType()); How to change the BaseClass to make this assertion true? 回答1: Static methods are not inherited. The compiler substitutes BaseClass for DerivedClass when calling a base class' static methods through one of its derived classes. For