0

I have to make class Car with constructor which catch power and owner. Objects are typed static, not getting data from user. And my homework look like - "wrong values of arguments for constructor should be reported using exceptions". Is that possible ? When I try to type int where conctructor want string I have compilator error. So how can i catch type of variable if program can't be compiled ?

class Car{
public:

    string owner;
    int power;
    Car(string owner, int power)
    {
        this->owner=owner;
        this->power=power;
    }
};

Compilation error

int main()
{
    Car *Fiat = new Car(15, 1200);
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 5
    You could have a wrong value without it being the wrong type. e.g. if I store a person's age, and get the value 3027, it's probably wrong! – BoBTFish Jun 03 '19 at 14:01
  • Is a power value of `0` or `-1` or `1000000` acceptable? Is an owner value of `""` acceptable? – Eljay Jun 03 '19 at 14:06
  • 7
    You'll want to get rid of the habit of using `new` for objects as soon as possible. It isn't the natural way to create an object in C++. – chris Jun 03 '19 at 14:07
  • it is possible with templates, though I doubt that it makes sense to turn a compiler error into a runtime error when anyhow there is no chance that passing the wrong type could go well during runtime. Can you post the task verbatim? maybe some details got lost, tbh I doubt that solving the task as described here makes any sense – 463035818_is_not_an_ai Jun 03 '19 at 14:07
  • "My bad, question is about value not type of variable" that explains quite some confusion. You should also fix the rest of the question that is still asking to check the type... – 463035818_is_not_an_ai Jun 03 '19 at 14:11
  • 3
    note that editing a "RESOLVED" into the question is not the way questions on SO are flagged as resolved. Typically you'd get answers and you can accept one of them. If your question turned out to be based on a misunderstanding you could either fix the question or delete it – 463035818_is_not_an_ai Jun 03 '19 at 14:12

2 Answers2

0

The question asks about wrong values, not wrong types. The constructor you have is a good start.

What are the valid strings for owner? What are the valid values for power? Write code to check that the arguments passed to the constructor meet those requirements.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
-1

Just do this:

template<typename _OwnerType, typename _PowType>
class Car {
public:

    _OwnerType owner;
    _PowType power;
    template<typename _OwnerType, typename _PowType>
    Car(_OwnerType owner, _PowType power)
    {
        this->owner = owner;
        this->power = power;
    }

};

This way the class dosen't care about the types you provide it with, just the values.

Shannarra
  • 521
  • 4
  • 17
  • 2
    Side note: Those underscored identifiers are reserved by the standard. Safe rule of thumb: Don’t use leading underscores at all unless you’re implementing the standard library itself. – besc Jun 03 '19 at 16:33
  • 1
    @besc To be more specific: identifiers with leading underscore *followed by uppercase letter* are reserved in any scope. https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – lisyarus Jun 03 '19 at 16:54
  • Or to be absolutely precise: [lex.name] in the standard, that’s chapter 5.10(3) in C++17. But I prefer the rule of thumb for its simplicity, even though it is a bit too broad. – besc Jun 03 '19 at 18:10