Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
Hello folks!

I'm currently doing a school assignment in C++, but so far I'm not being able to solve it. I have to implement the class message, which has a string topic, time_point timestamp and some data. However, that data may be a float value, string, boolean, or char data, and it's the task to figure out a nice way to handle these data formats. The only information I get is
Quote:
there are a few classes, one for each data type, that all inherit from a common (abstract) ancestor.


I tried a bit, but so far no luck. Can anyone help me out?

My current idea is this: create 4 classes that all inherit from a Data class like this: class DataFloat : public Data { }. No idea if that works, but you also need getters and setters, so let's add some functions:


Code:
class DataFloat : public Data {
public:
    float getValue();
    void setValue(float value);
private:
    float value;
};


This might work for 1 class, but how would I call these functions from the main Data class? (I prefer to only have a Data member in the message class).
Unfortunately you can't call setValue nor getValue from the parent class, as it can't ever know what it's children have.
You can get close with virtual methods, so that the parent can call methods that it's children override, but the data type would still be unknown.

I would use one of std::variant, unions+enums or template types for cases like these, but I feel as if I'm overcomplicating this.
You are all overthinking it. Polymorphism (in this case a polymorphic C++ class) is your answer.

Google it. Hope this helps Smile
I did mention polymorphism, but you can't deduce the data type from that like OP wants (as said in the last sentence).
The question specifically says you have to use an abstract class, so polymorphism isn't quite what they want. You have to make an abstract method in your superclass, and a virtual function that implements the abstract one in each subclass.
I've only ever done this in Java, but I'm assuming it's the same deal in C#
mr womp womp wrote:
The question specifically says you have to use an abstract class, so polymorphism isn't quite what they want. You have to make an abstract method in your superclass, and a virtual function that implements the abstract one in each subclass.
I've only ever done this in Java, but I'm assuming it's the same deal in C#

sighs internally

ABSTRACT BASE CLASSES ARE A FORM OF C++ POLYMORPHISM.

If OP bothered to Google that, he would understand that by now. This is an easy problem, not sure why people are saying it is hard.
MateoConLechuga wrote:
This is an easy problem, not sure why people are saying it is hard.


It seems easy until you read "This might work for 1 class, but how would I call these functions from the main Data class?". What that question asks for is not possible with regular polymorphism, which was my point.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are GMT - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement