Thread: operator overloading and dynamic memory program

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    Exclamation operator overloading and dynamic memory program

    I started writing the class for the program but am unsure if I'm doing it correctly. Can someone look at it and give me some advice and examples? Also, how would I define an operator overload? Is my dynamic array in my private section declared correctly, how would I use it?

    Here's what the program should do, followed by what I have:

    Task: One common limitation of programming languages is that the built-in types are limited to 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.

    Ceate 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 in a program). Provide operator overloads, so that objects of type MyInt will act like regular integers, to some extent (To make them act completely like regular integers, it would take overloading most of the operators available, which we will not do here). 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).

    Class must storage of non-negative integers of any size.

    My class:

    Code:
    class Myint
    
    {
    
    friend Myint operator+(const MyInt& x , const MyInt& y);
    friend Myint operator*(const MyInt& x , const MyInt& y);
    friend bool operator < (const MyInt& x , const MyInt& y);
    friend bool operator > (const MyInt& x , const MyInt& y);
    friend bool operator <= (const MyInt& x , const MyInt& y);
    friend bool operator >= (const MyInt& x , const MyInt& y);
    friend bool operator = =(const MyInt& x , const MyInt& y);
    friend bool operator != (const MyInt& x , const MyInt& y);
    friend ostream &operator  << (ostream &x, const MyInt &y);
    friend istream &operator >> (istream &x, const MyInt &y);
    
    public:
    
    MyInt (int n = 0); //constructor number 1
    MyInt (); //default constructor
    ~MyInt(); //user defined destructor
    MyInt(const MyInt &); //copy constructor
    const MyInt &operator = (const MyInt &); //assignment operator
    
    private:
    
    int*MyInt = new int [SIZE]; dynamic array used to store digits

    cpp file:

    Code:
    #include <iostream>
    #include "myint.h"
    
    using namespace std;
    
    int C2I(char c)
    // converts character into integer (returns -1 for error)
    {
       if (c < '0' || c > '9')	return -1;	// error
       return (c - '0');				// success
    }
    
    char I2C(int x)
    // converts single digit integer into character (returns '\0' for error)
    {
       if (x < 0 || x > 9)		return '\0';	// error
       return (static_cast<char>(x) + '0'); 	// success
    }
    
    friend MyInt::Myint operator+(const MyInt& x , const MyInt& y)
    {
    }
    
    friend MyInt::Myint operator*(const MyInt& x , const MyInt& y)
    {
    }
    
    friend MyInt::bool operator < (const MyInt& x , const MyInt& y)
    {
    }
    
    friend MyInt::bool operator > (const MyInt& x , const MyInt& y)
    {
    }
    
    friend MyInt::bool operator <= (const MyInt& x , const MyInt& y)
    {
    }
    
    friend MyInt::bool operator >= (const MyInt& x , const MyInt& y)
    {
    }
    
    friend MyInt::bool operator = =(const MyInt& x , const MyInt& y)
    {
    }
    
    friend MyInt::bool operator != (const MyInt& x , const MyInt& y)
    {
    }
    friend MyInt::ostream &operator  << (ostream &x, const MyInt &y)
    {
    }
    friend MyInt::istream &operator >> (istream &x, const MyInt &y)
    {
    }
    There should be a constructor that expects a regular int parameter, with a default value of 0 (so that it also acts as a default constructor). If a negative parameter is provided, set the object to represent the value 0. Otherwise, set the object to represent the value provided in the parameter. There should be a second constructor that expects a C-style string (null-terminated array of type char) as a parameter. If the string provided is empty or contains any characters other than digits ('0' through '9'), set the object to represent the value 0. Otherwise, set the object to represent the value of the number given in the string (which might be longer than a normal int could hold).

    Note that the two constructors described above will act as "conversion constructors" This simply means that these constructors will allow automatic type conversions to take place when using certain code statements that mix variables of type MyInt with regular int variables or character strings. This makes our operator overloads more versatile, as well.

    Since dynamic allocation is necessary, you will need to write appropriate definitions of the special functions (the "automatics"): destructor, copy constructor, assignment operator. The destructor should clean up any dynamic memory when a MyInt object is deallocated. The copy constructor and assignment operator should both be defined to make a "deep copy" of the object (copying all dynamic data, in addition to regular member data), using appropriate techniques. Make sure that none of these functions will ever allow memory "leaks" in a program.

    If you use a dynamic array, you will need to allow for resizing the array when needed. There should never be more than 5 unused array slots at any given time.

    Operator overloads: (The primary functionality will be provided by the following operator overloads).

    Create an overload of the insertion operator << for output of numbers. This should print the number in the regular decimal (base 10) format -- and no extra formatting (no newlines, spaces, etc -- just the number).

    Create an overload of the extraction operator >> for reading integers from an input stream. This operator should ignore any leading white space before the number, then read consecutive digits until a non-digit is encountered (this is the same way that >> for a normal int works, so we want to make ours work the same way). This operator should only extract and store the digits in the object. The "first non-digit" encountered after the number may be part of the next input, so should not be extracted. You may assume that the first non-whitespace character in the input will be a digit. i.e. you do not have to error check for entry of an inappropriate type (like a letter) when you have asked for a number.

    Create overloads for all 6 of the comparison operators ( < , > , <= , >= , == , != ). Each of these operations should test two objects of type MyInt and return an indication of true or false. You are testing the MyInt numbers for order and/or equality based on the usual meaning of order and equality for integer numbers.

    Create an overload version of the + operator to add two MyInt objects. The meaning of + is the usual meaning of addition on integers, and the function should return a single MyInt object representing the sum.

    Create an overload version of the * operator to multiply two MyInt objects. The meaning of * is the usual meaning of multiplication on integers, and the function should return a single MyInt object representing the product.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "I started writing the class for the program but am unsure if I'm doing it correctly."

    Get a book on Beginning C++ or read some online tutorials on classes. Start with easy classes that have one member variable, and a couple of simple functions, and learn how those work. You're going to need to program a few examples to get the hang of it. Then learn about constructors, destructors, copy constructors, and assignment operators and the pitfalls you encounter with dynamic memory allocation. Finally, learn about operator overloading. Before you learn those things, you can't do the program you're trying. If you study hard and you're reasonably intelligent, you can learn those things in 1 or 2 weeks.

    Your code demonstrates you don't know the basics about classes. Sorry, those are the cold, hard facts.
    Last edited by 7stud; 04-07-2003 at 01:46 AM.

  3. #3
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    I think he has a book or a class because it seems like he's doing homework...
    If you ever need a hug, just ask.

  4. #4
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Okay, you sure are asking alot, I'll help with a litte.

    Code:
    // Dynamically allocating memory
    
    private:
    char *pointer; // Pointer to string
    ...
    
    // Constructor
    MyInt(char Int_String[], int size)
    {
       int i = 0;
    
        pointer = new int[size];
        pointer = Int_String;
    
    
        while(Int_String[i] != 0)
         {
            if(isDigit(Int_String[i]))
              {
                *pointer = "0";
                break;
              }
          }
    }
    Something like that for dynamic memory allocation, you just have empty functions, give it a try!
    Last edited by CheesyMoo; 04-09-2003 at 01:52 PM.
    If you ever need a hug, just ask.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heap corruption with dynamic memory
    By Head In Jar Man in forum C++ Programming
    Replies: 8
    Last Post: 01-14-2009, 02:58 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Issues with free and dynamic memory
    By dan s in forum C Programming
    Replies: 3
    Last Post: 01-14-2007, 03:44 AM
  4. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM