Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

myint.cpp : Here are some sample runs: Sample Run 1: You can take your time and

ID: 3854498 • Letter: M

Question

myint.cpp:

Here are some sample runs:

Sample Run 1:

You can take your time and update me in the comments.

Thanks a lot for your assistance!

This C++ dynamic memory allocation inside a class. Program consists of: operator overloading, as well as experience with managing Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C+ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes) is limited to the range 0 through 4,294,967,295. Heavy scientific computing applications often have the need for computing with larger numbers than the capacity of the normal integer types provided by the system. In C++, the largest integer type is long, but this still has an upper limit Your task will be to create a class, called MyInt, which will allow storage of any non-negative integer (theoretically without an upper limit -- although there naturally is an eventual limit to storage ina program). You will also provide some operator overloads, so that objects of type MyInt will act like regular integers, to some extent. There are many ways to go about creating such a class, but all will require dynamic allocation (since variables of this type should not have a limit on their capacity) The class, along with the required operator overloads, should be written in the files "myint.h" and "myint.cpp". I have provided starter versions of these files, along with some of the declarations you will need. You will need to add in others, and define all of the necessary functions. Here are the files:

Explanation / Answer

main.cpp


#include <iostream>
#include "myint.h"

using namespace std;

MyInt Fibonnaci(MyInt num);

int main()
{

// demonstrate behavior of the two constructors and the << overload

MyInt x(12345), y("9876543210123456789"), r1(-1000), r2 = "14H67", r3;
char answer;
cout << "Initial values: x = " << x << " y = " << y
       << " r1 = " << r1 << " r2 = " << r2 << " r3 = " << r3 << " ";

// demonstrate >> overload

cout << "Enter first number: ";
cin >> x;
cout << "Enter second number: ";
cin >> y;

cout << "You entered: ";
cout << " x = " << x << ' ';
cout << " y = " << y << ' ';

// demonstrate assignment =
cout << "Assigning r1 = y ... ";
r1 = y;
cout << " r1 = " << r1 << ' ';

// demonstrate comparison overloads
if (x < y)   cout << "(x < y) is TRUE ";
if (x > y)   cout << "(x > y) is TRUE ";
if (x <= y)   cout << "(x <= y) is TRUE ";
if (x >= y)   cout << "(x >= y) is TRUE ";
if (x == y)   cout << "(x == y) is TRUE ";
if (x != y)   cout << "(x != y) is TRUE ";

// demonstrating + and * overloads
r1 = x + y;
cout << "The sum (x + y) = " << r1 << ' ';
r2 = x * y;
cout << "The product (x * y) = " << r2 << " ";
cout << "The sum (x + 12345) = " << x + 12345 << ' ';
cout << "The product (y * 98765) = " << y * 98765 << ' ';

// create Fibonacci numbers (stored as MyInts) using +
cout << " Assuming that the Fibonnaci sequence begins 1,1,2,3,5,8,13..."
       << " The 10th Fibonnaci number   = " << Fibonnaci(10)
       << " The 100th Fibonnaci number = " << Fibonnaci(100)
       << " The 1000th Fibonnaci number = " << Fibonnaci(1000)
       << " The 2000th Fibonnaci number = " << Fibonnaci(2000)
       << " ";

}

MyInt Fibonnaci(MyInt num)
{
   MyInt n1 = 1, n2 = 1, n3;
   MyInt i = 2;
   while (i < num)
   {
   n3 = n1 + n2;
   n1 = n2;
   n2 = n3;
   i++;
   }
   return n2;
}

myint.cpp

#include <iostream>
#include "myint.h"

using namespace std;

MyInt Fibonnaci(MyInt num);

int main()
{

// demonstrate behavior of the two constructors and the << overload

MyInt x(12345), y("9876543210123456789"), r1(-1000), r2 = "14H67", r3;
char answer;
cout << "Initial values: x = " << x << " y = " << y
       << " r1 = " << r1 << " r2 = " << r2 << " r3 = " << r3 << " ";

// demonstrate >> overload

cout << "Enter first number: ";
cin >> x;
cout << "Enter second number: ";
cin >> y;

cout << "You entered: ";
cout << " x = " << x << ' ';
cout << " y = " << y << ' ';

// demonstrate assignment =
cout << "Assigning r1 = y ... ";
r1 = y;
cout << " r1 = " << r1 << ' ';

// demonstrate comparison overloads
if (x < y)   cout << "(x < y) is TRUE ";
if (x > y)   cout << "(x > y) is TRUE ";
if (x <= y)   cout << "(x <= y) is TRUE ";
if (x >= y)   cout << "(x >= y) is TRUE ";
if (x == y)   cout << "(x == y) is TRUE ";
if (x != y)   cout << "(x != y) is TRUE ";

// demonstrating + and * overloads
r1 = x + y;
cout << "The sum (x + y) = " << r1 << ' ';
r2 = x * y;
cout << "The product (x * y) = " << r2 << " ";
cout << "The sum (x + 12345) = " << x + 12345 << ' ';
cout << "The product (y * 98765) = " << y * 98765 << ' ';

// create Fibonacci numbers (stored as MyInts) using +
cout << " Assuming that the Fibonnaci sequence begins 1,1,2,3,5,8,13..."
       << " The 10th Fibonnaci number   = " << Fibonnaci(10)
       << " The 100th Fibonnaci number = " << Fibonnaci(100)
       << " The 1000th Fibonnaci number = " << Fibonnaci(1000)
       << " The 2000th Fibonnaci number = " << Fibonnaci(2000)
       << " ";

}

MyInt Fibonnaci(MyInt num)
{
   MyInt n1 = 1, n2 = 1, n3;
   MyInt i = 2;
   while (i < num)
   {
   n3 = n1 + n2;
   n1 = n2;
   n2 = n3;
   i++;
   }
   return n2;
}

myint.h

#ifndef MYINT_H
#define MYINT_H

#include <iostream>
#include <string>
using namespace std;

class MyInt
{
    public:

        MyInt(int n = 0);
        MyInt(const char* n);
        MyInt(const MyInt &n);
        ~MyInt();

        friend ostream& operator<<(ostream &out, const MyInt &myInt);
        friend istream& operator>>(istream& input, MyInt &myInt);

        // Operators overloading
        void operator=(const MyInt &myInt);
        MyInt operator+(const MyInt &myInt);
        MyInt operator-(const MyInt &myInt);
        MyInt operator*(const MyInt &myInt);
        MyInt& operator++();       // Prefix increment operator.
        MyInt operator++(int);     // Postfix increment operator.

      
        friend bool operator<(const MyInt &a, const MyInt &b);
        friend bool operator<=(const MyInt &a, const MyInt &b);
        friend bool operator>(const MyInt &a, const MyInt &b);
        friend bool operator>=(const MyInt &a, const MyInt &b);
        friend bool operator==(const MyInt &a, const MyInt &b);
        friend bool operator!=(const MyInt &a, const MyInt &b);

    private:
        // class fields
        int* value;
        int length;

        void init(const string &s);
        MyInt add(const MyInt &a, const MyInt &b);
        MyInt sub(const MyInt &a, const MyInt &b);
        MyInt mul(const MyInt &a, const MyInt &b);

        // class function
        static string toString(const MyInt &n);
        static int compare(const MyInt &a, const MyInt &b);
};

#endif