Question
Coding (finish in 20min) Update the member function theValue() of SuperInt class so that SuperInt can be put on the left hand side of an
Coding (finish in 20min)
Update the member function theValue() of SuperInt class so that SuperInt can be put on the left hand side of an assignment statement using theValue(). That is, theValue() can be use for assignment, e.g., theValue() = 4; Note that in this case, theValue() is referred to as the lvalue since it appears no the left-hand-side of the assignment statement.
Create a new class "SuperSuperInt," which inherits the "SuperInt" class. Please declare and define this class in separate .h and .cpp files, and DO NOT update SuperInt.h and SuperInt.cpp directly. Please name your new files as "SuperSuperInt.cpp" and "SuperSuperInt.h."
Add one member function theValue() to SuperSuperInt.
Code the member function theValue() of SuperSuperInt to return the value of data member "myValue" in SuperInt. Please make sure the returned value of theValue() CANNOT be put on the left hand side of an assignment statement.
ScopeEtc.cpp
#include
#include "SuperInt.h"
using namespace std;
static SuperInt var(0, "global");
int main()
{
cout << "At start of main()" << endl;
cout << "var's value is " << var.theValue() << endl << endl;
SuperInt var2(1, "Main");
cout << "var2's value is " << var2.theValue() << endl << endl;
cout << "Entering top block" << endl;
{
SuperInt var2(2, "block");
cout << "var2's value is " << var2.theValue() << endl << endl;
}
cout << "Exiting top block" << endl << endl;;
cout << "Entering bottom block" << endl;
{
static SuperInt var2(2, "static");
cout << "var2's value is " << var2.theValue() << endl << endl;
}
cout << "Exiting bottom block" << endl << endl;
int i = var2.theValue();
SuperInt* pvar = new SuperInt(3, "dynamic");
cout << "At end of main()" << endl;
}
superInt.cpp
#include
#include "SuperInt.h"
using namespace std;
// Name helps us see which variable
SuperInt::SuperInt(int init, const char *name): myValue(init), myName(name)
{
cout << "Created a SuperInt called " + myName << endl;
}
SuperInt::~SuperInt()
{
cout << "Destroyed a SuperInt called " + myName << endl;
}
// For demonstrating lvalues
const int& SuperInt::theValue()
{
cout << "Getting value (can't use as lvalue) of a SuperInt called " + myName << endl;
myValue = 3;
return myValue;
}
SuperInt.h
#include
class SuperInt {
public:
// Name helps us see which variable
SuperInt(int init, const char *name);
~SuperInt();
// For demonstrating lvalues
//int& theValue(void);
const int& theValue(void);
private:
int myValue;
std::string myName;
// Neat trick: making this private prevents creating unitialized
// objects
SuperInt();
};
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started