Questions tagged [polymorphism]

In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface.

Filter by
Sorted by
Tagged with
17
votes
2answers
3k views

A vector-like polymorphic container to store objects from a type hierarchy in contiguous memory

Normally, in order to have a polymorphic collection, we store pointers to a base class: ...
14
votes
11answers
1k views

Design dilemma: extensibility vs simplicity

Here is my problem abstracted to Bird classes. I know that number of Birds will increase on the future and new behaviors might ...
12
votes
2answers
1k views

Implementing Dynamic Network Message Handling

I'm trying to handle network messages in a dynamic, and effective way, though I feel I've overthought it again. As of now, I have a very structured approach, but it feels as if it's a lot of overhead ...
11
votes
1answer
7k views

Variadic templates and pointers to member functions to achieve a named-parameters interface in C++

I studied a bit and packed all the suggestions that I received here: Fluent interface and polymorphism for building a scene with shapes and I came up with this: ...
11
votes
1answer
255 views

Accessing the Stack Exchange API

Part of a project I'm getting started on requires access to the Stack Exchange API for certain data, as a result I built a .NET implementation to interact with it. The implementation is pretty simple,...
11
votes
1answer
170 views

Multiple similar manager classes for handling scroll lists

I am working on my first object-oriented programming project, written in C# for Unity. My code does what I want it to do, but I am wondering if there is a better way to do it. I think I am building ...
10
votes
6answers
3k views

Fluent interface and polymorphism for building a scene with shapes

I would like to improve the interfaces of some polymorphic classes going from positional to named parameters and I came up with the fluent interface. The following is the most clean, compact and ...
10
votes
2answers
191 views

There Was an Old Lady - generate lyrics to a cumulation song

This is a problem from exercism.io. View the repo here (contains full example of song). Problem statement: Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'. While you ...
10
votes
1answer
2k views

Static Multilevel Inheritance with CRTP (Fast Intrusive Pointers)

I wanted to have a way to be able to static_cast to the proper derived type pointer of a base class to simulate a virtual call. Here the virtual call is for a ...
10
votes
1answer
1k views

Concept based polymorphism

I have a concept based polymorphism example listed below. I allow the user to provide any type that implements the draw method and then I add it into a vector of ...
9
votes
2answers
244 views

Area calculator for shapes as an OOP interview test

A while ago a tricky C++ interview question came across to me, and ever since I could not tell what I did wrong. Create a base class named “Shape” with a method to return the area of the shape. ...
8
votes
3answers
226 views

Single inheritance polymorphism

I am learning about polymorphism in C++ and decided to implement an example. Is there anything I need to fix in my code? This compiles and runs. ...
8
votes
1answer
15k views

Calculate area and perimeter for shapes: polygon, circle, rectangle and triangle

This is my project on inheritance and polymorphism. I have the main file from my teacher and built the project base on it. My base class is Shape. ...
8
votes
1answer
216 views

Polymorphic animals speak their name and make a noise

I'm new to GNU Smalltalk. I'd like to port a script I've written in Ruby, Scala, CoffeeScript, and several others. It's the one I use to try to learn the classic OOP concepts of abstract classes, ...
8
votes
1answer
2k views

Polymorphic (owned) reference wrapper for class hierarchies

Rationale: I often have the requirement to own object instances, while preserving polymorphic behavior (i.e. own the object and hold it by pointer, or reference). This is usually expressed as a ...
8
votes
1answer
839 views

Transformation of abstract models

In one of my recent projects I faced the problem of transforming an abstract class into another abstract class. The classes were structured like this and are part of an api for questionnaires: ...
7
votes
3answers
328 views

Implementing Dynamic Network Message Handling (Part II)

This is an iterative review request of this other question I posted. As a follow up to that previous question, I changed the pattern completely and I went to an events-based approach, which might be ...
7
votes
1answer
2k views

Python solution to Mars Rover

I'm still quite new to Python, but am loving the language (I come from a more strongly-typed language background..). A month or so ago I found the Mars Rover Challenge and attempted to solve it. I ...
7
votes
1answer
113 views

Kattis “Hitting the Targets” utilizing a polymorphic relashionship

In order to learn C++ I have written a solution to this Kattis "Hitting the Targets" problem Basically the program is to accept some shapes (circles and rectangles) on std in and a set of points and ...
7
votes
2answers
903 views

Interface-based polymorphic collection

Here is a small C++11 utility collection to store any object that satisfies a given interface. Only the basic concepts are provided, I did not try to create a full collection: ...
7
votes
2answers
142 views

C function to print arrays of arbitrary data type

C is naturally clumsy at polymorphism but I wanted to avoid duplication so I wrote this function. But I'm sure there are ways left to improve it. Please not that for brevity's sake not all data types ...
7
votes
1answer
85 views

A C++17 std::function implementation

Some time ago I implemented dynamic_array and posted it on Code Review. It used std::function internally for type erasure. This ...
7
votes
1answer
935 views

CUDA/C++ Host/Device Polymorphic Class Implementation

I have an abstract class which acts as in interface for a variety of physical models providing Electric/Magnetic Fields as the result of a number of phenomena. I'm wondering if how I've done it is a ...
7
votes
1answer
181 views

Array view adaptor class for static polymorphism over network header parsing

Some context: I have some existing code to populate network header structs from istreams. A motivating excerpt: ...
7
votes
1answer
784 views

Conversion calculator using polymorphism and Swing

This is my first attempt at a program (outside of school) and I was trying to implement and learn polymorphism on a conversion calculator for volumes and weights. I found myself using long ...
6
votes
2answers
203 views

Return the correct object type in Javascript

I am currently wondering if the following piece of Javascript code could be simplified? I would like to return the correct {},[],"" without having to pass a type as ...
6
votes
2answers
1k views

Google reCAPTCHA Validator: Iteration II

This is a follow-up to the other post I made (less than an hour ago) about my Google reCAPTCHA C# implementation: Google reCAPTCHA Validator This adds support for error messages, so that you can ...
6
votes
2answers
460 views

Dependency problem in C#, through polymorphism, and appropriate design pattern

I'm currently working on a project which involves EntityFramework with a (very) rich model (lots of inheritance). I'm currently facing a problem, quite interesting in my opinion. My model is ...
6
votes
1answer
1k views

Simple implementation of signals and slots mechanism using templates

I tried to implement a simple signal&slots mechanism for some DSP software that runs on embedded Linux. I would like if somebody can review this code and give me some guidelines. Any review and ...
6
votes
1answer
220 views

Polymorphic TLV serialization

Inspired by this question I decided to write an alternative that uses polymorhphism and a Factory pattern. The code works for the subset of implemented types, namely ...
6
votes
1answer
1k views

Image Processing Pipeline

I'm currently toying around with some Computer Vision algorithms and always wanted to learn more about templates, so I came up with the idea to build a templated Image Processing Pipeline. I want the ...
5
votes
4answers
13k views

Understanding interface with animal classes

I made this small program to demonstrate to myself an example of interface. I wanted to confirm whether it is correct and if there is anything more I should know about interfaces other than the ...
5
votes
2answers
434 views

Stopwatch that uses the abstract factory design pattern

I have wrote code for a stopwatch that utilizes the abstract design pattern and would like to get some feedback on the code, so be as harsh as you can. Note: I used ...
5
votes
2answers
241 views

Free general e-book downloader

So I decided to expand a little one-off C# console program I wrote awhile ago to download the free e-book offered by Syncfusion, and decided to also do it for other free e-book publishers... like O'...
5
votes
2answers
114 views

Avoiding casts in abstract types

I asked this question on Stack Overflow and in the comments someone had this to say. In a proper design, you should almost never have to do a dynamic_cast, even if it's hidden inside some nice ...
5
votes
1answer
204 views

Changing the type of an object inside a method?

First off, this is not my code. I'm curious what everyone thinks about changing the type of an object inside of a method then modifying the properties of the cast type. I wanted to use composition to ...
5
votes
2answers
613 views

Polymorphism with overrides and base calls simulating an employee

I've studied and worked a lot on the subject for a month or two to get closer to the concept in a cleaner way, i.e. no virtual tables and no bunch of functions with prefixes etc. and most importantly ...
5
votes
2answers
468 views

99 Bottles of Beer using polymorphism

This was part of an exercise from exercism.io. If you fancy, view the repo here. Given problem statement: Write a program which produces the lyrics to that beloved classic, that field-trip ...
5
votes
1answer
1k views

Building a model-view-controller application in PHP

I've seen at least two recent PHP questions that would do well from a Model-View-Controller ('MVC' from here-on-out in this question) setup. Now, me, being the horrible person I am, I wanted to build ...
5
votes
1answer
449 views

Making TypeNameHandling in json.net more convenient

Handling abstract types in json.net can be sometimes challenging because it requires a very long and complex syntax like: ...
5
votes
1answer
875 views

Initializing characters in a visual novel game

I am making a visual novel game and I was wondering is there a better way of setting character attributes and initializing than the way that I have done down below. As you can see I did an abstract ...
4
votes
3answers
35k views

Very simple events implementation

I've been thinking about implementing events and wrote some abstract code: ...
4
votes
2answers
3k views

Inserting and displaying books

I'm trying to write a program where you can insert and display some books (without using a database). For doing this, I use three classes: Book - is the base ...
4
votes
3answers
437 views

Simple trie class in C++

I would be very grateful to get some thoughts on this toy-implementation of a trie that I wrote to practice my C++. Some questions I have: Is my create member ...
4
votes
2answers
469 views

Data structure to store large numbers of runtime-dependent arithmetic types

Warning: lots of code. If it's too much, please focus primarly on channel.h and channel.hpp. This is my first time posting on ...
4
votes
1answer
927 views

Text-based Tetris game with CRTP - follow-up

Previous question: https://codereview.stackexchange.com/questions/74677/text-based-tetris-game-follow-up-final Summary of improvements: Implementation of a ...
4
votes
1answer
102 views

Polymorphic pointers-to-functions whose parameters are void pointers as arguments in C

My question is about this sort of table that's simplified here. Often, I find myself putting this element, (int (*)(const void *, const void *))&strcmp for <...
4
votes
1answer
745 views

Calculating a postfix expression

I've got the following code in my calculator project: ...
3
votes
1answer
655 views

Check if JavaScript input is empty

Having a general utility function to see if the input is empty makes sense to me. Empty means different things depends on the type. I just spent ~5 minutes writing this one up, so I'm sure it's ...
3
votes
2answers
312 views

Implementing different types of light sources in a Graphics project

Edit: I got asked a lot why I need to have the LightSource base class or why do I keep one vector of all the light sources, so here's my explanation: In many ...