C++ Write one line of code for each line below. What is the template type in eac
ID: 3844315 • Letter: C
Question
C++
Write one line of code for each line below. What is the template type in each case?
- Create a vector to store student IDs
- Create a vector to store student names
- Create a vector to store Point2D points (class Point2D down below)
- Create a set to store unique (no duplicates) student names
- Create a set to store unique (no duplicates) student IDs
- Create a map that maps student IDs to their score
- Create a map that maps student IDs to their name
- Create a map that maps student names to the name of their lab partner
#include <iostream>
#include <string>
using namespace std;
class Point2D
{
private:
double x;
double y;
public:
Point2D() { x = 0; y = 0; }
Point2D(double xVal, double yVal) { x = xVal; y = yVal; }
double getX() {
return x;
}
double getY() {
return y;
}
void setX(double xVal) {
x = xVal;
}
void setY(double yVal) {
y = yVal;
}
void moveHorizontally(double xVal) {
x += xVal;
}
void moveVertically(double yVal) {
y += yVal;
}
void moveToOrigin() {
x = 0;
y = 0;
}
void printLocation() {
cout << "Point at (" << x << ", " << y << ")" << endl;
}
};
int main()
{
Point2D p1;
p1.setX(3); p1.setY(2);
p1.printLocation();
p1.moveHorizontally(5);
p1.printLocation();
p1.moveVertically(5);
p1.printLocation();
return 0;
}
Explanation / Answer
The answers are as follows:
1. Vector to store student ids:
vector<int> array (Template type is integer)
2. Vector to store student names:
vector<string> array (Template type is string)
3. Vector to store Point2D:
vector<Point2D> array (Template type is Point2D)
4. set to store unique names:
set<string> array (Template type is string)
5. set to store unique ids:
set<int> array (Template type is int)
6. Map that maps student ids to their score:
map<int, int> mp (Template type is int & int)
7. Map that maps student ids to their names:
map<int, string> mp (Template type is int & string)
8. Map that maps student names to their lab partner names:
map<string, string> mp (Template type is string & string)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.