Extending A Class Instance and Inheriting Parent Class Instance Data - flutter

I'm really not too sure of how to say this but hopefully through showing the code someone might be able to help.
I'm trying to access the variable data in the semibreve which is an instance of the MainMusicNotesClass and then when i create a new class have it inherit the previous instance data and add on the new fields.
class MainMusicNoteValues {
String noteName;
double noteValue;
AssetImage? noteApperanace;
MainMusicNoteValues(this.noteName, this.noteValue, this.noteApperanace);
}
class MainNotes {
static final semibreve = MainMusicNoteValues(
"Semibreve", 4, const AssetImage('/assets/NoteValues/semibreve.png'));
}
class NVLiteracyNote extends MainMusicNoteValues {
int fastestTime;
int correct;
int incorrect;
NVLiteracyNote([this.correct = 0, this.incorrect = 0, this.fastestTime = 0])
: super('', 0.0, null);
//I understand that the super is the parent constructor and will determine the noteName, NoteValue and Appearance
}
Now I want to have the new variable (with the same name) in another class inherit the previous MainNotes.semibreve data and add on the new fields.
class NVNotes{
static final semibreve = NVLiteracyNote();
}
I'm thinking of how to take the MainNotes.semibreve data and pass in the noteName,noteValue and Appearance into the new NVNotes.semibreve.
Essentially at the end NVNotes.semibreve Should have the data:
noteName: 'Semibreve'
noteValue: 4.0
Appearance: const AssetImage('/assets/NoteValues/semibreve.png')
correct: 0
incorrect: 0
fastestTime: 0
I understand that this could be a terrible explanation of what i am trying to achieve but i would love any help.

Related

Initialize and modify class variables in derived classes

I want to initialize a variable class for my Base class and modify it only in some children classes.
The initialization of this class variable is in the header:
class Base{
public:
Base();
int a = 1;
The header of my derived class is:
class ChildA : public Base{
public:
ChildA ();
int a = 2;
}
Problem
I tried to run this:
Base classe*;
classe = new ChildA();
std::cout << classe->a << std::endl;
The problem is that, instead of printing 2 as I expected, it prints 1 (value initialized in my parent class). For other derived classes, I want classe->a to still return 1.
How can I solve this?
What you are seeing are the results of upcasting - having a base class point to a derived class object.
I highly recommend you read more on the topic, but a simplified picture is that the base class "carries" the whole object (base and derived content), but can access only it's original, own content. This is why you see a value from the base class rather than the derived class.
Lippman's C++ Primer has a very comprehensive explanation of upcasting, downcasting, object slicing, and other inheritance-related concepts. This includes implementing virtual functions which give you the functionality to invoke derived class functions through an interface that's common with base.
|----------------|-----------------|
| a (for Base) | a (for ChildA) |
|----------------|-----------------|
\________________/
^
|_ classe
Sorry if my drawing is not that good! As you can see in the above picture, when you create an object of type ChildA, this object contains a part for keeping the data members of the base class (i.e. Base) and another part for the data members of derived class (i.e. ChildA). Considering the fact that you have a pointer to the base class (i.e. classe), this pointer only allows you to access the base member variables and methods. So calling classe->a, returns the Base::a for you, that is 1.
If you change the type of classe pointer into ChildA, in that case calling classe->a will return 2, because it refers to the a inside the derived class, i.e. ChildA::a.
For other derived classes, I want classe->a to still return 1. How can
I solve this?
If you need in some derived classes to access different as, it is better to have a virtual function in the base class and override the base function if needed:
class Base {
public:
Base() {}
virtual int getA() { return a; }
private:
int a = 1;
};
class ChildA : public Base
{
public:
ChildA() {}
int a = 2;
};
class ChildB : public Base
{
public:
ChildB() {}
int getA() { return a; }
private:
int a = 2;
};
Now by calling getA() on a pointer of Base or ChildA, you will have 1, but calling getA on an object of type ChildB will return 2.

What do I need to do in order to ask, if an pointer to an abstract class in instance of a child class?

I need some help to understand everything correctly.
I want to check a pointer to a abstract class if it is instance of a specific child class.
I have one abstract class "kunde" and two child classes "firmenkunde" and "privatkunde" who inherit the methods of kunde.
In order to search by "int knr" (int customer number) I use as return type a pointer to "kunde".
Within the database class I get back the "kdstatus" and use
kunde * kd = new privatkunde(0);
if the status is PK or new firmenkunde(0) if it would be FK.
The abstract class kunde doesn't have any attribute I could use to save the status.
This way I did hope to be able to ask, if the returned pointer would be an instance of the class privatkunde or firmenkunde. But I did not found any method to do this.
MessageBox::Show(marshal_as<String^>(typeid(kd).name()));
I did ask the typeid name but this only returns "class kunde *" and is not what I want to know.
Is there a way to work around this problem?
You can use typeid as you mentioned. Using the polymorphism, you can write:
kunde * kd = new privatkunde(0);
if(typeid(kd) == typeid(privatkunde))
{
// This is a privatkunde objet
}
See here to have more explanation.
The problem you encounter is that your kunde class is not polymorphic. As commented by #George, see how the virtual methods work.
I hope it can help.
Generally you can use dynamic_cast for this.
An example:
class Parent
{
public:
virtual ~Parent();
virtual void do_thing()= 0;
};
class Child1: public Parent
{
public:
void do_thing() override
{
//...
}
};
class Child2: public Parent
{
public:
void do_thing() override
{
//...
}
};
void use_parent(Parent* p)
{
auto c1_ptr = dynamic_cast<Child1*>(p);
if(c1_ptr != nullptr)
{
// p is a Child1
do_thing_with_child1(c1_ptr);
}
auto c2_ptr = dynamic_cast<Child2*>(p);
if(c2_ptr != nullptr)
{
// p is a Child2
do_thing_with_child2(c2_ptr);
}
}
While this works, it becomes difficult to maintain if you have multiple child classes. You have to remember to add a case for each new child class you define.
A better way to structure something like this would be to use the "Tell, don't ask" concept when designing your object-oriented systems. A great write-up on this concept can be found here.

How to access Number through separate class?

Hey everyone so this is something that I have always had trouble trying to accomplish or understand. So I have my main Engine class calledescapeEngine where I have a private var nScore I want to be able to access this variables through a separate class called mcPlanets but I don't know how I would accomplish this. I know how to do the opposite but not how to access a var from my main Engine class. Can anyone help me out?
I am not sure what you are trying to do, but here is an example that may help you:
Inside esacapeEngine class (main), create a public var nString and new instance of mcPlanets.
// two lines in escapeEngine.as
var nScore = 0;
var mcPlant = new mcPlanets(this);
So, when you create new mcPlanets, pass in the reference (keyword 'this' in the parentheses). Now mcPlanets knows about your main class.
And now in mcPlanets class, write this:
public class mcPlanets
{
private var escapeEngine;
public function mcPlanets(main) // 'this' = 'main'
{
escapeEngine = main;
// access nScore defined in main class
escapeEngine.nScore = 5;
}
}
In this example, nScore must be a public variable, it could be a private but you should use 'get and set' methods.

haxe Incrementing class ID with an autoBuild macro

I'm trying to use an autoBuild macro on my Component class so that every class extending Component get an incrementing ID (0, 1, 2, 3...), the problem is that it seems haxe caches some files, so when recompiling some classes won't get rebuilt, so let's say some already built classes have ID 0, 1 and 2. Now I create some new classes and they will also get ID 0, 1 and 2 because the already built classes arent processed so they don't increment the ID. So I end up with some classes with the same ID in the generated code.
The code I use with the autoBuild macro is this:
#if (macro)
public static function build():Array<Field>
{
var pos = Context.currentPos();
var c = macro : {
override function get_index()
{
return $v{componentCounter};
}
public static inline var INDEX = $v{componentCounter};
};
componentCounter++;
switch (c)
{
case TAnonymous(fields):
return Context.getBuildFields().concat(fields);
default:
throw 'unreachable';
}
}
#end
public static var componentCounter = 0;
Is there any way to make something like this work?

How non-static is accessible in static context in this program?

I am having confusion with the following code:
class A
{
int x;
static void F(B b) {
b.x = 1; /* Ok,
I want to know how is this ok, in a static block how a non static
instance variables are called because as I know that static block
gets memory at compile time before execution of a single command
while non static at run time and static method accessing a non static
variable which is not created yet please elaborate me on this
*/
}
}
class B: A
{
static void F(B b) {
b.x = 1; // Error, x not accessible
}
}
Nothing gets memory at compile time. Static fields are indeed placed in the static block of memory when the type gets initialized. Call stacks for static methods are allocated at run time exactly like in case of instance methods.
Now, why static methods don't have access to the instance fields. Consider this:
class A {
public int Value;
static int GetValue() {
return Value;
}
}
There you have a class with an instance field and a static method. Now, somewhere else you try this:
var a1 = new A();
a1.Value = 5;
var a2 = new A();
a2.Value = 10;
int result = A.GetValue();
Now, if compiler allowed this, what value would the result get? 5 or 10 or something else? This just doesn't make sense, because static methods are declared for class as a whole and aren't aware of instances of this class. So in the code of static method you don't know how many (if any) instances of this class exist and can't access their instance fields.
Hope this makes a little sense.
Either you changed the code in question a bit or I didn't read very carefully. Seems like it's completely different problem right now. The variable x is indeed not accessible for the class B because of its level of protection (default in C# is private). Class A can modify X because it's declared in class A and visible to its method. Class B can't do it (you must make x protected or public for that).