Description
Introduction:
The goal of this lab is to create a class in C++ and commit your changes in GIT.
Reference:
Git commit: https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners
Week 2 Powerpoint materials on Brightspace.
Steps:
- Use the project you started with by following the Powerpoint slides for week 2: CMakeLists.txt and week2.cpp. Create a git repository and add those two files. Then commit them using the commit message “initial commit”
- Remove the std:: namespace declarations and add the line: using namespace std;
- Above the main( ) function, declare a class called Vehicle. Vehicle should have 2 private integer variables numWheels and numDoors.
- Implement 3 different public constructors: (look at slides 28 & 29)
- Vehicle( int w, int d ) // constructor for the number of wheels and doors
You should be setting numWheels to w, and numDoors to d.
- Vehicle( int w) // this should call constructor a) with parameters doors = 4, and wheels = w. In other words, call: Vehicle(4, w)
- Vehicle() // empty constructor. This should call constructor b) with wheels = 4. Constructor c) should call b), which itself calls constructor a).
- Commit your files again using the commit message “Finished constructors”. Now go to the constructors and write the corresponding output statements:
- This goes in Vehicle():
cout << “In constructor with 0 parameters” << endl;
- This goes in Vehicle(int w):
cout << “In constructor with 1 parameters, wheels = “ << w << endl;
- This goes in Vehicle(int w, int d):
cout << “In constructor with 2 parameters” << endl;
- Write a destructor that only outputs a message: cout << “In destructor” << endl;
Use git to commit your work with the message “Finished destructor”
- In the main function, practice calling the constructors by writing the line:
Vehicle myVehicle; // This calls constructor Vehicle()
- Commit your work using the message “Demo #1” using the command:
git commit -am “Demo #1”
- Now modify the constructor in the main() function to use the second constructor:
Vehicle myVehicle( 4 ); //This calls constructor Vehicle(int);
- Commit your work using the message “Demo #2” using the command:
git commit -am “Demo #2”
- Now modify the constructor in the main() function to use the second constructor:
Vehicle myVehicle( 4, 2 ); //This calls constructor Vehicle( int, int);
- Commit your work using the message “Demo #3” using the command:
git commit -am “Demo #3”
For your demonstration, run your code from step 12. Visual Studio will run and should output messages to the console. You should also make sure that your constructors are actually setting the variables with initial values. In other words, the variables numWheels and numDoors should be set to w and d in the 2-parameter constructor.
Next, type “git log –oneline”. Then type “git checkout …….” but replace the dots with the hash string that corresponds to “Demo #2”. Your code in visual studio will automatically change to the Demo 2 version of the code. Then type “git checkout …” with the hash string for Demo #1.
Lastly, type “git show ….” Followed by the hash string for Demo #1, #2, and #3. Marks: (total of 10) The Vehicle class is defined and has private variables +1 The Vehicle class has public constructors that are chained +3 The 2-parameter constructor sets the variables to the parameters passed in +1 You demonstrate calling the 3 constructors by checking out various commits +3 You can show what lines in a file are changed by a commit using “git show” +1 The destructor is implemented and has an output message +1