polymorphism

What things (or in what cases) can make C++ slower than C ?

ぃ、小莉子 提交于 2019-12-31 20:15:30
问题 This is an interview question, the interview has been done. What things can make C++ slower than C ? The interviewer asked it very deep and always asked "anything else ? " whenever I said something. My ideas: C++ features not available in C may have some cost. For example, if we use assignment to initialize class's members inside a constructor not by the initialization list, the member's default constructor may be called once before the body of the constructor, and then that value wiped out

Understanding Fortran extends types and override

不问归期 提交于 2019-12-31 12:59:20
问题 I am trying to understand the object-oriented concepts in Fortran 2003 standards (or later). I have some knowledge in C++ so I think there are some common ideas between these two languages which can help me understand them better. In C++, the polymorphism is done through class derivation and member function overriding. One defines a "abstract" base class where almost all the virtual functions are defined. Different derived classes contains the actual implementation of them. So other functions

Understanding Fortran extends types and override

邮差的信 提交于 2019-12-31 12:58:18
问题 I am trying to understand the object-oriented concepts in Fortran 2003 standards (or later). I have some knowledge in C++ so I think there are some common ideas between these two languages which can help me understand them better. In C++, the polymorphism is done through class derivation and member function overriding. One defines a "abstract" base class where almost all the virtual functions are defined. Different derived classes contains the actual implementation of them. So other functions

Polymorphic object members

房东的猫 提交于 2019-12-31 02:57:28
问题 I have the following problem. I have a base class and several classes inheriting from it. All those classes are sharing a very similar interface and will most probably not be required to overload most of the shared methods. However, all of them are using different member objects that are derived from each other and share a very similar interface too. class BaseClass { protected: Com* com; public: void setReady() { com->setReady(); } } class DerivedClass : BaseClass { protected: DerivedCom*

polymorphism with types for common fields

不羁岁月 提交于 2019-12-31 02:22:08
问题 Is this question solvable through functional idiomatic approach, could generics or discriminated unions be the answer? Is it possible to have polymorphism with passing different types to a function while the function is consuming some common fields. Idea is to be able to call and reuse the function with different types and use the common attributes/fields. type Car = { Registration: string Owner: string Wheels: int customAttribute1: string customAttribute2: string } type Truck = {

Difference between inheritance and polymorphism

懵懂的女人 提交于 2019-12-31 02:19:06
问题 I was studying about polymorphism. I couldn't determine the analogy in Java about these two features. Let's say Animal class is a concrete superclass with Cat and Dog as its subclasses. I know that this is a case of inheritance. But isn't Cat and Dog classes, polymorphs of Animal class? I am well aware of interfaces in Java. I couldn't understand why interfaces are used instead of concrete classes to explain polymorphism. It could be that the whole purpose of creating interface is to create

What does the new() constraint do on a class definition?

只愿长相守 提交于 2019-12-30 23:27:09
问题 I saw this code example and was wondering what the purpose of the new() constraint was: public class Client<T> : IClient where T : IClientFactory, new() { public Client(int UserID){ } } 回答1: That's called a "'new' constraint". Here's the documentation on it. The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor . To use the new constraint, the type cannot be abstract. (Emphasis mine) Basically, you need it whenever you

How to have Moose return a child class instance instead of its own class, for polymorphism

淺唱寂寞╮ 提交于 2019-12-30 18:56:10
问题 I want to create a generic class, whose builder would not return an instance of this generic class, but an instance of a dedicated child class. As Moose does automatic object building, I do not get to understand if this something possible, and how to create a Moose class with Moose syntax and having this behaviour. e.g.: The user asks: $file = Repository->new(uri=>'sftp://blabla') .... and is returned an `Repository::_Sftp`` instance User would use $file as if it is a Repository instance,

Overriding default accessor method across different classloaders breaks polymorphism

≯℡__Kan透↙ 提交于 2019-12-30 18:31:21
问题 I come across to a strange behavior while trying to override a method with default accessor (ex: void run() ). According to Java spec, a class can use or override default members of base class if classes belongs to the same package. Everything works correctly while all classes loaded from the same classloader. But if I try to load a subclass from separate classloader then polymorphism don't work. Here is sample: App.java: import java.net.*; import java.lang.reflect.Method; public class App {

Polymorphism and casting

泄露秘密 提交于 2019-12-30 17:19:18
问题 I want to understand polymorphism in c# so by trying out several constructs I came up with the following case: class Shape { public virtual void Draw() { Console.WriteLine("Shape.Draw()"); } } class Circle : Shape { public override void Draw() { Console.WriteLine("Circle.Draw()"); } } I understand that in order to send the Draw() message to several related objects, so they can act according to its own implementation I must change the instance to which (in this case) shape is 'pointing' to: