Question

I am working on an assignment and it asks to do the following to my inheritance class:

  • A no argument constructor that sets the data section to "0"
  • An overloaded constructor that takes a string and sets the data section to the value being passed to it.

I do not fully understand and would really appreciate it if I could have examples of both.

I am working on inheritance.

My Number.h class through inheritance.

#ifndef Number
#define Number

#include "Double.h"
#include "Integer.h"


class Number : public string

{


};

#endif
Was it helpful?

Solution

class Number : public string
{
public:
    Number() : string("0") { }
    Number(const string &str) : string(str) { }
};

However, as long as you have the following 2 lines in the beginning, the name Number will be replaced by preprocessor.

#ifndef Number
#define Number

You should use another macro name for the #include guard.

OTHER TIPS

You created the class Number. Now you can a new class (I called SpecialNumber) that has 2 constructors. The first one does not have parameter and sets the value to 0 while the other constructor gets the paramater to be used as a value for the class.

#ifndef NUMBER_H
#define NUMBER_H

class Number
{
private:
    int myValue;
};

class SpecialNumber : public Number
{
public:
    SpecialNumber()
        : myValue( 0 ) // fixed value
    {
    }

    SpecialNumber( int someValue )
        : myValue( someValue ) // value coming from a parameter
    {
    }
}

#endif
public:
Number::Number()
{
    data = "0";
}
Number::Number(string a)
{
    data = a;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top