Thread: Umm...Inheritance or something similar?

  1. #1
    Registered User
    Join Date
    Nov 2004

    Lightbulb Umm...Inheritance or something similar?

    I've read a few books on learning C++, and I have the basic idea of what OOP is all about, but I encountered a slight problem while writing a console based address book.

    I'm certainly not trying to take an entirely OO approach to it, since I'm not skilled enough to make that much use of it yet (I've only been programming for about a year off and on), but I do recall something about IS_A and HAS_A relationships, which I think is the key to my question.

    If I had to guess, I'd say list HAS_A contact.

    I've read up about it, but I didn't quite understand. I was wondering if someone could suggest an efficient solution to the following:

    class contact{
            contact(string *pName){
                cFirstName=(*pName); //Contacts must at LEAST have a first name
            string cFirstName;
            string cLastName;
            string cEmail;
            string cPhoneNumber;
    class list{
            list(string *newListName){
                loaded=true; // If there's an instance, it's been loaded.
            void unload(){
            void view(){
                    <<"Contact List: "<<listName<<endl;
                for (int i=0;i<(contactList.size());++i){
                    cout<<(i+1)<<". "<<contactList[i].cLastName<<", "
                cout<<" "<<endl; //Instructions for viewing a specific contact in detail goes here    
            bool loaded;
            string listName;
            vector<contact> contactList;
    My problem, is that list::view() iterates through vector<contact> contactList displaying each contact's names in the vector. The problem with this is that the contacts names and other details are protected data, so list can't legally access them.

    I could declare list as a friend of contact, but I was hoping to avoid that. Should I make functions that return the names of the contact? Or is there a better way to do this?

    Last edited by Callith; 11-26-2004 at 04:41 AM.

  2. #2
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Basically yes to both of your options. You can do it either way. Since you don't want to make a friend function then just write accessory functions to return the private datamembers;
    <return type> Class::accessorfunct(void)
    return privatemembervariable;
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    But perhaps a better way would be that the list asks the contact to display itself, using some kind of method.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Nov 2004
    Ah I decided what I would, which was using CornedBee's suggestion. Thanks everyone. ^^

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    The other thing is, if contact doesn't have any member functions (i.e. it's just used to hold a bunch of data), you can:
    a) Declare it as struct - preferred
    b) Declare it as class, but put everything as public.

    Since any data that is 'protected' (or private) is unusable and inaccessible anyways if the class has no member functions, there is really no point in creating a set of accessor functions for each member variable; you might as well just make it all public (i.e. declare it as a struct instead of class).
    Just Google It. √

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

  6. #6
    Registered User
    Join Date
    Nov 2004


    Well, in the previous code they didn't have member functions, but they do in my current code. I've worked on it a bit more and realized that's probably the best way to do it.... :P

    Anyways, it's working great so far. I can create a contact list, give it a name, create contacts, view the whole list, or view a specific contact, and edit contact details. I just finished writing how to delete the contacts, but I haven't tried compiling yet. O.o

    Saving and loading the lists is probably gonna take awhile. Streams aren't my strong point.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Forms of Inheritance
    By kolucoms6 in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2008, 03:09 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM