Here are 6 files I've been beating my head against the floor for about 20 hours now. I know I am sooooo close, but I just can't seem to find much information about overloading operators in a derived class to perform arithmetic operations on objects created in the base class. Hopefully you can point me in the right direction. I attached a MS Word document that contains the full error log from the compiler. It looks to me like the main program is not calling the overloaded operator function in the extRomanType derived class, but I'm not sure. The errors that C++ spits out are pretty rough for beginners to interpret.

Also, note that I have a bunch of stuff commented out. I'm focusing on just getting the addition operator overloaded first. Once I get that to work, the rest of the arithmetic operators will be easy. I also have some overloaded operator functions commented out in the base class. I built them and tested them just to prove to myself that I could overload an operator. They work just fine but I can't seem to overload an operator in the derived class to work on the base class objects. I'm totally baffled by this problem. I really need that light bulb to light up above my head but it just doesn't seem to be happening.

While I have your attention, is there anything special about overloading the increment and decrement operators? I haven't even tried those yet. I think my biggest problem right now is just getting the overloaded operator functions in the derived class to work on the base class objects.

Thanks in advance,

Dani

/*
* File:   main.cpp
* Author: Dani
* Description: CS270 Chapter 2, Problem 20.  Program that prompts the user for 2 Roman numerals
*      and will perform addition, subtraction, multiplication, and division on the two numbers,
*      then return the results in either Roman numeral or decimal format.
* Created on March 29, 2014, 6:22 AM
*/

#include <cstdlib>
#include <iostream>
#include <string>

#include "romanType.h"

using namespace std;  

int main() {

   string answer;

   //create objects
   romanType Rom1, Rom2, Rom3;

   //user input
   cout << "Please enter a Roman numeral: ";
   cin >> Rom1;
   cout << "Please enter a second Roman numeral: ";
   cin >> Rom2;
   cout << endl;

   cout << "*********************************************************************" << endl;
   cout << "*  This program will perform addition, subtraction, multiplication  *" << endl;
   cout << "*        and division on the two Roman numerals entered.            *" << endl;
   cout << "*********************************************************************" << endl << endl;

   //perform Roman to decimal conversions
   Rom1.setDecimal();
   Rom2.setDecimal();

   //some output to test romanToDecimal() function
   cout << "The first Roman numeral is: " << Rom1 << endl;
   cout << "The second Roman numeral is: " << Rom2 << endl;

   //addition
   Rom3 = Rom1 + Rom2;
   Rom3.decimalToRoman(Rom3.getDecimal());
   Rom3.setRoman();        
   answer = Rom3.getRoman();
   cout << Rom1 << " + " << Rom2 << " = " << answer << endl;

   //subtraction
   Rom3 = Rom1 - Rom2;
   Rom3.decimalToRoman(Rom3.getDecimal());
   Rom3.setRoman();
   answer = Rom3.getRoman();
   if(Rom1.getDecimal() < Rom2.getDecimal()){
       answer = "Since the first number was smaller than the second, this operation cannot be executed.";
       cout << answer<< endl;
   }
   else
       cout << Rom1 << " - " << Rom2 << " = " << answer << endl;

   //multiplication
   Rom3 = Rom1 * Rom2;
   Rom3.decimalToRoman(Rom3.getDecimal());
   Rom3.setRoman();
   answer = Rom3.getRoman();
   cout << Rom1 << " * " << Rom2 << " = " << answer << endl;

   //division
   Rom3 = Rom1 / Rom2;
   Rom3.decimalToRoman(Rom3.getDecimal());
   Rom3.setRoman();
   answer = Rom3.getRoman();
   if(Rom1.getDecimal() < Rom2.getDecimal()){
       answer = "Since the first number was smaller than the second, this operation cannot be executed.";
       cout << answer << endl;    
   }
   else
       cout << Rom1 << " / " << Rom2 << " = " << answer << endl;

   return 0;
}

Code 2

/*
* File:   romanType.cpp
* Author: Dani
*
* Created on March 29, 2014, 10:03 PM
*/

#include <cstdlib>

#include "romanType.h"

//constructor with parameter
romanType::romanType(string s, int i) {
   Roman = s;
   Decimal = i;
}

//default constructor
romanType::romanType(){
   Roman = "";
   Decimal = 0;
}

//function to overload << operator
ostream &operator<<(ostream & output, romanType & Rom){
   output << Rom.Roman;
   return output;
}

//function to overload >> operator
istream &operator>>(istream & input, romanType & Rom){  
   input >> Rom.Roman;
   return input;
}

//Function to set the object's decimal equivalent
void romanType::setDecimal(){
   Decimal = romanToDecimal(Roman);
}

void romanType::setRoman(){
   Roman = decimalToRoman(Decimal);
}

//function to get value of Roman numeral
string romanType::getRoman(){
   return Roman;
}

//function to get value of Decimal number
int romanType::getDecimal(){
   return Decimal;
}

//parse the char array and perform arithmetic operations
int romanType::romanToDecimal(string RN) {
   Dec = 0;
   Str = RN;
   for (int i = 0; i < Str.length(); i++) {
       switch (Str.at(i)) {

           case 'M': case 'm': Dec += 1000;
           break;

           case 'D': case 'd': Dec += 500;  
           break;

           case 'C': case 'c': Dec += 100;
           break;

           case 'L': case 'l': Dec += 50;
           break;

           case 'X': case 'x': Dec += 10;
           break;

           case 'V': case 'v': Dec += 5;
           break;

           /*the following is to determine whether or not the 'i' character is
            * to be added or subtracted and the value of 'i' at the time. ie: 1,
            * 10, or 100
            */
           case 'I': case 'i': Dec += 1;
           if (Str.at(i + 1) == 'V' || Str.at(i + 1) == 'v'){
               Dec -= 2;
           }
           if (Str.at(i + 1) == 'X' || Str.at(i + 1) == 'x'){
               Dec -= 2;
           }
           if (Str.at(i + 1) == 'L' || Str.at(i + 1) == 'l'){
               Dec -= 11;
           }
           if (Str.at(i + 1) == 'C' || Str.at(i + 1) == 'c'){
               Dec -= 11;
           }
           if (Str.at(i + 1) == 'D' || Str.at(i + 1) == 'd'){
               Dec -= 101;
           }
           if (Str.at(i + 1) == 'M' || Str.at(i + 1) == 'm'){
               Dec -= 101;
           }
           break;
       }
   }
   return Dec;
}

//function to convert decimal number to a Roman numeral
string romanType::decimalToRoman(int DN) {
   Rom = "";
   Dec = DN;
   numLetters = 0;

   //determine how many thousands
   if(Dec >= 1000){
       numLetters = (Dec / 1000);

       for(int i = 0; i < numLetters; i++){
           Rom += 'M';
       }
       Dec %= 1000;
   }
   //determine how many hundreds
   if(Dec >= 100){
       numLetters = (Dec / 100);
       if(numLetters == 9){
           Rom += "CM";
       }
       else if (numLetters >= 5){
           Rom += 'D';
           for (int i = 0; i < numLetters - 5; i++){
               Rom += 'C';
           }
       }
       else if (numLetters == 4){
           Rom += "CD";
       }
       else if (numLetters >= 1){
           for(int i = 0; i < numLetters; i++){
               Rom += 'C';
           }
       }
       Dec %= 100;
   }
   if(Dec >= 10){
       numLetters = (Dec / 10);
       if(numLetters == 9){
           Rom += "XC";
       }
       else if (numLetters >= 5){
           Rom += 'L';
           for(int i = 0; i < numLetters - 5; i++){
               Rom += 'X';
           }
       }
       else if (numLetters == 4){
           Rom += "XL";
       }
       else if (numLetters >= 1){
           for(int i = 0; i < numLetters; i++){
               Rom += 'X';
           }
       }
       Dec %= 10;      
   }
   if(Dec >= 1){
       numLetters = (Dec / 1);
       if(numLetters == 9){
           Rom += "IX";
       }
       else if (numLetters >= 5){
           Rom += 'V';
           for(int i = 0; i < numLetters - 5; i++){
               Rom += 'I';
           }
       }
       else if (numLetters == 4){
           Rom += "IV";
       }
       else if (numLetters >= 1){
           for(int i = 0; i < numLetters; i++){
               Rom += 'I';
           }
       }
   }  
   return Rom;
}

//overload + operator
//romanType romanType::operator+(romanType& r){
//    romanType romanType;
//    romanType.Decimal = this->Decimal + r.Decimal;
//    return romanType;
//}

//overload - operator
//romanType romanType::operator-(romanType& r){
//    romanType romanType;
//    romanType.Decimal = this->Decimal - r.Decimal;
//    return romanType;
//}

//overload * operator
//romanType romanType::operator*(romanType& r){
//    romanType romanType;
//    romanType.Decimal = this->Decimal * r.Decimal;
//    return romanType;
//}

//overload / operator
//romanType romanType::operator/(romanType& r){
//    romanType romanType;
//    romanType.Decimal = this->Decimal / r.Decimal;
//    return romanType;
//}

Code 3

/*
* File:   romanType.h
* Author: Dani
*
* Created on March 29, 2014, 10:03 PM
*/

#include <iostream>
#include <string>

using namespace std;

#ifndef ROMANTYPE_H
#define ROMANTYPE_H

class romanType {

protected:
   int Dec;                 // temporary decimal value in romanToDecimal()
   string Roman;            // used in constructor to hold original Roman numeral
   string Str;              // temporary string used to hold Roman numeral in romanToDecimal()
   string Rom;              // temporary string used to hold Roman numeral in decimalToRoman()
   int Decimal;             // used in constructor to hold decimal number
   int numLetters;          // number of each Roman numeral character in decimalToRoman()

public:
   //constructor
   romanType(string, int);

   //default constructor
   romanType();

   //function to overload insertion operator
   friend ostream & operator<<(ostream & output, romanType & Rom);

   //function to overload extraction operator
   friend istream & operator>>(istream & input, romanType & Rom);

   //function to set decimal
   void setDecimal();

   //function to set Roman numeral
   void setRoman();

   //function to get Roman numeral
   string getRoman();

   //function to get Decimal number
   int getDecimal();

   //function to convert Roman numeral to Decimal equivalent
   int romanToDecimal(string);

   //function to convert decimal numeral to Roman equivalent
   string decimalToRoman(int);

   //overload + operator
   //romanType operator+(romanType& r);

   //overload - operator
   //romanType operator-(romanType& r);

   //overload * operator
   //romanType operator*(romanType& r);

   //overload / operator
   //romanType operator/(romanType& r);

   //overload ++ operator
   //romanType operator++(int romanType&);

   //overload -- operator
   //romanType operator--(int romanType&);

};

#endif

Code 4

/*
* File:   extRomanType.cpp
* Author: Dani
* Description: derived class of romanType that overrides operators
* Created on March 30, 2014, 10:21 PM
*/

#include <cstdlib>

#include "extRomanType.h"


//constructor
extRomanType::extRomanType() {
   Decimal = 0;
}

//overload constructor
extRomanType::extRomanType(int d){
   Decimal = d;
}

//overload + operator
extRomanType extRomanType::operator+(extRomanType& Rom){
   extRomanType tempRoman;
   tempRoman.Decimal = Decimal + Rom.Decimal;
   return(tempRoman);
}

//overload - operator
//extRomanType extRomanType::operator -(extRomanType& tempRom){
//    tempRom.Decimal = Decimal - tempRom.Decimal;
//    return tempRom;
//}

//overload * operator
//extRomanType extRomanType::operator *(extRomanType& tempRom){
//    tempRom.Decimal = Decimal * tempRom.Decimal;
//    return tempRom;
//}

//overload / operator
//extRomanType extRomanType::operator /(extRomanType& tempRom){
//    tempRom.getDecimal() = Decimal / tempRom.Decimal;
//    return tempRom;
//}

//overload ++ operator
//extRomanType extRomanType::operator ++(int extRomanType&){

//}

//overload -- operator
//extRomanType extRomanType::operator --(int extRomanType&){

//}

Code 5

/*
* File:   extRomanType.h
* Author: Dani
* Description: header file for class extRomanType
* Created on March 30, 2014, 10:10 PM
*/

#include "romanType.h"

#ifndef EXTROMANTYPE_H
#define EXTROMANTYPE_H

class extRomanType: public romanType {

protected:
   int Decimal;

public:
   //constructor
   extRomanType();

   //overload constructor
   extRomanType(int);

   //overload + operator
   extRomanType operator+(extRomanType&);

   //overload - operator
   //friend extRomanType operator-(extRomanType&);

   //overload * operator
   //friend extRomanType operator*(extRomanType&);

   //overload / operator
   //friend extRomanType operator/(extRomanType&);

   //overload ++ operator
   //extRomanType operator++(int extRomanType&);

   //overload -- operator
   //extRomanType operator--(int extRomanType&);
};

#endif /* EXTROMANTYPE_H */

Word Document:

Error log:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf

make[1]: Entering directory `/cygdrive/c/Users/Kevin/Documents/NetBeansProjects/CS270_Ch2_Ex20'

"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/cs270_ch2_ex20.exe

make[2]: Entering directory `/cygdrive/c/Users/Kevin/Documents/NetBeansProjects/CS270_Ch2_Ex20'

mkdir -p build/Debug/Cygwin-Windows

rm -f build/Debug/Cygwin-Windows/extRomanType.o.d

g++ -c -g -MMD -MP -MF build/Debug/Cygwin-Windows/extRomanType.o.d -o build/Debug/Cygwin-
Windows/extRomanType.o extRomanType.cpp

mkdir -p build/Debug/Cygwin-Windows

rm -f build/Debug/Cygwin-Windows/main.o.d

g++ -c -g -MMD -MP -MF build/Debug/Cygwin-Windows/main.o.d -o build/Debug/Cygwin-Windows/

main.o main.cpp

nbproject/Makefile-Debug.mk:73: recipe for target `build/Debug/Cygwin-Windows/main.o' failed

make[2]: Leaving directory `/cygdrive/c/Users/Kevin/Documents/NetBeansProjects/CS270_Ch2_Ex20'

main.cpp: In function `int main()':

nbproject/Makefile-Debug.mk:61: recipe for target `.build-conf' failed

make[1]: Leaving directory `/cygdrive/c/Users/Kevin/Documents/NetBeansProjects/CS270_Ch2_Ex20'

main.cpp:46: error: no match for 'operator+' in 'Rom1 + Rom2'

main.cpp:53: error: no match for 'operator-' in 'Rom1 - Rom2'

main.cpp:65: error: no match for 'operator*' in 'Rom1 * Rom2'

main.cpp:72: error: no match for 'operator/' in 'Rom1 / Rom2'

make[2]: *** [build/Debug/Cygwin-Windows/main.o] Error 1

make[1]: *** [.build-conf] Error 2

nbproject/Makefile-impl.mk:39: recipe for target `.build-impl' failed

make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 1s)

You're trying to use the + operator with objects of type RomanType, here:
Rom3 = Rom1 + Rom2;
but the code for that operator is commented out. For starters, uncomment it.

You've got code for that operator on the extRomanType, but your code doesn't ever use objects of that type.

I suggest you delete the files extRomanType.h and extRomanType.cpp, uncomment out all the operator code in the romanType.h and romanType.cpp, fix the errors in that code (i.e. where your do this romanType romanType; you should probably have something like romanType temp;) and then it will work.

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.