Kamal_Java 0 Newbie Poster

HI,

I am new to C++ and i need help on this topic . Overloading streams . I feel this very difficult to learn after i have learnt java.

I have a Base class Called BX

class BX
{
public:
    BX( );
    BX( string X, string Y );

    // 2 set function here 
    
    virtual void setType( )  { };

    friend ofstream& operator << (ofstream& os, const BX& e );
    friend ifstream& operator >> (ifstream& is, BX& e );
    friend ostream& operator << (ostream& os, const BX& e );
    friend istream& operator >> (istream& is, BX& e );

protected:
    string type;
    
private:
    string X;
    string y;    
};

and two derived class D1 and D2.

class D1 : public BX
{
public:
    D1( );
    D1( string X, string Y, int d3 ,.... ); // few other paramter of this calls

    void setType( );

    friend ostream& operator << (ostream& os, const D1& m );
    friend istream& operator >> (istream& is, D1& m );
    friend ofstream& operator << (ofstream& os, const D1& m );
    friend ifstream& operator >> (ifstream& is, D1& m );

    
private:
    void setSnow( ); // this is a private function that sets the
                     // bool snow variable to true or false, depending
                     // on the value stored in height

    int X1
    bool X2;
    string X3;
    string X4;
};

And a list through which i have ti insert all

list<BX*> eList;

Now i want to read in the contents of a data file ( in text ) that contains some information and create a list from this data.

Sample of text file :

BX        //character indicating the class of the record
X          // varible for base class
Y          // Varible for derived class
Z
A           

DX        //character indicating the class of the record
X          // varible for base class
Y          // Varible for derived class
Z
A

etc.

In My Main Program i have opened this file using ifstream and now i need to overload this to read input file.

Note : My list is : list<BX*> eList;

string str;
Environ* e;
while( getline( fin, str ) ) // fin is ifstream object
{
if( str[0] == 'BX' ) // Mountain
{
e = new Mountain( );

fin >> dynamic_cast<Mountain&>(*e);

// how do i do this in my derived class
eList.push_back( e );
}
if( str[0] == 'DX' ) // Mountain
{
e = new Mountain( );

fin >> dynamic_cast<Mountain&>(*e);

// how do i do this in my derived class
eList.push_back( e );
}

It is also mentioned in my derived class (i.e overloaded function) that
* This function must include a call to the overloaded input (file)
operator in the BX(Base) class to read in those values that are
actually stored in the Environ class.

How do i do that . Thanks

Help me !!!


}

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.