13. Why does the overloaded assignment operator need to check for assignment to
ID: 3760593 • Letter: 1
Question
13. Why does the overloaded assignment operator need to check for
assignment to self (especially in a class that holds pointers
to dynamic resources)?
--------------------------------------------------------------------
14. Explain the three main concepts represented in the
Standard Template Library (STL), what their purpose is, and
how they work together.
--------------------------------------------------------------------
15. (a) What are the advantages of using an stl::string to hold character
data vs. a dynamic char array?
Explanation / Answer
Answer :
13) Overloaded operator is used to perform operation on user-defined data type. For example '+' operator can be overloaded to perform addition on various data types, like for Integer, String,Double etc..
To overload a assignment operator, a assignment operator function is defined inside a class.The return type comes first which is followed by keyword operator in this case it is assignment operator, followed by operator sign,i.e., the operator you want to overload like i.e assignment(=) and finally the arguments is passed. Then, inside the body of you want perform the task you want when this assignment operator function is called.This assignment operator function is called when, the assignment operator(=) operates on the object of that class class name.
14) The main concepts of template library are :
Iterators:Iterators are used to step through the elements of collections of objects. These collections may be containers.
Algorithms: They provide the means by which you will perform initialization, sorting, searching, and transforming of the contents of containers.These perform act on containers.
Containers: Containers are used to manage collections of objects of a anytype of certain kind. There are several different types of containers like deque, list, vector, map ,queue,set etc
What their purpose is, and how they work together. ?
Efficiency
Strict attention to time complexity of algorithms
Templates avoid virtual function overhead
Flexibility
Iterators decouple algorithms from containers and unanticipated combinations are easily supported
Reuse:
Type-safe plug compatibility between Standard Template Library components
STL hides complex, tedious and error prone details
The programmer can then focus on the problem .
Each generic algorithm can operate over any iterator for which the necessary operations are being provided.
15(a)
An std::string is a class that handles the storage of an array of characters, and gives the user several member functions to manipulate say stored array and also overloaded operators.
A char* is basically a pointer to a character. It frequently makes this pointer point to the first character in an array.
Advantages of using an stl::string to hold character data vs a dynamic char array are :
Efficient searching, replacement, and manipulation functions.
Less amount of risk in segmentation faults.
char*s have a very lower-level access.
And finally very natural to use.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.