Can someone help answer these C++ questions. How is overloading involved with co
ID: 3858318 • Letter: C
Question
Can someone help answer these C++ questions.
How is overloading involved with constructors? What is an initializer list with respect to constructors? Give an example of using an initializer list for a Rectangle class. (Assume a Rectangle is initialize-able via its lower-left and upper-right corner points.) Initializer lists aim to make class object construction more efficient. How? In the following code fragment: explain what object(s) is/are being constructed from what it/they is/are being constructed which constructor(s) will be used to perform the construction(s) (See the explanation in #18 about what a rational number is.) void f(Rational a): void f(Rational & a): inline Rational g(void) (Rational t: return t: } //in same function...maybe main? (Rational x: Rational y(4, 3): Rational z = y: Rational w(x): Rational r(-2): f(x): x = g():)Explanation / Answer
44) Overloading with constructor
When we have more than one function having same name with different parameter is called overloading.
In case of constructor overloading function name will obviously be same but will have different parameter than we can call this as constructor overloading.
Example:
//Default constructor
Point()
{
X = 0;
Y = 0;
}
Point(int s, int e)
{
X = s;
Y = e;
}
In the above example Point is the class name. First constructor takes no argument and second constructor takes two arguments. As per the definition of constructor overloading the parameters are different so it is called constructor overloading.
45)
Initializer List is used to initialize data members of a class. The members that we need to initialize is indicated with constructor as a comma separated list followed by a colon.
If the parent class which takes arguments to its constructor then, initialization lists come into play. Initialization list follows the constructor's signature, separated by a colon (:) symbol.
In case of derivation first base class constructor is called to initialize the members of base class then, derived class constructor is called to initialize the derived class members.
#include<iostream>
using namespace std;
class Rectangle
{
private:
int lowerLeft;
int upperRight;
public:
Rectangle(int ll, int ur):lowerLeft(ll), upperRight(ur) {}
int getLowerLeft()
{
return lowerLeft;
}
int getUpperRitht()
{
return upperRight;
}
};
int main()
{
Rectangle r(1, 25);
cout<<"Lower Left = "<<r.getLowerLeft()<<", "<<"Upper Right = "<<r.getUpperRitht();
return 0;
}
46)
i) The objects are x, y, z, w, r, t, a.
ii)
Rational x; will create object x.
Rational y(4, 3); will create object y.
Rational z = y; will create object z.
Rational w(x); will create object w.
Rational r(-2); will create object r.
f(x) function call will create object a.
x = g() function call will create object t.
iii)
Rational x; Default constructor.
Rational y(4, 3); Parameterized constructor with two integer type arguments.
Rational z = y; Copy constructor.
Rational w(x); Parameterized constructor with one class type argument.
Rational r(-2); Parameterized constructor with one integer type argument.