Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Define and use default constructor and constructor overloads with a class .This

ID: 3571759 • Letter: D

Question

Define and use default constructor and constructor overloads with a class .This exercise is a built up from Ex 24. You can re-use your solution/project for Ex 24. (You can create a new project and copy your existing source code for Ex 24 and delete the parts that you don’t need)

Programming Exercise

You should have a class called Skateboard where Skateboard has 3 private member variables (refer to Ex 24 for details).

You don’t need the previous code for the main function. Remove it and make a clean main function to start with.

1) Add 1 more private member variable called width.

2) Add print member function to print each element of the Skateboard class.

3) Write 2 constructors as follows:

default constructor (has NO input parameter) with member initialization list that initializes the brand and model with “NA”, and length and width to -1.0. Since this uses a member initialization list, there shouldn’t be any statements inside the body.

2nd constructor overloaded with 4 parameters for the skateboard brand, model, length, and width. The constructor will initialize all of the member variables and will accept values for these parameters. Also this should have an empty body; the assignments should be done in the header.

In the main function, create 2 separate objects of Skateboard s1 and s2 where s1 uses the default constructor and s2 uses the 2nd constructor. For s2, use any values you want as long as the length and width are non-negative. After creating the 2 objects, call the print member function for each object and check the output.

Q1: Which object has “NA” as the brand?
Q2: Which object has a length that is not -1?

My Code:

#include "stdafx.h"

#include <iostream>

#include <string>

using namespace std;

//Class Function

class Skateboard

{

       //Declaring Varibales

       string brand, model;

       int length;

public:

       void print()

       {

              cout << "Brand: " << brand << endl;

              cout << "Model: " << model << endl;

              cout << "Length(inches): " << length << endl;

       }

       void set(string b, string m, int l)

       {

              brand = b;

              model = m;

              length = l;

       }

       bool isLongBoard()

       {

              if (length > 28)

                      return true;

              else

                      return false;

       }

};

int main()

{

       string b, m;

       int i, len;

       Skateboard skateboardCollection[4];

       //for Loop

       for (i = 0; i < 4; i++)

       {

              cout << "---SKATEBOARD " << i + 1 << "---" << endl;

              cout << " Enter Brand: "; cin >> b;

              cout << " Enter Model: "; cin >> m;

              cout << " Enter Length(inches): "; cin >> len;

              skateboardCollection[i].set(b, m, len);

       }

       //For Loop

       for (i = 0; i < 4; i++)

       {

              cout << "---SKATEBOARD " << i + 1 << "---" << endl;

              skateboardCollection[i].print();

              if (skateboardCollection[i].isLongBoard() == true)

                      cout << "Long Board Status: TRUE" << endl;

              else

                      cout << "Long Board Status: FALSE" << endl;

       }

       system("pause");

       return 0;

}

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;

class Skateboard{
       string brand, model;
       float length;
       float width; //newly added private member variable
public:
      //constructor with no input parameter
      Skateboard(): brand("NA"), model("NA"), length(-1.0), width(-1.0) {}
      //overloaded constructor with 4 parameters
      Skateboard( string b, string m, float l, float w ): brand(b), model(m), length(l), width(w) {}
    
      void print()
      {
              cout << "Brand: " << brand << endl;
              cout << "Model: " << model << endl;
              cout << "Length(inches): " << length << endl;
              cout << "Width(inches): " << width << endl;
      }
      void set(string b, string m, float l, float w)
      {
              brand = b;
              model = m;
              length = l;
              width = w;
      }
      bool isLongBoard()
      {
              if (length > 28)
                      return true;
              else
                      return false;
      }
};

int main()
{
Skateboard s1;
Skateboard s2( "brand","model",2,3);
s1.print();
cout << endl;
s2.print();

//Ans1: s1 has NA as brand
//Ans2: s2 has length that is not -1
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote