Thread: do i understand inheritence properly

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    do i understand inheritence properly

    ok i just want to type this code out , and want someone to tell me if i have the correct understanding of inheritence..
    Code:
    #include <iostream>
    using namespace std;
    class a
    {
    public:
    int t;
    a(){t =1;} //constructor 
    };
    
    class b : public a
    {
    public
    
    };
    
    now i could access t varible through b like this?
    or if there was more varible or functions in a could access them through b
    
    int main()
    {
    b b;
    cout<<b.t<<endl;
    return 0;
    }
    Last edited by Anddos; 11-16-2005 at 06:59 AM.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Well you understand what is going on in that case. Inheritance is more "complicated" than that. It's more about saving code and such. Also when you are writting a class it is common pratice to have your data private so nothing but the class it is in can touch it aka encapsalation(sp).
    Woop?

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    yeh but if i put t in private then i wouldent be able to do
    cout<<b.t<<endl;

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Correct, which is what you want. Your data members are part of the implementation of your class. You generally want to encapsulate your data members by making them private and adding interface methods so that outside code can access those members. Even though the derived class is also a base class object, it is considered outside code and should not have access to the internal implementation of the base class. So you would make t private, and add a method called get_t() or something like that which would return the value of t. Then you could do cout << b.get_t() << endl;

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    so get is a function for class?
    to access private data

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can add functions to a class. You can name them whatever you want (I named my example get_t). You can do whatever you want with them.

    In general, you should prefer to write a public function that returns the value of a private member variable, rather than making the member variable public. One convention is to call these methods get methods, but it is only a convention and is not required.

    The reasoning is that in slightly more complex classes, it is possible that you might want to change how you implement the class. Maybe you don't want to store t in a variable, maybe you want to calculate it from other variables. By having a get method, you can change the get method to do the calculation, and all the code that uses the get method doesn't have to change.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    sometimes i wonder why there is such thing as class , everything could be coded as global and still do the same thing in away
    because you just put all the varibles in global and then use them in any functions etc
    Last edited by Anddos; 11-16-2005 at 01:14 PM.

  8. #8
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    sometimes i wonder why there is such thing as class , everything could be coded as global and still do the same thing in away
    I know what you mean. I've worked with people who wonder why there are such things as c compilers, since everything can be done in assmebly anyway.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  9. #9
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    >> sometimes i wonder why there is such thing as class , everything could be coded as global and still do the same thing in away
    because you just put all the varibles in global and then use them in any functions etc

    lol .. now that would be a mess

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 03:14 AM
  2. C++ system("rm") not deleting properly?
    By fattysmo in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2008, 11:37 AM
  3. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 10:46 PM
  4. How to properly use a .lib file?
    By Kurisu33 in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2006, 08:19 PM
  5. tic tac toe not working properly
    By h_howee in forum Game Programming
    Replies: 2
    Last Post: 01-01-2006, 01:59 AM