LW: Class Constructors Objectives Write a default constructor for a class. Note that this constructor is used to build the object anytime an argument is not provided when it is defined. Write a parameterized constructor that takes arguments that are used to initialize the class’s member variables Labwork Download the code associated with this lab: ColorConstructor.zip Main.cpp Color.h Color.cpp Compile and run the code. The first line of output of the program should display nonsensical integers for the values of the color object declared on line 5, for example: Color values after declaration : (1355559696,32767,1764024374) The program will then prompt you to input the r, g, and b values of a color, which will be assigned to the corresponding members of the color object, provided that they are valid (within the range 0-255). Create a default constructor that initializes the r, g, and b members to 255. After you've done this, compile your code and run the program. The output should be similar to that presented below: Color values after declaration: (255,255,255) followed by the previously observed input prompt. Create a parameterized constructor (this will be a new, additional constructor; leave the constructor that you modified in the previous step as is) that: takes integer arguments for r, g, and b; initializes the Color object’s members R, G, and B to the passed parameters r, g, and b using the initializer list syntax; in the body, checks whether R, G, and B are valid values; if not, you should throw an instance of Color_error{} (throw Color_error{}). Let's test the code that you've written in the previous step. Comment out the code falling between //Part 1 and //Part 2; Uncomment the code below Part 2. Compile and run your code. Test your constructor by providing valid and invalid R,G,B values as input. Main.cpp ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// // CLASS CONSTRUCTORS LAB // // MAIN.CPP // // // // Date ...: 18/MAR/2017 // ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// // Includes // ////////////////////////////////////////////////////////////////////////////// #include <iostream> #include <string> #include <limits> #include "Color.h" using namespace std; ////////////////////////////////////////////////////////////////////////////// // main // ////////////////////////////////////////////////////////////////////////////// int main(int argc, char *argv[]) { string prompt = "Please enter the r, g, and b integer values of a color, \nwith each separated by a space (e.x., \"255 255 255\"): "; // Part 1 Color color; cout << "Color values after declaration : " << color.to_str() << endl; char cont = 'y'; // continue is yes while (tolower(cont) == 'y') { cout << prompt; int r, g, b; while (!(cin >> r >> g >> b)) { if (cin.bad() || cin.eof()) { return 1; } else if (cin.fail()) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); } cout << "Invalid input." << endl << endl << prompt; } try { color.set_R(r); color.set_G(g); color.set_B(b); cout << "Color : " << color.to_str() << endl << endl; } catch (Color_error& e) { cout << "Invalid input." << endl << endl; } cout << "Do another? (y or Y) "; cin >> cont; } /* // Part 2 char cont = 'y'; // continue is yes while (tolower(cont) == 'y') { cout << prompt; int r, g, b; while (!(cin >> r >> g >> b)) { if (cin.bad() || cin.eof()) { return 1; } else if (cin.fail()) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); } cout << "Invalid input." << endl << endl << prompt; } try { Color color(r, g, b); cout << "Color : " << color.to_str() << endl << endl; } catch (Color_error& e) { cout << "Invalid input." << endl << endl; } cout << "Do another? (y or Y) "; cin >> cont; } */ cout << "Goodbye!" << endl; return 0; } Color.h ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// // CLASS CONSTRUCTORS LAB // // COLOR.H // // // // Date ...: 18/MAR/2017 // ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// #ifndef COLOR_H #define COLOR_H ////////////////////////////////////////////////////////////////////////////// // Dependencies // ////////////////////////////////////////////////////////////////////////////// #include <iostream> #include <string> #include <sstream> ////////////////////////////////////////////////////////////////////////////// // Color Class Definition // ////////////////////////////////////////////////////////////////////////////// class Color_error {}; class Color { public: std::string to_str(); bool is_valid_val(int); void set_R(int); void set_G(int); void set_B(int); private: int R; int G; int B; }; ////////////////////////////////////////////////////////////////////////////// #endif Color.cpp ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// // CLASS CONSTRUCTORS LAB // // COLOR.CPP // // // // Date ...: 18/MAR/2017 // ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// // Includes // ////////////////////////////////////////////////////////////////////////////// #include "Color.h" ////////////////////////////////////////////////////////////////////////////// // Color Class Member Definitions // ////////////////////////////////////////////////////////////////////////////// std::string Color::to_str() { std::stringstream ss; ss << '(' << R << ',' << G << ',' << B << ')'; return ss.str(); } void Color::set_R(int r) { if (!is_valid_val(r)) throw Color_error{}; R = r; } void Color::set_G(int g) { if (!is_valid_val(g)) throw Color_error{}; G = g; } void Color::set_B(int b) { if (!is_valid_val(b)) throw Color_error{}; B = b; } bool Color::is_valid_val(int v) { if (v < 0 || v > 255) return false; else return true; }


Are there any questions left?
New questions in the section Engineering
Sign up for the IQClass
Answers from experts with no ads!
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Made with love
This website uses cookies to make IQClass work for you. By using this site, you agree to our cookie policy

Pleased to see you again

IQClass unlocks the learning potential of every child
  • Master useful skills
  • Improve learning outcomes
  • Share your knowledge
Create an account
Sign in
Recover lost password
Or log in with

Create an account

IQClass unlocks the learning potential of every child
  • Master useful skills
  • Improve learning outcomes
  • Share your knowledge
Create an account
Sign Up
Or sign up with
By signing up, you agree to the Terms of use and Privacy policy.
Looking for an answer to a question you need help with?
you have баллов