2. Create a Boat class that contains informa in subsequent tasks. It has the att
ID: 3699497 • Letter: 2
Question
2. Create a Boat class that contains informa in subsequent tasks. It has the attributes and methods shown in the table below. Not parameterized constructor, does not make direct assignments to instance variables (attributes). Rather, it must initialize them using the appropriate properties. tion and methods common to all boats. It will be used as a base class e carefully, that the Attributes color length Method Boat Boat A string value for the boat color An int value for the boat length Description Default constructor that initializes color to white and length to 20 A constructor with 2 parameters; a string value for the boat color that initializes color using Color and an integer value for the boat length that initializes length using Length. Color (setter) A setter property that tests the input parameter to insure it is white, red blue or yellow. If so, it sets color. For all other values, color is set to an empty string " Color (getter) A getter property that returns the color Length (setter) A setter property that tests the input parameter to ensure it is in the range of 20 to 50 inclusive. If so, it sets length. For all other values, length is set to A getter property that returns the length. Length (getter) IsValid (getter) ToString (overrides) Returns true if length is greater than 0 and color is not empty string. Returns a string as: Col or- color Length- length constructor. Display the values of the attributes. Attempt to set invalid values, such as 100 for length and purple" for color. 4. In the Main() create an instance of the class create code to create an instance of the Boat class using a default 5. Compile and run the program. The output should match that shown in Figure 1 e a sai1Boat class using the Boat class as the base class and having the following atributes methodsExplanation / Answer
here is your program : ----------->>>>>>>>>>
#include<iostream>
#include<string>
using namespace std;
class Boat{
protected:
string color;
int length;
public:
Boat(){
Length(20);
Color("white");
}
Boat(string color,int length){
Length(length);
Color(color);
}
void Color(string col){
string s[] = {"white","red","blue","yellow"};
for(int i = 0;i<4;i++){
if(col == s[i]){
color = s[i];
return;
}
}
color = "";
}
void Length(int l){
if(l >= 20 && l <= 50){
length = l;
}else{
length = 0;
}
}
int Length(){
return length;
}
string Color(){
return color;
}
bool IsValid(){
string s = "";
return color != s && length > 0;
}
string ToString(){
string s1 = "Color = ";
string s2 = " Length = ";
s2 += to_string(length);
s1 = s1 + color + s2;
return s1;
}
};
class SailBoat:public Boat{
int numSails;
public:
SailBoat():Boat(),numSails(1){
}
SailBoat(string color,int length,int numSail):Boat(color,length){
NumSails(numSail);
}
void NumSails(int num){
if(num >= 1 && num <= 4){
numSails = num;
}else{
numSails = 0;
}
}
int NumSails(){
return numSails;
}
bool IsValid(){
return Boat::IsValid() && numSails > 0;
}
double CalcPrice(){
return (double)(length*1000.0 + numSails*2000.0);
}
string ToString(){
string s1 = Boat::ToString();
string s2 = " Number Of Sails = ";
s2 += numSails;
string s3 = " Price = ";
s3 += to_string(CalcPrice());
s1 = s1 + s2 + s3;
}
};
int main(){
Boat boat1;
cout<<"boat1: "<<boat1.ToString();
cout<<" Setting Boat length to 100 and testing for validity ";
boat1.Length(100);
if(!boat1.IsValid()){
cout<<" ERROR: Boat's length can only be 20 to 50, inclusive ";
cout<<" Setting Boat's length to 20";
boat1.Length(20);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.