c++

This is a discussion on c++ within the C++ Programming forums, part of the General Programming Boards category; where should we use a 'friend' class and when should inheritance be used?...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    1

    Smile c++

    where should we use a 'friend' class and when should inheritance be used?

  2. #2
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    friend function, not class. You declare a function friend when you want to give it access to private data members of more than one class.

    I suck at OOP

    edit:and I/O

  3. #3
    Banned
    Join Date
    May 2008
    Location
    Four Dots Planet
    Posts
    72
    Quote Originally Posted by sheelvant singh View Post
    where should we use a 'friend' class and when should inheritance be used?
    use inheritance in bedroom and friend in kitchen.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> friend function, not class.
    You can have friend classes as well.

    >> where should we use a 'friend' class and when should inheritance be used?
    These are vague questions that aren't really related. Are you asking about scenarios where both will work?

  5. #5
    Banned
    Join Date
    May 2008
    Location
    Four Dots Planet
    Posts
    72
    daved put it more clear that is what i wanted to indicate that be more specific because we are not supposed to lecture you on the topic of friend versus inheritance.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Inheritance is when two or more classes can "share" protected data members or functions. This is helpful in some circumstances if you are creating multiple classes which will have similar attributes.

    Code:
    class Mamal 
    {
    protected:
       int age;
       std::string name;
    };
    
    // dog class now have access to age and name
    class Dog : public Mamal
    I'm just trying to be a better person - My Name Is Earl

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Read the C++ FAQs regarding inheritance, especially the ones regarding proper inheritance and substitutability, otherwise you'll be back here in a few days asking how to use dynamic_cast. Inheritance is more than just re-using functions and data, but unfortunately it is often abused.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21