Constructors in derived class

Here is a base class Unit, the constructor is

Unit(double v, srting a, double sif, string sia) : value(v), abbreviation(a), siFactor(sif), siAbbreviation(sia) {}

And a derived class is UnitOfLength with the SIUnit is meter, then I wrote the constructor as

UnitOfLength(double v, string a, double sif, string sia = "m") : Unit(v, a, sif, sia) {}

Now, I have to build a derived class Meter of UnitOfLength, but it requires that I can only write the constructor with one parameter, e.g. Meter(double m = 0), other 3 parameters should be set automatically. I tried to finish the code, but it doesn't work. I wonder if it can be realized.

Would you please give me some hints?

Thank you so much!
i just saw this kinda derivation now, is this legal? all this time, all i see is the first type of assignment
Last edited on
Do you have any other constructors for the UnitOfLength class? Maybe something along the lines of
UnitOfLength(double v) : Unit(v, "m", 1, "m") {} //probably should make explicit too
This way when you create the Meter constructor the appropriate UnitOfLength constructor is called.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
struct Unit
{
    Unit( double v, std::string a, double sif, std::string sia )
        : value(v), abbreviation(a), siFactor(sif), siAbbreviation(sia) {}

    virtual ~Unit() {}

    protected: double value ;

    public:
        const std::string abbreviation ;
        const double siFactor ;
        const std::string siAbbreviation ;

    // ...

};

struct UnitOfLength : Unit
{
    UnitOfLength( double v, std::string a, double sif, std::string sia = "m" )
        : Unit(v, a, sif, sia) {}

    // ...
};

struct Meter : UnitOfLength
{
    explicit Meter( double m = 0 ) : UnitOfLength( m, "metre", 1.0, "m" ) {}

    // ...
};
Experience tells me I've probably misunderstood your question, but here goes:

Many things you could do legally, not sure how wise any of them would be without more information.
If you already have Meter(double m = 0.0) in mind, you could just extend that same paradigm Meter(double m = 0.0) : UnitOfLength(m, "", 0.0)
or if you make UnitOfLength(double v, string a = "", double sif = 0.0, string sia = "") then you could just do Meter(double m = 0.0) : UnitOfLength(m)
Or you could have a default constructor for UnitOfLength and just not bother about it in your Meter(double m = 0.0) constructor. See example below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <string>

using namespace std;

class Unit
{
	double value;
	string abbreviation;
	double siFactor;
	string siAbbreviation;

public:
	Unit(double v, string a, double sif, string sia) : value(v), abbreviation(a), siFactor(sif), siAbbreviation(sia) {}
};

class UnitOfLength : public Unit
{
public:
	//constructor for scenario 1, comment out for scenarios 2 and 3
	UnitOfLength(double v, string a, double sif, string sia = "m") : Unit(v, a, sif, sia) {}
	
	//constructor for scenario 2, uncomment for scenario 2
	//UnitOfLength(double v, string a = "", double sif= 0.0, string sia = "m") : Unit(v, a, sif, sia) {}

	//constructor for scenario 3, uncomment for scenario 3
	//UnitOfLength() : Unit(0.0, "", 0.0, "") {}
};

class Meter : public UnitOfLength
{
public:
	Meter(double m = 0.0) : UnitOfLength(m, "", 0.0) {} // comment out for scenarios 2 and 3
	//Meter(double m = 0.0) : UnitOfLength(m) {}        // uncomment for scenario 2
	//Meter(double m = 0.0) {}                          // uncomment for scenario 3
};

int main()
{
	Meter m(2.5);
	return 0;
}
Thank you all so much! Then the problem is solved with your help! Thank you again!
Topic archived. No new replies allowed.