Thread: help writing function definitions

  1. #1
    Registered User
    Join Date
    Oct 2002

    Exclamation help writing function definitions

    I tried writing most of my program but I'm stuck on the definitions. Can someone show me an example of a definition? Here's my program:

    #ifdef MYINT1_H
    #define MYINT1_H
    class MyInt
    friend ostream &operator  << (ostream & x, const MyInt &y);
    friend istream &operator >> (istream & x, const MyInt &y);
    friend Myint operator+(const MyInt& x , const MyInt& y);
    friend Myint operator*(const MyInt& x , const MyInt& y);
    friend bool operator < (const MyInt &) const;
    friend bool operator <= (const MyInt &)const; 
    friend bool operator = =(const MyInt & ) const;
    friend bool operator ! (const MyInt & )const;
    friend bool operator != ( const MyInt & right) const
    return !(*this = = right);
    friend bool operator > (const MyInt &right) const
    return ! (right < *this);
    friend bool operator >= (const MyInt &right) const
    return ! (right > *this);
    friend bool operator <= (const MyInt &right) const
    return ! (right < *this);
    MyInt (int n = 0); //constructor number 1
    MyInt(char Int_String[]) //constructor 2
    MyInt(const MyInt &); //copy constructor
    const MyInt &operator = (const MyInt &);
    char *pointer; // Pointer to string

    CPP File

    #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;
       return (c - '0');
    char I2C (int x)
    if (x < 0 || x > 9)     return '\0';
    return (static_cast <char>(x) + '0';
    MyInt::MyInt(int n=0)
       num = 0;
      return num;
    MyInt::MyInt(const char Int_String[])
     int i = 0;
        pointer = Int_String;
        while(Int_String[i] != 0)
                *pointer = "0";
    MyInt::MyInt (const MyInt &copy)
       delete [] *pointer;
    MyInt::const MyInt &operator = (const MyInt &right)
    if (&right != this){
    delete[] ptr;
    return *this;
    ostream &operator  << (ostream & x, const MyInt &y)
    return output;
     istream &operator >> (istream & x, const MyInt &y)
    return input;
    Myint operator+(const MyInt& x , const MyInt& y)
    Myint operator*(const MyInt& x , const MyInt& y)

    Here are the program details:

    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.

    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 in a program). You will also provide some 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).

    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. You can get a copy of the starter files here: myint.h and myint.cpp. I have also provided a sample main program to assist in testing, along with a link to some sample test runs from my version of the class. The sample main program is linked at the bottom of this specification. Details and Requirements are listed below, followed by some hints and tips on some of the items.

    Details and Requirements:

    1) Your class must allow for storage of non-negative integers of any size. While not the most efficient in terms of storage, the easiest method is to use a dynamic array, in which each array slot is one decimal "digit" of the number. (This is the suggested technique). You may go ahead and assume that the number of digits will be bound by the values allowed in an unsigned int varible (i.e. you could keep track of the number of digits in such a variable). You should create appropriate member data in your class. All member data must be private.

    2) 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" (described in the Deitel book on pg. 568). 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. For example, the conversion constructor allows the following statements to work (assuming appropriate definitions of the assignment operator and + overloads described later):

    MyInt x = 1234;
    MyInt y = "12345";
    MyInt z = x + 12;

    3) 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.

    4) 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).

    5) 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).

    6) 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.

    Example: Suppose the following code is executed, and the input typed is " 12345 7894H".

    MyInt x, y;
    char ch;
    cin >> x >> y >> ch;

    The value of x should now be 12345, the value of y should be 7894 and the value of ch should be 'H'.

    7) 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.

    8) 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.

    9) 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.

    10) General Requirements: as usual, no global variables other than constants, all member data should be private, use appropriate good programming practices as denoted on previous assignments. Since the only output involved with your class will be in the << overload, your output should match mine exactly when running test programs.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Here's and example of a function definition:

    friend bool operator != ( const MyInt & right) const
    return !(*this = = right);

    It seems to me you've gotten in way over your head with this program. Your trying to overload operators, but your code demonstrates that you don't know the basics about classes. Read some beginning online tutorials about classes, and do a few simple programs, so that you learn the basics before proceeding any further.

    When you write a program, you should write one function at a time, and then test it in main() to ensure that it works correctly. Then you do the same thing with the second function until you finish with all your functions. Finally, write your code in main() and then compile it . If instead you write your whole program and then run it, you are going to get 100's of errors that can be difficult to track down.
    Last edited by 7stud; 04-10-2003 at 09:30 PM.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003

    Re: help writing function definitions

    Originally posted by jlmac2001
    Your task will be to create a class...
    If you have specific problem, we will be more than happy to help. "I'm stuck on definitions" does not sufficiently explain your problem that you would like help with. Posting all the details of your assignment doesn't help either.

    Here are all the errors in your posted code - you should work on understanding compiler errors and how to fix them...
    - "Myint" should be "MyInt"...look for it
    - operator< and operator<= must be member functions (ie not friends)
    - non-member functions (ie friends) cannot be const..."friend void foo() const; <-- wrong
    - friend operator==() requires two parameters (and can't have space between "==")
    - non-member functions (ie friends) cannot access "this" - refer to your text book to get the proper declaration of overloaded operators.
    - you overloaded operator<= twice...
    - missing ";" on "constructor 2"
    - here is how you implement a member function "return_type classname::function_name(<parameters>) { }"


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM