Create a class called Skateboard. Skateboard has 3 private member variables and
ID: 3780054 • Letter: C
Question
Create a class called Skateboard. Skateboard has 3 private member variables and 3 public member functions.(C++, Visual Studios)
Skateboard has the following 3 private member variables:
1) the brand of the skateboard (such as “Sector 9”)
2) the model of the skateboard (such as “Hot Steppa”)
3) the length of the skateboard in inches (such as “22”)
Skateboard has the following 3 public member functions:
a member function named print which does not have any return value or input parameter. It should display to the screen all 3 member variables (use whatever format you prefer)
a member function named set which does not have any return value but has 3 input parameters which are 1) skateboard brand 2) skateboard model and 3) length of the skateboard. This function will set the value of each member variable with each parameter given.
For example, consider the following code.
Skateboard s;
s.set(“Sector 9”, “Hot Steppa”, 22);
The above line should set the three member variables’ values accordingly.
a member function named isLongBoard which returns true if the skateboard has a length of more than 28 inches. Otherwise, it returns false.
In the main function, create a vector of 4 Skateboards named skateboardCollection.
Using a for loop and cin, ask the user to enter the brand, model, and length of each skateboard and populate each element in the vector using set member function.
After that display all of the data in the vector via a well-designed loop using the print member function. Also display if the board is a longboard as determined by the isLongBoard function.
Explanation / Answer
tested on ubuntu,Linux
/**************************Skateboard.cpp*********************/
#include <iostream>
#include <vector>
using namespace std;
class Skateboard {
//private variables
private:
std::string brand;
std::string model;
int length;
//public functions
public:
void set(std::string,std::string,int);
void print();
bool isLongBoard(int length);
};
// create a vector to store int
vector<Skateboard> store;
//set functions is used to setting variable values
//if length greater then 28 then it set default value to 28
void Skateboard::set(std::string brand,std::string model,int length) {
this->brand=brand;
this->model=model;
if(isLongBoard(length)) {
this->length=28;
}else {
this->length=length;
}
}
//printing data from vector
void Skateboard::print() {
// use iterator to access the values
vector<Skateboard>::iterator v = store.begin();
while( v != store.end()) {
cout << "Skateboard brand:" << v->brand <<" Model:"<<v->model<<" Length:"<<v->length<< endl;
v++;
}
}
bool Skateboard::isLongBoard(int length) {
if(length>28) {
return true;
}else {
return false;
}
}
int main() {
//variable declaration
int n,i;
std::string brand,model;
int length;
std::cout<<"Please Enter number of Skateboards"<<std::endl;
std::cin>>n;
//object creation of Skateboard class
Skateboard s[n];
Skateboard output;
// prompt for user input
for(i = 0; i < n; i++){
std::cout<<"user to enter the brand of skateboard"<<std::endl;
cin.ignore();
std::getline(std::cin, brand);
std::cout<<"user to enter the model of skateboard"<<std::endl;
std::getline(std::cin, model);
std::cout<<"user to enter the length of skateboard"<<std::endl;
std::cin>>length;
//calling set method for setting value
s[i].set(brand,model,length);
//inserting value into vector
store.push_back(s[i]);
}
//calling print method
output.print();
return 0;
}
/*******************output***********************/
raj@raj:~/Desktop/chegg$ g++ Skateboard.cpp
raj@raj:~/Desktop/chegg$ ./a.out
Please Enter number of Skateboards
2
user to enter the brand of skateboard
Sector 9
user to enter the model of skateboard
Hot Steppa
user to enter the length of skateboard
22
user to enter the brand of skateboard
Sector 10
user to enter the model of skateboard
Hot peppa
user to enter the length of skateboard
25
Skateboard brand:Sector 9 Model:Hot Steppa Length:22
Skateboard brand:Sector 10 Model:Hot peppa Length:25
Thanks a lot
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.