CPSCI 12T-Obycct Oricntcd Programming Spring 2017 Program #100 duc Thursday, 4/2
ID: 3821679 • Letter: C
Question
CPSCI 12T-Obycct Oricntcd Programming Spring 2017 Program #100 duc Thursday, 4/20 by 11:00 pm on Titanium Write a class, called Number, that will hold one integer value in the range of 0-999 Write your code in three files, the class declaration, the class definitions, and a main program that will test all of the class functions. When finished, compress these 3 files (2 cpp files and h file) into a zipped file and submit the zip filc Since you are wr ng the main and the class, the exact function names are up to you The data field must be private, but all functions will be public. Write operators as member functions when possible. Class Number specifications: Private data members Value field an integer in the range of 0-999 Public member functions: default constructor: Sets the number to be 0 constructor receiving a value: Checks the parameter value to make sure ha s in the righ range. If it is in the right range, store it to the private data member. Store a value of 0 or end the program ifthe parameter value is bad copy constructor: Receive one Number object as a parameter and make the new object a copy of that parameter object. Set function: This function receives an integer value. If the parameter value is in the range of 0-999, store it to the private data member. Store a value of 0 or end the program if the parameter value is bad Get function: Return a copy of the private data field Print function Writes out the Number object's private data value in word form. (ex. 938 would be written out as nine hundred thirty eight 512 would be written out as five hundred twelve' Operators: Receives a Number parameter and returns another Number that has the sum of the two objects' private data (ex. A+B will return an object with the sum of A's value and B's value as its private data value) Same as but subtract instead of add Receives a Number object and returns true if the left hand operand's value is greater than the right hand operand's value. Otherwise it returns false Receives a Number object and returns true if the left hand operand's value is less than or equal to the right hand operand value, otherwise it returns false prefix Should add one to the private data field and return a copy of the changed object postfix: Should add one to the private data field and return a copy of the original object as it was before the addition was performed. Receives a reference to the input stream and a Number object. Read an integer in from the keyboard. Ifthe integer is in the range of 0-999, store it to the private data member of the Number parameter. Store a value of 0 or end the program if the parameter value is bad. Return a reference to the input stream Receives a reference to the output stream and a Number object Write out the Number's private data as an integer. Return a reference to the output streamExplanation / Answer
I couldn't find the option to attach files for the answer.
I'm sending you the individual files. Place each of the files in the same directory, for them to work.
Number.h
#ifndef NUMBER_H
#define NUMBER_H
#include <iostream>
#include "Number.cpp"
using namespace std;
class Number
{
private:
int val;
public:
Number();
Number(int value);
Number(Number &X);
void SetNum(int value);
int GetNum();
void PrintWord();
Number operator+(Number&);
Number operator-(Number&);
// void operator = (const Number );
bool operator>(Number&);
bool operator<=(Number&);
Number operator++();
Number operator++(int);
friend ostream& operator<<(ostream &output, const Number&);
friend istream& operator>>(istream &output, const Number&);
};
#endif
Number.cpp
#include <string>
#include <iostream>
#include <math.h>
// #include "Number.h"
using namespace std;
class Number
{
private:
int val;
public:
Number();
Number(int value);
Number(Number &X);
void SetNum(int value);
int GetNum();
void PrintWord();
Number operator+(const Number&);
Number operator-(const Number&);
// void operator = (const Number );
bool operator>(const Number&);
bool operator<=(const Number&);
Number operator++();
Number operator++(int);
friend ostream& operator<<(ostream &output, const Number&);
friend istream& operator>>(istream &output, Number&);
};
Number::Number(){
this->val = 0;
}
Number::Number(int value){
this->val = value;
if(this->val <= 999 && this->val >=0)
return;
else
this->val = 0; //set it to 0
}
Number::Number(Number &X){
this->val = X.val;
}
void Number::SetNum(int value){
if(value >= 0 && value <= 999)
this->val = value;
else
this->val = 0; //set it to 0
}
int Number::GetNum(){
return val;
}
// void Number::operator=(const Number &num){
// this->SetNum(num.val);
// }
string toword(int num)
{
int l = (int)log10(num) + 1;
if(num == 0) l = 1;
if (l >= 4) {
//the range is only for 3digits numbers.
return "";
}
string single[] = { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};
string two[] = {"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
string tens[] = {"", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};
if (l == 1) {
return single[num];
}
else if(l == 2){
string ret = "";
if(num/10 == 1)
ret += two[num%10];
else if(num/10 > 1){
ret += tens[num/10];
if(num%10) ret += " " + single[num%10];
}
return ret;
}
else{
string ret = single[num/100] + " hundred";
if((num/10)%10 == 1)
ret += " " + two[num%10];
else if((num/10)%10 > 1){
ret += " " + tens[(num/10)%10];
if(num%10) ret += " " + single[num%10];
}
else
if(num%10) ret += " " + single[num%10];
return ret;
}
}
void Number::PrintWord(){
cout << toword(this->val);
}
Number Number::operator+(const Number& right){
Number num;
num.SetNum(this->val + right.val);
return num;
}
Number Number::operator-(const Number& right){
Number num;
num.SetNum(this->val - right.val);
return num;
}
bool Number::operator>(const Number& right){
return this->val > right.val;
}
bool Number::operator<=(const Number& right){
return this->val <= right.val;
}
Number Number::operator++(){
SetNum(this->val + 1);
Number t(this->val);
return t;
}
Number Number::operator++(int){
int old = this->val;
SetNum(old+1);
Number t(old);
return t;
}
ostream& operator<<( ostream &output, const Number& num){
output << num.val;
return output;
}
istream& operator>>(istream &input, Number& num){
int in;
input >> in;
num.SetNum(in);
return input;
}
main.cpp
#include <iostream>
#include "Number.cpp"
using namespace std;
int main(){
/*
Number();
Number(int value);
Number(Number &X);
SetNum(int value);
GetNum();
PrintWord();
Number operator+(Number&);
Number operator-(Number&);
bool operator>(Number&);
bool operator<=(Number&);
Number operator++();
Number operator++(int);
friend ostream& operator<<(ostream &output, const Number&);
friend istream& operator>>(istream &output, const Number&);
*/
//first has the value 0
//second has the value 10
//Default Constructor and Parameterised Constructor
Number first, second(10);
cout << "first:" << first << " second:" << second << endl;
cout << "Setting first as 5 ";
first.SetNum(5); //first has the value of 5
cout << "first:" << first << " second:" << second << endl;
//inputstream and Get Number function
cout << " Enter a new value for second:";
cin >> second;
cout << "second:" << second.GetNum() << endl;
//operator + and - overloading
cout << " Setting third as first + second" << endl;
Number third, fourth;
third = first + second; //third has the sum of first and second
cout << "Setting fourth as second - first" << endl;
fourth = second - first;
//postfix and prefix increment
cout << "third:" << third << " fourth:" << fourth << endl;
cout << " incrementing third and fourth using postfix and prefix respectively ";
cout << "third:" << third++ << " fourth:" << ++fourth << endl;
cout << "third > fourth ?" << (third > fourth) << endl;
cout << "third <= fourth ?" << (third <= fourth) << endl;
Number fifth(fourth); //copy constructor
cout << " Fifth has been initialised from fourth Fifth:" << fifth << endl;
cout << "Value of fifth in words: ";
fifth.PrintWord();
cout << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.