Thread: Inheritance, istream, class datamember

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    48

    Inheritance, istream, class datamember

    Hi.. I am having a slight problem with one of my projects and was wondering if anyone could help. I do not understand why this is not working like it should.

    Here is my class(this is a base class for a referencing class).

    date.h
    Code:
    #include <iostream>
    using namespace std;
    
    class date  { 
            private:
                    int month;
                    int day;
                    int year;
            public:
                    date();
                    date(int,int,int);
    
    
    friend istream &operator >>(istream &in, date &d);
    };
    date.cpp
    Code:
    #include "date.h"
    #include <iostream>
    
    using namespace std;
    
    date::date() : month(0), day(0), year(0) {}
    
    date::date(int month, int day, int year) {
    }
    
    istream &operator >>(istream &in, date &d) {
            in >> d.day >> d.month >> d.year;
            return in;
    }
    Referencing class
    insurance.h
    Code:
    #include <string>
    #include <iostream>
    #include "date.h"
    
    using namespace std;
    
    class insurance: public date {
            protected:
                    string firstName;
                    string lastName;
                    char gender;
                    date birthDate;
            protected:
                    static int total;
            public:
                    insurance();
                    insurance(string, string, char, date);
                    void readInfo(istream &in);          
    };
    insurance.cxx
    Code:
    #include "insurance.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    insurance::insurance() : firstName(""), lastName("") {}
    
    insurance::insurance( string firstName, string lastName, char gender,
                                                            date birthDate) {}
    
    void insurance::readInfo(istream &in) {
            in >> firstName >> lastName >> gender >> birthDate;
    }

    So, in main when I call the insurance.readinfo(inFile); function, I'm assuming that the stream for the date members should be readinto the date's class datamembers?... However, im unsure if they are being read, or if im just having a problem accessing them..

    Would I need to create a member function to "GET" the data members stored in the "insurance.birthDate" Date object? if so how would this be implemented? each time I try to create something like this I get errors saying the datamembers are private...

    I want to eventually just use cout like
    Code:
    cout << getDay(birthDate) << "\n";
    to get it to work?

    any suggestions?

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    ok, well I have decided to do this in my insurance.h file:

    Code:
    class insurance: public date {
          private:
          date birthDate;
          public:
          int getDay() {return birthDate.day;}
          int getMonth() {return birthDate.month;}
          int getYear() {return birthDate.year;}
    }
    Why am i getting these errors? How can I access the private data members in the birthDate data object holding the data that was read in from istream?

    If I create the getSTUFF member functions in date.h, I get null ouput when I call the functions, im assuming because all of the data is in the birthDate object.


    Code:
    date.h: In member function `int insurance::getDay()':
    date.h:9: error: `int date::day' is private
    insurance.h:25: error: within this context
    date.h: In member function `int insurance::getMonth()':
    date.h:8: error: `int date::month' is private
    insurance.h:26: error: within this context
    date.h: In member function `int insurance::getYear()':
    date.h:10: error: `int date::year' is private
    insurance.h:27: error: within this context
    make: *** [car.o] Error 1
    Last edited by guda; 09-29-2004 at 08:49 PM.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>How can I access the private data members in the birthDate data object holding the data that was read in from istream?

    Without looking at the rest of your code, I'm thinking that it's because you declared it as private... which is, frankly, private. Possible solutions:
    a) Declare it as protected, not private
    b) If that doesn't work, declare insurance as a friend class of date.

    Protected allows derived classes to access the data member while preventing access from mofos in the outside world. However, although in this way the derived class can access 'its own' members that were inherited from the base, I don't know if the access extends to allowing the derived class to access the protected members of an object of the base class, which is why I also suggested the second solution. Don't do the friend thing unless you need to, because it tends to messify code
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    Unfortunately I cant declare it as protected (project guidelines) so declaring the calss as a friend will work, and does work for that matter, now I just have to get it to print out the correct data :-).

    thanks.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Public inheritance models an IS-A relationship, but I highly doubt that insurance IS-A date. So why does insurance inherit from date? In fact, you don't even seem to be using the inherited date data, because you also have a member variable of type date.

    I doubt that this is what you want to do. It would make more sense in my opinion to remove the inheritance completely.

    As far as your problem (which is separate from the inheritance because you weren't using the inheritance in any way that I could tell), I would suggest not making insurance a friend. It is not necessary. Just make public accessor functions inside the date class similar to the ones you tried inside the insurance class. Then you can call those functions from the accessor functions of the same name inside the insurance class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Inheritance with pointers:Overloading?
    By katie in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2004, 01:26 PM
  3. istream class
    By bond_007 in forum C++ Programming
    Replies: 0
    Last Post: 01-11-2003, 04:18 PM
  4. Error returning istream in class implementation.
    By joshdick in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2002, 03:02 PM
  5. Need some help on class inheritance
    By HelpMe in forum C++ Programming
    Replies: 1
    Last Post: 05-21-2002, 03:44 PM