In C++: In programming Assignment 1 at the end of the previous module, lines of text were read from an input file one-by-one, and parsed. The result of parsing was an STL vector of C++ strings . That vector would have one or more tokens, with the zeroth one being the name of a shape (like Square), followed by a list of measurements or "dimensions". Given a vector token , where token[0] is the word "SQUARE", write a code block to do the following: create a dynamic Square object, if there is a token[1] , use it to set the object's side attribute, otherwise set that attribute to zero. Remember that it's possible that the side dimension is missing on the input line. In that case the size of the vector will be one and there will be no token[1] . If you even try to look at token[1] in that case, the program will crash!
Static vs Dynamic Objects, Exercise
In
Given a vector<string> token , where token[0] is the word "SQUARE", write a code block to do the following:
- create a dynamic Square object,
- if there is a token[1] , use it to set the object's side attribute,
- otherwise set that attribute to zero.
Remember that it's possible that the side dimension is missing on the input line. In that case the size of the vector will be one and there will be no token[1] . If you even try to look at token[1] in that case, the program will crash!
Write just the code block. Assume that all needed libraries are included and that the name of the attribute in struct Square is side . Do not repeat the struct definition -- just the code block.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps