So it's been a while since I've coded (a quarter) and I'm taking a data structures II class. I have an assignment where I have to implement a C++ Class named LongInt that uses an embedded internal private data member of type STL list<char> to
represent the list of each of the single digit characters in the long unsigned integer value. The various methods of the STL list object can be used to insert characters into a LongInt variable (e.g., push_front(…), push_back(…)), determine the current number of items in the list (size(…) ), and iterate through and access the items in the list with iterators or reverse_iterators. Your class must include appropriate constructors, a size() method, an operator+=(…) method, an operator*=(…) method, and friend functions for operators >>, <<, +, *, and ==.

I've got the basic header and source file outlined (look below) but I am confused on how to implement the class functions. It's really worrying me just how much I forgot about coding so any help would really...REALLY be appreciated.

Thanks.

Header:

// LongInt.h 
// Embedded STL <list>  
#pragma once 
#include <iostream> 
#include <list> 
#include <string> 
using namespace std; 
class LongInt 
{
public: 
 LongInt(void); 
 LongInt(const string v); 
 ~LongInt(void); 
 // Size of LongInt value in characters 
 int size(void); 
 friend LongInt operator+(const LongInt& x, const LongInt& y); 
 friend LongInt operator*(const LongInt& x, const LongInt& y); 
 LongInt& operator+=(const LongInt& rhs); 
 LongInt& operator*=(const LongInt& rhs); 
 friend ostream& operator<<(ostream& out, const LongInt& v); 
 friend istream& operator>>(istream& in, LongInt& v); 
 friend bool operator==(const LongInt& x, const LongInt&y); 
private: 
 list<char> val; 
 };

Source file:

#include <iostream> 
#include <iomanip> 
using namespace std; 
#include "LongInt.h" 
int main(int argc, char *argv[]) 
 }
 LongInt x, y; 
  cout << “Initially x = “ << x << endl;               //0 
 cout << "LongInt Testing" << endl; 
 cout << "Enter x: "; 
 cin >> x;        //82738392000900333454432483992432
 cout << "Enter y: "; 
 cin >> y;        //8485938378494837372832442
 int maxlen = x.size(); // determine length of largest value 
 if(maxlen < y.size())  maxlen = y.size(); 
 ++maxlen;  // Leave room for carry out 
 cout << "  x = " << setw(maxlen) << x << endl;   //82738392000900333454432483992432
 cout << "  y = " << setw(maxlen) << y << endl;    //8485938378494837372832442
 LongInt sum = x + y; 
 LongInt prod = x * y; 
 cout << "sum = " << setw(maxlen) << sum << endl;  //82738400486838711949269856824874
 cout << "prod = " << setw(maxlen) << prod << endl; 
 // Test == method 
 if(x == y)  cout << "[x == y]" << endl; 
 else        cout << "[x != y]" << endl; 
 // Test "On the fly" addition 
 cout << "fly = " << LongInt("12345") + LongInt("99") << endl; 
 // Test += method 
 x = LongInt("12345");  x += LongInt("99"); 
 cout << "  x = " << x << endl; 
 y = LongInt("12345");  y *= LongInt("99"); 
 cout << "  y = " << y << endl; 
 return(0); 
}

Recommended Answers

All 3 Replies

I am confused on how to implement the class functions

That's really the trick, isn't it? Have you worked out the logic for these operations and just have trouble putting it into code, or are you drawing a complete blank on how to proceed?

Complete blank! :(

Advice on how to implement the functions would be counter-productive if you're at the "complete blank" stage. You can't implement anything if you don't know what each function needs to do, which means sticking comments above each function describing what it does, what parameters it needs, and what it returns. I assume if you're at "completer blank" stage that someone provided you these function headers. I also assume that each char in the val class varible represents one digit from 0 to 9? How are they ordered? Is the first digit the most significant or the least? You can't do anything until this is decided.

My first function to write is just about always a constructor and a display function, so that'd be your << operator. Might as well write the destructor while you're at it. I think you'll want to comment out most of main and uncomment it as you write and test each part of the program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.