Question 10. (10 marks). Classes and Objects. Consider the following declaration
ID: 3597764 • Letter: Q
Question
Question 10. (10 marks). Classes and Objects. Consider the following declaration and implementation of the class Complex in the file Complex.h class complex t private: float* real: float* imag: public: Complex Complex(float r, float 1); float getRealQ const; float getImag const: void setReal (float r): void setImag(float i): void printO const: Now consider the implementation of the class in Complex.cpp. #include "Complex"h" #include using namespace std; Complex::ComplexO real-new float; *real = 0.0; imag = new float; *imag 0.0 complexi Comeleffloat r, float i) t real-new float; *real = r; imag new float; *imag-i; float complex: :getRea1O const freturn (*real); float complex: :getImago const (return (*imag):) void Complex::setReal(float r) t*real r;) void complex:: set!mag(float i) {*imag = i;} void Complex: :print) const ( cout exz ptnro "inag ") c endl coutExplanation / Answer
vodafone aadhar linking
a)
on calling flip method, b is passed by reference while a is not.. hence only b's change will persist after function execution...
before function:
a = (5,4.6), b=(3.7,1.5);
in function,
y.setImag(x.getImag()) => b becomes (3.7, 4.6)
So after function, output will be:
a: (5,4.6)
b: (3.7,4.6)
b)
Each complex object is created using new operator for real and imaginary float parts.. Hence for 2 objects, 4 floats memory leak.. along with that 2 complex objects are created which were also not made free.. hence memory leak for them too.
c)
declaration:
void freeRealPart();
void freeImagPart();
Implementation:
void Complex::freeRealPart() {
free real;
}
void Complex::freeImagPart() {
free imag;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.