polymorphism

What happens when a subclass object is assigned as a superclass object

送分小仙女□ 提交于 2019-12-24 06:46:28
问题 So I have a question about a superclass A and subclass B, where A has 2 public variables and B has 1 more. I saw this snippet of code: A a = new A(); B b = new B(); a = b; What does that last line do? I don't really understand what actually happens when you use "=" between 2 classes in an inheritance relationship. 回答1: It's simple assignment. = is an assignment operator. Lets be clear of below points. In Java when an Object is created and it is accessible via. reference. A reference refer to

derivation template classes

不问归期 提交于 2019-12-24 06:44:30
问题 I have written a template BST class with the usual operations like this: template <class Key,class T> class BTree { public: BTree():root(0){}//crea un albero vuoto BTree<Key,T>& treeInsert(const Key& k,const T& val); BTree<Key,T>& treeDelete(const Key& k); Node<Key,T>& treeSearch(const Key& k); Node<Key,T>& treeMinimum(); void treeClear(); protected: BTree<Key,T>& transplant(Node<Key,T>& n1,Node<Key,T>& n2); Node<Key,T>* root; }; I would like to implements a template red-black tree class that

rails has_many manager

﹥>﹥吖頭↗ 提交于 2019-12-24 05:47:13
问题 I'm attempting to create a polymorphic imaging system which would allow various objects to have a cover image and additional images. Would I be correct in creating an Image model with belongs_to :imageable ? Or, should I separate out my logic so each model that will inherit image capabilities be given a separate polymorphic associations for both cover images and additional images? Then, once I have setup has_many relationship, how do I manage it? In a perfect world I would want to be able to

Polymorphism in VB.NET via Late Binding disallows With Events, workaround?

删除回忆录丶 提交于 2019-12-24 05:17:07
问题 I'm working on developing an application that talks to a family of USB sensors. I've created a basic implementation that utilizes a class called Sensor. The class contains events and methods that allow for interaction with the sensor (there is also a threaded task processor involved but I'll go with a simple example). My issue is that this simple proof of concept example works fine but now I need to expand the application to support the whole family of sensors. To do this I've created a

Runtime polymorphism

馋奶兔 提交于 2019-12-24 04:32:21
问题 Suppose I have class A public class A { public void method() { //do stuff } } Also another class B public class B extends A { public void method() { //do other stuff } } Now I have the following statements: A a = new B(); a.method(); Is this an example of run time polymorphism? If yes, then is no binding is done for the reference a at compile time? 回答1: The compiler will tell you that this can't work, because there's no relationship between A and B that will allow you to write A a = new B();

Template Polymorphism not Working?

徘徊边缘 提交于 2019-12-24 03:39:33
问题 I'm building a small template hierarchy and try to make use of class polymorphism. Here's some example code (which does not compile) to demonstrate it: template<typename T> struct A {}; template<typename T> struct B { B (A<B> *) {} }; struct C : public B<int> { C(A<C> *p) : B<int>(p) {} // error }; int main() { A<C> ac; C c(&ac); } My compiler (gcc 4.01 on OS X) throws the following error on the specified line: error: no matching function for call to ‘B<int>::B(A<C>*&)’ But from my logical

A need for dynamic cast of a derived class: looking for an alternative approach

北战南征 提交于 2019-12-24 03:32:43
问题 I present my question in this simple form: class animal { public: animal() { _name="animal"; } virtual void makenoise(){ cout<<_name<<endl; } string get_name(){ return _name; } protected: string _name; }; class cat : public animal { public: cat() { this->_name="cat"; } }; class dog : public animal { public: dog() { this->_name = "dog"; } }; I want to store all animal types together in a single container such as: vector<animal*> container; barnyard.push_back(new animal()); barnyard.push_back

How does inheritance and polymorphism work in this situation?

試著忘記壹切 提交于 2019-12-24 02:04:10
问题 This is the first class package test; public class Project { public void doSomething (String stuff) { writeStuff(); whichProject(stuff); } public void writeStuff(){ System.out.println("This is stuff"); } public void whichProject(String stuff){ System.out.println("This is a random project " + stuff); } } and this is the derived class package test; public class Project1 extends Project{ public void whichProject(String stuff){ System.out.println("Coding project number one: " + stuff); } public

Calling a method on a List of Generic Type from abstract Parent class

时间秒杀一切 提交于 2019-12-24 01:57:28
问题 Here is my current Type Hierarchy: I am trying to implement a method in PlaneRegion that will call a method named Shift() on a list in its derived classes where the list is called PlaneBoundaries in all of them but they are of different types like so: public abstract class PlaneRegion<T> { public abstract List<T> PlaneBoundaries { get; set; } } public class Polygon : PlaneRegion<LineSegment> { public override List<LineSegment> PlaneBoundaries { get { return _planeBoundaries; } set {

Overloading rules for inheritance in C++

痞子三分冷 提交于 2019-12-24 01:27:22
问题 The following does not compile (Apple LLVM version 4.2 (clang-425.0.28)): class A { public: virtual void foo() {}; virtual void foo( int i ) {}; }; class B : public A { public: virtual void foo( int i ) override { foo(); } }; The compiler error is "Too few arguments" for the call to foo() inside B::foo(int). The compiler apparently thinks that I want to recursively call B::foo(int) and does not recognize that I want to call A::foo(void). The error goes away if I replace the call to foo() by A