Here are a few questions to help you clarify your understanding of advanced classes (mostly mixing them with vectors).
The software engineering term composition is used to describe a situation when one class is made up of data members (objects) of another class.
Given this class definition:
class Composite { string member; public: Composite(void); const string get_memb(void) const; };
Show how to define the default constructor having the member string call its default constructor in your member initialization list.
Composite::Composite(void) : member() { }
Show how to utilize chaining of method calls to display the length of the string member. (Hint: You'll need an object, right?)
Composite bob; cout << bob.get_memb().length();
When a class is partially composed of a vector data member, normal construction, access, and mutation patterns are not normally followed [pardon the pun].
Given the following class:
class VectMembDemo { vector<long> member; // other data? public: // ctor's // acc's // mut's // other methods... };
Briefly describe what needs to happen in the 'ctor', 'acc', and 'mut' portions of the class method area with respect to the vector member we are focusing on here.
The constructor area needs to default construct the vector member or copy it from another of our object instances. But it most likely won't take in an initial vector already fully formed. If we are holding a vector as our data member, we should be responsible for it from birth — not just from its teens or twenties on. The accessor area needs to allow access to individual elements of the vector — not to the entire vector, typically. But also we need a meta-accessor for the vector member's current size. That's because when they ask for an element of the vector member they'll have to indicate which one via a position and, of course, they'll need to know the [strict] bounds on those positions to give us good information. The mutator area needs to allow mutation of individual elements of the vector — not to the entire vector, typically. But we'll also need an extra mutator that adds a new element to the vector. After all, the other mutator only allows them to change what is there.
When we use a class as a vector base type, we often end up with the strange syntax combination of a dot (.) following a pair of square brackets used to acquire a single element of the vector.
When you either declare a vector with an initial size or resize a vector to a size larger than it is now, the default constructor will be called on all the new elements.
Given a vector defined as:
vector<Point2D> corners; // corners of polygon
Show code to calculate the length of the polygon's perimeter (assuming the corners are already filled in).
double peri = 0.0; if ( corners.size() > 1 ) { corners.back().distance(corners.front()); for (vector<Point2D>::size_type p = 0; p+1 != corners.size(); p++) { peri += corners[p].distance(corners[p+1]); } }
TRUE✓ | FALSE✗ | class methods can be called before they are even declared unlike non-class functions. |
---|