2) Review the way in which const variables and references can be initialized in
ID: 3863781 • Letter: 2
Question
2) Review the way in which const variables and references can be initialized in a class constructor. 6) Review the definition of a default assignment operator.
7) Review the definition of the this pointer.
8) What is a const member function? How is it declared?
9) Write a code fragment that allocates an array of double of size 10 using new and initializes a pointer. Include the declaration of the pointer. Write the statement that frees the memory allocated above.
10)Review the logic and syntax of try / catch blocks.
13) Write a class representing points in the x-y plane. Implement an overloaded operator+.
14) Read and understand the reason why the class Penguin cannot be simply derived from the class Bird in the lecture notes (Lecture 8).
16) What is Run-Time Type Identification (RTTI)? Give an example using the dynamic_cast operator.
19) Review the definitions of the STL sequential and associative containers discussed in the lecture notes.
24) Write a program that reads strings from stdin, inserts them in a set container, and prints them sorted on stdout.
27) Express the cost of finding an element in a balanced binary search tree, as a function of the number N of elements in the tree.
29) Understand where to add an element in a binary search tree.
30)Write a range-based for loop to print on stdout all the elements of a list declared as list a;
20) Assuming that a vector contains the numbers {1,2,3,4,5}, write a statement using the insert function so that the element 6 is placed between 3 and 4 (i.e. the resulting vector contains {1,2,3,6,4,5}. Write a short program containing your statement and run it to check that it does what you expect.
Explanation / Answer
2) Review the way in which const variables and references can be initialized in a class constructor.
The const variable specifies whether a variable is modifiable or not. The constant value assigned will be used each time the variable is referenced. The value assigned cannot be modified during program execution.And A const variable has to be declared within the class, but it cannot be defined in it. We need to define the const variable outside the class.
Example:
#include <iostream>
using namespace std;
class T1
{
const int t;
public:
T1() : t(100)
{
cout << "T1 constructor: " << t << endl;
}
};
6)Review the definition of a default assignment operator.
If a class doesn’t contain pointers, then there is no need to write assignment operator. The compiler creates a default assignment operators for every class. The compiler created assignment operator may not be sufficient when we have pointers or any run time allocation of resource like file handle, a network connection..etc.
The implicit assignment operator does member-wise assignment of
each data member from the source object. For example,
MyClass& MyClass::operator=( const MyClass& other ) {
x = other.x;
c = other.c;
s = other.s;
return *this;
}
Compiler doesn’t creates default assignment operator in following cases
1. Class has a nonstatic data member of a const type or a reference type
2. Class has a nonstatic data member of a type which has an inaccessible copy assignment operator
3. Class is derived from a base class with an inaccessible copy assignment operator.
7)Review the definition of the this pointer.
The this pointer is passed as a hidden argument only within the nonstatic member functions of a class, struct, or union type.
‘this’ pointer is a constant pointer that holds the memory address of the current object. It points to the object for which the member function is called. Static member functions do not have a this pointer.
Syntax:
this
this->member-identifier
The expression *this is commonly used to return the current object from a member function:
return *this;
Example:-
Consider the object:
setMonth( &myDate, 3 );
void Date::setMonth( int mn )
{
month = mn; // These three statements
this->month = mn; // are equivalent
(*this).month = mn;
}
The object's address is available from within the member function as the this pointer. Most uses of this are implicit. It is legal, though unnecessary, to explicitly use this when referring to members of the class.
8)What is a const member function? How is it declared?
A function becomes const when const keyword is used in function’s declaration.
Example:void f() const makes the function itself const. This only really has meaning for member functions.
Making a member function const means that it cannot call any non-const member functions, nor can it change any member variables. It also means that the function can be called via a const object of the class.
The idea of const functions is not allow them to modify the object on which they are called.
Following is a simple example of const function.
#include<iostream>
using namespace std;
class Test {
int value;
public:
Test(int v = 0) {value = v;}
int getValue() const {return value;}
};
int main() {
Test t(20);
cout<<t.getValue();
return 0;
}
When a function is declared as const, it can be called on any type of object. Non-const functions can only be called by non-const objects.
For example the following program has compiler errors.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.