Click here to Skip to main content
15,867,971 members
Everything / Polymorphism

Polymorphism

polymorphism

Great Reads

by GProssliner
cobj is a preprocessor based generator for interface based polymorphism
by Borislav Stanimirov
A new take on polymorphism in an object-oriented setting
by Shun Huang
Introduce Python's way to support polymorphism and duck typing from the concept of C++ polymorphism
by Faye Williams
Polymorphism and overloading in C++

Latest Articles

by Shun Huang
Introduce Python's way to support polymorphism and duck typing from the concept of C++ polymorphism
by Greg Utas
Registering and efficiently accessing polymorphic objects
by Borislav Stanimirov
A new take on polymorphism in an object-oriented setting
by Satprem Pamudurthy
This article describes a C++ technique called type erasure and shows that it can be used to write generic algorithms on runtime types. It then examines the relationship between type erasure and other forms of polymorphism through the notion of type compatibility.

All Articles

Sort by Score

Polymorphism 

7 Jun 2016 by GProssliner
cobj is a preprocessor based generator for interface based polymorphism
13 Feb 2018 by Borislav Stanimirov
A new take on polymorphism in an object-oriented setting
10 Oct 2021 by Shun Huang
Introduce Python's way to support polymorphism and duck typing from the concept of C++ polymorphism
11 Mar 2018 by OriginalGriff
Suppose you have three classes: class A {} class B : A { public string S { get; set ; } } class C : B { public string S2 { get; set ; } } So C is derived from B which is derived from A. That means that instances of both C & B are instances of A plus a bit, and that a C instance is also an...
18 Nov 2015 by CPallini
Try:void Agent::delete(int flightNumber){ vector::iterator ptr; for(ptr=flightList.begin();ptr!=flightList.end();ptr++) { if( ptr->FlightNumber == flightNumber) { flightList.erase(ptr); return; } } if ((ptr)...
9 Apr 2017 by Midi_Mick
You need a base property or method that sets the _cnmae field without performing any checks. If you don't want it visible to the importers of the classes, make it protected. Then, in each of the class's CName property, call this protected method to set the value if or when the conditions are...
26 Apr 2018 by Frederic GIRARDIN
Hi. I'm using vb.net and I'm trying to extend and/or overrides a class from a referencing DLL (called IFCX) in the using DLL (ESTX). Whole shoud asking me "WHY doing this ?" the reason is overwellmind : - Both DLL have a means, but when i use ESTX, i load the data from IFCX too, but IFCX object...
4 Aug 2018 by OriginalGriff
Read the instructions again: Quote: Override the parent class ToString() method to include the name of the insurance company, the percentage paid, and the amount due after the insurance has been applied to the bill. Which class did you add this information to?
9 Jul 2020 by OriginalGriff
A Dog is an Animal, and so is a Cat. So if you create a method that takes a Animal parameter, you can pass it either a Cat or a Dog instance - but inside the method you can only use methods and properties that are applicable to both species - you...
10 Jul 2020 by OriginalGriff
What is Polymorphism? Webopedia Definition[^]: Generally, the ability to appear in many forms. In object-oriented programming, polymorphism refers to a programming language's ability to process objects differently depending on their data type or...
18 Jul 2022 by CPallini
If the classes DeriveOne, DeriveTwo derive polymorphically from Base then you may use dynamic_cast, e.g.std::cout (base[0]))->getStr()
20 Sep 2022 by OriginalGriff
Use a switch rather than if - it's "tidier" code. Then just use an initialiser: switch (discountType) { default: throw new ArgumentException($"Unknown discount type \"{discountType}\""); case PERCENTAGEDISCOUNT: return new...
13 Feb 2015 by deelll
When you create a game in c# or similiar, is it a standard to use polymorphism in relation to classes?A example of polymorphism is uploaded in this message.Thanks!
13 Feb 2015 by Paulo Zemek
I actually don't see anything "attached" to this message.In any case, yes, it is very common to use Polymorphism. Most game objects will participate in the Update/Draw logic, so they will probably have a virtual Update and Draw method implemented, which happens to be different by different...
3 Apr 2015 by Svetlozar Iliev
Hello. In my OpenGL program i have multiple shaders for multiple types of models,terrains or lamps. As up until now i had 1 shader class which was able to handle all of type of shaders. But i had to find the variable ID of each variable every time i wanted to load up something in it. Then i saw...
5 Apr 2015 by KarstenK
you should read and about Polymorphism to use it you should avoid ANY OpenGL which you can cachean abstract class cant have instances, it is an interface.Use for the shader a global instance and for your textures an so on some array or list mechanism.Tip: read about OpenGL and how the...
3 Aug 2015 by Afzaal Ahmad Zeeshan
Both are used in Object-oriented programming, and they are entirely different yet confusing terms. Function overriding is related to only inheritance. It works if methods are allowed to be overridden in the base class. Overloading works in the same class, overloading in different classes causes...
17 Nov 2015 by ikon95
I'm new to C++ and my question is how can I retrieve an object from the Flight to be compared to the input (flightNumber) in the main? How do I declare the attributes type in the main? The error message displays "invalid conversion of 'int' to 'Flight' in the second last line.class...
7 Dec 2015 by jjbruno
Hi, what Im trying to do is to create a class AVL which will inherite from a BST class. The BST may look like :class BST{private: struct Node { int val; Node* left; Node* right; };public: Node* insert(Node* node) {/* do stuff using...
2 Mar 2017 by Andy Walton
I have a class hierarchy of BaseThing, with 2 child classes XThing and YThing.I want to be able to match XThings to XThings and YThings, but I want to prevent YThings from being matched to YThings. I want a compile time error if I try and match a YThing to a YThing.The attempt to use a...
8 Apr 2017 by sreeyush sudhakaran
Change base class property to below public virtual string Cname { get { return _cnmae; } set { if (value == "Champ"|| (_status != 0 && _bal >= 500)) { _cnmae...
15 Feb 2018 by User98743
I'm struggling to understand how to implement principles of encapsulation, inheritance and polymorphism to break this code into logical parts. My background is in VBS/Classic ASP and database development, not OOP. I'm not asking anyone to write the code, but rather to point me in the right...
14 Feb 2018 by #realJSOP
Inheritance (C# Programming Guide) | Microsoft Docs[^]
15 Feb 2018 by Afzaal Ahmad Zeeshan
Apart from what Solution 1 has, I would also recommend keeping all the classes separate in their own specific files. Other than that, this can be easily converted to two separate classes; database connection management, database connection helpers (check if file or DBMS etc.). But that has to...
11 Mar 2018 by The_Unknown_Member
I understand how polymorphism works. I understand how implicit and explicit castings work for reference types but I cannot understand how inherited methods that return parent's type work For example: class SimpleClass { public SimpleClass ShallowCopy(SimpleClass sc) { ...
9 Apr 2018 by Sni.DelWoods
I have a logger class with wrapper and entries. The type of entries varies from the case I'm using the log functions. (Class for imports, exports, statistics, etc.) Is there a way to set the type in List by a parameter in the constructor? What I have tried: public abstract class...
2 Apr 2018 by #realJSOP
0) I would like to point out that you have an abstract class without any virtual methods or properties to override. 1) I would combine Import and Export objects into a single object with an enum that indicates which direction (import/export) is applicable to the object. 2) I would define Log...
3 Apr 2018 by Maciej Los
If i understand you correctly, a Log class has to be a generic class[^], which gets as an input a class derived from EntryBase class. It 's called constraints. See: Constraints on Type Parameters (C# Programming Guide) | Microsoft Docs[^] Seems, your Log class have to behave as a List[^]...
3 Apr 2018 by Sni.DelWoods
Thanks for the comments. I've found my working solution: using System; using System.Collections; using System.Collections.Generic; using System.Data; public abstract class LogBase { public class EntryBase { public DateTime Timestamp { get; set; } = DateTime.Now; public...
9 Apr 2018 by Sni.DelWoods
The method FillDerrivedFromBase(object classDerrived, object classBase) copies all values of a base class to the derrived class. This works also fine with List and I don't have to worry about the types. Simple way to fill the derrived instance with the data of the base instance: public...
26 Apr 2018 by Frederic GIRARDIN
Imports System.Runtime.CompilerServices Module MyClassExtended _ Public Function Assignations(Sender as IFCX.IMPORT_ELEMENT,about as ESTX.BL_ESTX) as list(of ESTX.ASSIGNATION) dim _list as list(of ESTX.ASSIGNATION) 'can't raise specific event Ask_Assignations because...
19 Apr 2019 by hiwa doski
in the example below when i call the foo method with p it gets the one in child class but when i access the num variable it gets the one in parent class, so is the member type based on the reference variable type or what object it points to ? public class Main { public static void...
19 Apr 2019 by Gerry Schmitz
You got an "override" for foo; but not num; and child is cast as a "parent"; so it goes for parent.num. Those are the differences.
9 Jul 2020 by User 14841286
I cant understand what the author mean by this can someone explain it to me with code In java? You can have polymorphic arguments and return types.If you can declare a reference variable of a supertype, say, Animal, and assign a subclass object...
9 Jul 2020 by Jon McKee
Griff's answer is spot on but here's some code too: class A { public void output() { System.out.println("Called A."); } } class B extends A { /* Override isn't required. It's just for clarity purposes and depending on * your dev...
9 Jul 2020 by Richard MacCutchan
One of the best ways to learn is to follow the language tutorials: Trail: Learning the Java Language (The Java™ Tutorials)[^].
10 Jul 2020 by User 14853400
So i am reading head First java book and when its explain inheritance right after that it goes to polymorphism without saying how are they connected what is the benefit its just say its make youe code flexible and others can use your code to be...
20 Jul 2022 by Phoenix Liveon
How do we properly downcast an array of Base objects to their corresponding Derive type/object? pseudocode: Base b[] = { Derive1("dOne"), Derive2(2) } print ((Derive1)b[0]).getSpecialMemFuncOfDerive1() print...
20 Sep 2022 by Himansh jain
class Discount { prop {get;set;} //some random properties string PERCENTAGEDISCOUNT {get;set;} string AMOUNTDISCOUNT {get;set;} } public Discount GetDiscountDetails(string discountType) { Discount dis = new Discount();...
5 Feb 2023 by Sơn Nguyễn Hoàng 2023
I created a virtual class Base with 2 sub class Sub1 and Sub2. And for class Sub1, I made a constructor: Sub1(istream &source) { getline(source, s); } Then i created a function to read Sub1 object from a text file: void...
5 Feb 2023 by KarstenK
My best guess is that your s is some weired global variable and so is fooling around. Sub1(istream &source) { getline(source, s); // that s is very weired } Second is that executing code in a constructor is always a bad idea. You...
5 Feb 2023 by CPallini
You are passing v by value. MOreover, you are accessing its (copy) items (v[i] without know if they actually exists. Your code should be more similar to vector Function() ifstream file("example.txt"); vector v{}; ...
2 Nov 2014 by Faye Williams
Polymorphism and overloading in C++
12 Sep 2015 by Shvetsov Evgeniy
Using C++ templates? Wish your template algorithm will be versatile and ready to work with the classes, which do not fully support the required interface? Want more functional programming with C++? Get it now!
22 Nov 2018 by Afzaal Ahmad Zeeshan
Oh boy, you got polymorphism and type safety all confused up there. What you are expecting to happen is wrong, that is not why a type is inherited. Inheritance works from parent, to child. So a function in parent, is guaranteed—let's ignore the access modifiers in inheritance for a minute—in the...
9 Apr 2016 by Akhil Mittal
This part of the article series will focus more on run time polymorphism also called late binding.
13 Oct 2017 by Satprem Pamudurthy
This article describes a C++ technique called type erasure and shows that it can be used to write generic algorithms on runtime types. It then examines the relationship between type erasure and other forms of polymorphism through the notion of type compatibility.
15 Jul 2015 by Akhil Mittal
This article will cover almost every OOP concept that a novice/beginner developer hunt for, and not only beginners, the article’s purpose is to be helpful to experience professionals also who need to sometimes brush-up their concepts or who prepare for interviews.
15 Jul 2015 by Akhil Mittal
Diving in OOP (Day 2):My article in the second part of the series will focus solely on inheritance concept in OOP
11 Sep 2014 by Francesco Bagnasco
Just a quick sample to clarify what we can do and what we can't
21 Feb 2016 by Shvetsov Evgeniy
String class with the auto. managed memory pool and performance tweaks + support modules
26 Aug 2015 by Christophe Duparquet
This article introduces a hardware abstraction tool for microcontroller programming.
26 Nov 2015 by _Ankit_Singh_
There are 4 fundamentals ideas of Object Oriented Programming. They have the wonderful terms – Abstraction, Polymorphism, Inheritance, Encapsulation. One way to remember these is the acronym APIE. Let’s dive in each of them one by one.
4 Dec 2014 by Shivprasad koirala
In this article we will learn can we overload mvc controller action methods.
22 Nov 2018 by Member 13913102
Please experts clear my doubts. I don't understand why it throws compilation error. These are polymorphic classes, in main I assigned base pointer with a derived class pointer, it should find func_2 as __vptr of base will point to __vtable of derived class. Or I have some gap in my...
22 Nov 2018 by CPallini
In order to make polymorphism work the method name (and signature) must be the same. Being ptr a actual deri pointer, you might still access its func_2 method via downcast, but that's not polymorphism: #include using namespace std; class base { public: virtual void func_1() ...
5 Feb 2023 by OriginalGriff
Without seeing the rest of your code, we have no idea if the fragment you show is even executed, let alone if it works. And we can;t run your code under the same conditions you do as we have no access to your data either. So, it's going to be up...
6 Jul 2020 by Greg Utas
Registering and efficiently accessing polymorphic objects
14 May 2017 by Habibur Rony
Application design - a post mortem of a bad design
3 Aug 2015 by F-ES Sitecore
You need function overriding overloading if you want the same method name to be called using different parameters.
9 Apr 2017 by Akhil Jain
Quote: here i have created Specialcustomer class in which i want it to change the name if cname="special" PLEASE LET ME KNOW IF I AM DOING RUN TIME POLYMORPHISM BY THIS ? pls help i am new to C# class Customer { protected int _cid,_bal,_status; protected string _cnmae; ...
4 Aug 2018 by Member 13845906
Create a class named PatientDemo that does the following: Create a Patient class for Wrightstown Hospital Billing Department. Include the following get and set properties and fields: patient ID number, name, age, and amount due to the hospital. Add a constructor for the Patient class that...
20 Sep 2022 by Virendra S from Bangalore, Karnataka
I have to create a object based on the input parameter received in a method. there two possible objects I need to create. Both objects will be having 9 same properties & just one property will be different based on the input received. public...