The following defines an object for this question class Question4 { private: int
ID: 3601987 • Letter: T
Question
The following defines an object for this question
class Question4 {
private:
int month, day, year;
public:
Question4 (int month =1, int day=1, int year=1){
this->month=month;
this->day=day;
this->year=year;
}
void printIt(){
cout <<month <<"/"<<day<<"/"<<year<<endl;
}
};
Question4 one(10,26,2017), two (2,15,1946), three(1,12,1997);
Give the code to create a standard template library(STL) container to hold a group these objects. you may choose your container.
Give the code to add the objects one,two and three to container you declare for first part of this question
Give a loop to step through container and output all the objects
Explanation / Answer
I would use vector stl container to hold the Question4 objects. below is the code written in main function.
#include <vector> // add this header file at the top of the file if the main function is going to be placed in the same file where Question4 class is declared
int main()
{
std::vector<Question4> q4vector(3);
// Add the Question4 objects to the vector
q4vector.push_back(one); // add object one to the vector
q4vector.push_back(two); // add object two to the vector
q4vector.push_back(three); // add object three to the vector
// use iterator on the vector to iterate through the vector and output the objects
vector<Question4>::iterator q4it; // declare an iterator q4it for q4vector vector
// use for loop to iterate through the vector
for ( q4it = q4vector.begin(); q4it != q4vector.end(); ++q4it ) // point the iterator to the begining of the vector // and start iterating through.
{
// q4it will point to each object of the Question4 class that were placed in the vector
q4it->printIt(); // print the data in the each object in the same order of insertion into the vector
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.