Ask Question

Challenge activity 7.5.1: Basic constructor definition.

Define a constructor as indicated. Sample output for below program:

Year: 0, VIN: - 1

Year: 2009, VIN: 444555666#include using namespace std; class CarRecord { public: void SetYearMade (int originalYear); void SetVehicleIdNum (int vehIdNum); void Print () const; CarRecord (); private: int yearMade; int vehicleIdNum; }; / / FIXME: Write constructor, initialize year to 0, vehicle ID num to - 1. / * Your solution goes here * / void CarRecord::SetYearMade (int originalYear) { yearMade = originalYear; } void CarRecord::SetVehicleIdNum (int vehIdNum) { vehicleIdNum = vehIdNum; } void CarRecord::Print () const { cout << "Year: " << yearMade << ", VIN: " << vehicleIdNum << endl; } int main () { CarRecord familyCar; familyCar. Print (); familyCar. SetYearMade (2009); familyCar. SetVehicleIdNum (444555666); familyCar. Print (); return 0; }

+2
Answers (1)
  1. 25 July, 11:30
    0
    Here is the complete program. The constructor CarRecord () is added to the given program and is written in bold.

    #include

    using namespace std;

    class CarRecord{

    public:

    void SetYearMade (int originalYear);

    void SetVehicleIdNum (int vehIdNum);

    void Print () const;

    CarRecord ();

    private:

    int yearMade;

    int vehicleIdNum; };

    / / FIXME: Write constructor, initialize year to 0, vehicle ID num to - 1.

    / * Your solution goes here * /

    CarRecord::CarRecord () {

    yearMade = 0;

    vehicleIdNum = - 1; }

    void CarRecord::SetYearMade (int originalYear) {

    yearMade = originalYear; }

    void CarRecord::SetVehicleIdNum (int vehIdNum)

    { vehicleIdNum = vehIdNum; }

    void CarRecord::Print () const

    { cout << "Year: " << yearMade << ", VIN: " << vehicleIdNum << endl; }

    int main ()

    { CarRecord familyCar;

    familyCar. Print ();

    familyCar. SetYearMade (2009);

    familyCar. SetVehicleIdNum (444555666);

    familyCar. Print ();

    return 0; }

    Explanation:

    Constructor is basically used to initialize the class data members. It has the same name as that of the class so the class name is CarRecord so the constructor is also named as CarRecord (). Its just a member function CarRecord class and it has no return type. So this constructor is used to initialize object of a class CarRecord. Here object is familyCar. In the constructor CarRecord () you can see two variables yearMade and vehicleIdNum. These two variables are the data members of CarRecord class. So in the constructor CarRecord () these variables are given some values. So yearMade is initialized by 0 and vehicleIdNum is initialized by - 1.

    Now lets see what the main () function does. Here the object familyCar of class CarRecord is created in order to call the functions of the CarRecord class. Next the function Print () is called using the object familyCar. SoPrint () function has the following print statement:

    cout << "Year: " << yearMade << ", VIN: " << vehicleIdNum << endl;

    Which displays the year and VIN. So as the constructor has initialized the values of yearMade as 0 and vehicleIdNum as - 1 so the following line is displayed in output:

    Year: 0, VIN: - 1

    Next the object calls the method SetYearMade () and then calls method SetVehicleIdNum. These two methods set the value of yearMade as 2009 and vehIdNum as 444555666. So the last statement familyCar. Print (); in main () will display the following line in the output:

    Year: 2009, VIN: 444555666
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Challenge activity 7.5.1: Basic constructor definition. Define a constructor as indicated. Sample output for below program: Year: 0, VIN: - ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers