Question: Need help with this short C++ lab involving constructors and destructors for a class. A sample execution is shown below. The main.cpp that needs finishing
Need help with this short C++ lab involving constructors and destructors for a class. A sample execution is shown below. The main.cpp that needs finishing is also listed below.


main.cpp that is given:
#include using namespace std; // TODO: add a constructor (no args) that sets mantissa and exp to 0 // print "Default constructor ", then invoke // print() method // add a constructor: given a mantissa and exponent, // invoke set() on the given values, // print "Constructor 2" then invoke print() // add a descructor: print "Destruct", then invoke print() // make the function called multiply a FRIEND of this class // ------- class scNum Interface ------- // describes a number in scientific notation // example: 1.234 x 10^3 is mantissa=1.234 exp=3 class scNum { // describes a number in scientific notation public: void set(float m, int e); float getMantissa(); int getExponent(); void print(); private: void normalize(); // make it "proper" scientific notation float mantissa; // should be -10 int exp; // exponent of 10 }; // ------- class scNum Implementation ------- float scNum::getMantissa() { return mantissa; } int scNum::getExponent() { return exp; } void scNum::print() { cout = 1 && m 10) { m = m / 10.0; // move dec pt to the left exp++; // increase exponent } mantissa = sign * m; // put the sign back on } // normalize() // TODO: write a free function called "multiply" that is given two scNums, // multiplies them, and returns the result // result mantissa = mantissa1 * mantissa2 // result exponent = exponent1 + exponent2 // example: 1.5x10^2 + 2.0x10^3 = 3.0x10^5 // NOTE: as a friend of class scNum, you shouldn't have to use // get() or set() methods!! so don't!! int main() { { scNum a, b(2.5, 3), c; a.set(3.0, 2); cout Learning objectives: Implementing constructors and destructors for a class Implementing overloaded methods (constructors) for a class Declaring and using friends of a class Implementing functions/methods that return an object Specifications: Create a Lab 10 project in MS VS; add a main.cpp source file (it's the only one we'll use) Get a copy of the main.cpp file for this lab from the class website and start with that. Note: all classes and functions are written inside one.cpp. not a good idea for reusability of classes, but will make this lab easier. Now complete the program as follows: look for TODO in the comments Add the following to class scNum: o A constructor with no arguments that sets mantissa and exp to 0. It should print "constructor 1:" nvoke print() Print endl o A constructor with two arguments, given a mantissa and an exponent. It invokes set() on the given arguments, then prints "constructor 2: , invokes print() then prints end. o A destructor that prints "destruct: ", invokes print(), then prints endl. o Make the free function multiply) a friend of this class Write a free function called multiply() that is given two scNum objects, multiplies them, and returns the result. Since this function is a friend of the scNum class, you should NOT use set() and get() methods...access the mantissas and exponents directly How to multiply numbers in scientific notation: o Multiply the mantissas o Ex: 1.5 x 102 * 2.0 x 103 = 3.0 x 105
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
