C++ ============= Part 3 - Modify the following Program. Convert the following p
ID: 3603460 • Letter: C
Question
C++
=============
Part 3 - Modify the following Program. Convert the following program as per comments
// program colors
// -> Convert class into .h header file and .ccp implemenation file.
// Keep int main() funciton same...
#include <iostream>
using namespace std;
#include <string>
/* srand example */
#include <ctime>
#include <stdio.h> /* NULL */
#include <stdlib.h> /* srand, rand */
class colorPicker {
private:
string colorArray[7];
public:
colorPicker() {
// Defalut Consructor assign values to array
// Use 7 assignment statements to assign each color to the color array
colorArray[0] ="Red";
colorArray[1] ="Green";
colorArray[2] ="Purple";
colorArray[3] ="Yellow";
colorArray[4] ="Orange";
colorArray[5] ="Indigo";
colorArray[6] ="Pink";
}
void printAllColors() {
// use for loop to print out all colors
for (int i = 0 ;i < 7; i++)
{ cout << colorArray[i] << endl;}
}
string randomColor() {
srand((unsigned)time(0));
// pick a random number between 1 and 7
int i =0;
i = rand()%7;
return colorArray[i];
}
};
int main()
{
colorPicker P;
//
P.printAllColors();
cout << "Random Color: " << P.randomColor();
system("pause");
return 0;
}
======================
Explanation / Answer
//colorPicker.h
#ifndef colorPicker_h
#define colorPicker_h
class colorPicker
{
string colorArray[7];
public:
colorPicker();
void printAllColors();
string randomColor();
};
#endif
// colorPicker.cpp
#include “colorPicker.h”
//
colorPicker :: colorPicker()
{
colorArray[0] = “Red”;
colorArray[1] = “Green”;
colorArray[2] = “Purple”;
colorArray[3] = “Yellow”;
colorArray[4] = “Orange”;
colorArray[5] = “Indigo”;
colorArray[6] = “Pink”;
}
//
void colorPicker :: printAllColors()
{
for (int I = 0; i<7;i++)
{ count << colorArray[i]<<rndl;}
}
//
string colorpicker :: randomColor()
{
srand((unsigned)time(0));
int I = 0;
I = rand()%7;
return colorArray[i];
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.