Using an overloaded operator of a base class

Discussion in 'OT Technology' started by BinaryMadman, Mar 17, 2007.

  1. BinaryMadman

    BinaryMadman New Member

    Joined:
    Jul 27, 2005
    Messages:
    54
    Likes Received:
    0
    Location:
    Texas
    Hi, I have a derrived class and a base class. The base class overrides the '>>' operator but the derrived class needs to override the '>>' operator as well. How do I make it so the program first uses the overloaded '>>' operator of the base class then allows the derrived class to do some additional '>>' overriding?

    some pseudo code:
    Code:
    
    (the main file)
    bigBoxType bigBox;
    
    cin << bigBox;
    
    (bigBoxType.cpp) <--Derrived class
    
    istream& operator>>(istream& is, bigBoxType& aBigBox)
    {
      is>>aBigBox.color;
    }
    
    (boxType.cpp)  <--Base class
    
    istream& operator>>(istream& is, boxType& aBox)
     {
       is>>aBox.height;
      is>>aBox.width;
     }
    
    The person would type "10 15 blue" at the "cin << bigBox;" part.
    The base class would grab "10 15" and the derrived class would grab the "blue" part.

    I hope my attempt to simplify it didn't confuse you guys. :big grin: Thanks in advance! :)
     
  2. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    with the cin and cout operators its weird as far as overloading goes with base classes since you have to return ostream or istream. with normal operators like = youd do this in your derived class to call the base classes = operator

    Code:
    is boxType::operator=aBigBox;
    your example is weird because youre doing cin << aBigBox but your overloads are for cout?? youre trying to print all the member values or something right?
     
    Last edited: Mar 17, 2007
  3. BinaryMadman

    BinaryMadman New Member

    Joined:
    Jul 27, 2005
    Messages:
    54
    Likes Received:
    0
    Location:
    Texas
    Oh, that was a typo. I meant to write "cin >> aBigBox;". The way the program is supposed to work is all I would have to do is type in one variable of a class for both cin and cout in the main file and the classes would handle the rest through overloading the input and output streams. I've been able to get it to work with other classes but not for the derrived
    class. I'll do a more complete example (only contains important parts) of what I've actually done:

    Code:
    (main.cpp)
    #include "personType.h"
    
    int main()
    {
         personType person;
         cout << "Enter a first name, last name, and SSN seperated by spaces:\n" << endl;
         cin >> person;
    
         cout << "The contents of " << person.Name << "is: \n\n";
         cout << person << endl;
    }
    
    
    Code:
    (personType.h)
    #include "nameType.h"
    
    class personType
    {
        friend ostream& operator<<(ostream&, const personType&);
        friend istream& operator>>(istream&, personType&);
    private:
         int SSN;
         nameType name;
    }
    
    Code:
    (personType.cpp)
    #include "personType.h"
    
    ostream& operator<<(ostream& os, const personType& aPerson)
    {
        os.setf(ios::left, ios::adjustfield);
        os<<aPerson.SSN;
        os<<aPerson.name;
        
        return os;
    }
    
    istream& operator>>(istream& is, personType& aPerson)
    {
        is>>aPerson.SSN;
        is>>aPerson.name;
        return is;
    }
    
    Code:
    (nameType.h)
    
    class nameType
    {
         friend ostream& operator<<(ostream&, const nameType&);
         friend istream& operator>>(istream&, nameType&);
    private:
         char firstName[25];
         char lastName[25];
    }
    
    Code:
    (nameType.cpp)
    ostream& operator<<(ostream& os, const nameType& aName)
    {
        os<<aName.firstName<<" ";            // print the first name
        os<<aName.lastName;                  // print the last name
        return os;
    }
    
    istream& operator>>(istream& is, nameType& aName)
    {
        is>>aName.firstName;
        is>>aName.lastName;
        return is;
    }
    
    All that works as supposed to right now. It's when I make a derrived class of personType that messes things up (the derrived class overloades << and >> operators as well). It only does what's in the derrived classes overloaded operator and doesn't do what's in personType.
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    The base class operator is not supposed to be invoked automatically. You might want to "call" the base class operator from the operator of the derived class.

    not sure if it works, but something like...

    istream& operator >> (istream& is, derivedType &x)
    {
    is >> (baseType)x; //cast to base class
    is >> x.foo;
    }
     
  5. BinaryMadman

    BinaryMadman New Member

    Joined:
    Jul 27, 2005
    Messages:
    54
    Likes Received:
    0
    Location:
    Texas
    I do the same for the output stream, right? Well, it runs but the data from the casting to personType isn't preserved. It returns empty values for the name and SSN up until I use a member from the derrived class, which returns the correct value.
     
  6. BinaryMadman

    BinaryMadman New Member

    Joined:
    Jul 27, 2005
    Messages:
    54
    Likes Received:
    0
    Location:
    Texas
    skinjob, you were pretty much on there. I got it to work with help from the professor:
    Code:
        personType& aPerson = (personType&)aStudent;
        is>>aPerson;
        is>>aStudent.deptName;
        return is;
    
    Thank you guys for all the help though. :)
     

Share This Page