1- What is a helper function? Explain the pros and cons. 2- There are three acce
ID: 3818532 • Letter: 1
Question
1- What is a helper function? Explain the pros and cons.
2- There are three access modifiers in class to limit or grant access to attributes and methods, name them and explain what they do.
3- Having the following Dynamic array of doubles class, write the code for the Constructor and copy constructor and destructor.
class DoubleArray { double* m_array; int m_size;
public:
DoubleArray(int size) {
}
DoubleArray(const DoubleArray& D) {
}
~DoubleArray() {
}
};
4- Explain what a reference is and support it with an example.
5- Convert the following function to a template so it can be used for any type of object. Make sure your conversion is done in a way that least amount of requirement is needed for an object to work with this template. This function check to make sure the “value” is in acceptable range between maxValue and MinValue inclusive:
bool isInRange(int maxValue, int minValue, int value) {
bool ret = false;
if (maxValue >= value && minValue <= value) {
ret = true;
}
return ret;
}
6- Modify the following cout statments to support the format specified:
char name[41] = "Fred";
double rate = 12.34;
cout << "*" << name << "*" << endl;;
cout << "*" << rate << "*" << endl;
* Fred*, right justified, 40 spaces
*000012.340*, right justified, 3 digits after decimal, 10 spaces padded with 0
7- If Class Base has a virtual function called act(), and the Class Derived which inherits the Base class has a function called act().
1- Is act() of Derived virtual too?
2- If Derived has another function and calls act() which act will be called, Base’s or Derived’s? How can you call act() in this function to make sure the Base’s act() is called.
8- How can you make sure that the destructor of Derived is always called when a dynamic Derived
is pointed by base’s pointer?
Explanation / Answer
1) A helper function is a function that performs part of the computation of another function. Helper functions are used to make your programs easier to read by giving descriptive names to computations.
Pros:
a)Allows many classes access to behaviour that isn't dependent on an instance of any of them.
b)Grouping together stateless utility methods in a helper class makes it clear .
c)It hs performance advantages because it uses the static methods.
Cons:
a) Because the class name is specified by each caller, you can't substitute a different class during unit testing or integration testing.
b)For the same reason, users of the class can never change a static method's behaviour (or add extra behaviour to it) by creating a subclass, proxy, decorator etc.
2) Three access modifiers are
a) Private :The private access modifier is accessible only within class.
b) Public :The public access modifier is accessible everywhere.
c) Protected :The protected access modifier is accessible within package and outside the package but through inheritance only.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.