In: Computer Science
Given this class:
class Issue
{
public:
string problem;
int howBad;
void setProblem(string problem);
};
And this code in main:
vector<Issue> tickets;
Issue issu;
a)
tickets.push_back(-99);
State whether this code is valid, if not, state the reason
b)
Properly implement the setProblem() method as it would be outside of the class. You MUST name the parameter problem as shown in the prototype like:
(string problem)
c)
Write code that will output the problem attribute for every element in the tickets vector. The code MUST work for no matter how many elements are contained in tickets. The output should appear as:
Problem #1 is the problem attribute in element 1.
Problem #2 is the problem attribute in element 2.
a. It is not valid as, tickets is a vector to store objects of Issue(type). Hence push_back() should be called with an argument which is an object of Issue(type).
Example:
vector<Issue> tickets;
Issue issu;
tickets.push_back(issu);
b. void setProblem(string problem) implementation outside the class:
void Issue::setProblem(string problem)
{
this->problem = problem;
}
c. code output the string problem for all the elements in the vector:
for(int i = 0 ; i < tickets.size(); i++)
cout << tickets[i].problem<<" is the problem attribute
of element "<<i+1<< endl;
Example code:
#include <iostream>
#include <vector>
using namespace std;
class Issue
{
public:
int num;
string problem;
void setProblem(string problem);
};
void Issue::setProblem(string problem)
{
this->problem = problem;
}
int main()
{
vector<Issue> tickets;
Issue issu1;
Issue issu2;
Issue issu3;
tickets.push_back(issu1);
tickets.push_back(issu2);
tickets.push_back(issu3);
tickets[0].num=0;
tickets[0].setProblem("p1");
tickets[1].num=0;
tickets[1].setProblem("p2");
tickets[2].num=0;
tickets[2].setProblem("p3");
for(int i = 0 ; i < tickets.size(); i++)
cout << tickets[i].problem<<" is the problem attribute
of element "<<i+1<< endl;
return 0;
}