DERIVED CLASSES

Hi, i have a question about derived classes and their use.

So, I have a base class Car and two derived classes oldCar and class newCar. oldCar has mileage information and newCar has warranty information. I want the user to input cars into the system. how do I use my derived classes that if the user wants to save the new car, the class that is used is newCar and vice versa if the user is saving the oldCar, it uses class oldCar.

Thank You!!!
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
#include <iostream>
#include <functional>

struct car
{
    virtual ~car() = default ;
    virtual void what() const = 0 ;
    // ...
};

struct old_car : car
{
    virtual void what() const override { std::cout << "old car\n" ; }
    // ...
};

struct new_car : car
{
    virtual void what() const override { std::cout << "new car\n" ; }
    // ...
};

int main()
{
    new_car nc ;
    old_car oc ;

    {
        // array of pointers to car
        car* my_cars[] { std::addressof(nc), std::addressof(oc) } ;
        for( car* p : my_cars ) if(p) p->what() ;
    }

    {
        // array of wrapped references to car
        // http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper
        std::reference_wrapper<car> my_cars[] { nc, oc } ;
        for( car& c : my_cars ) c.what() ;
    }
}

http://coliru.stacked-crooked.com/a/3c6f37ca1c623f00
Topic archived. No new replies allowed.