Thread: Any Tips on learning C++??

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are aware that those lines do absolutely nothing?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    70
    Quote Originally Posted by Elysia View Post
    You are aware that those lines do absolutely nothing?
    Well i created member variables so that the user can assign values to them.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, but those specific lines of code do exactly nothing. They serve no purpose whatsoever.
    You created those variables in your class definition. And you assign them later on in the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    70
    Quote Originally Posted by Elysia View Post
    Yes, but those specific lines of code do exactly nothing. They serve no purpose whatsoever.
    You created those variables in your class definition. And you assign them later on in the code.
    Oh i think i know what you mean, are you saying that i can just take those lines out and the program would still work fine since those member variables are already defined in the class definition?

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, that's right.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    May 2010
    Posts
    70
    Ahhhh thanks a lot :], hey i was wondering if you could explain public and private members and functions to me please?im reading my book right now but, i dont really get it.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Let's take the car analogy again.
    Your car has lots of components. An engine, an electrical system and who knows what else.
    But the engineers of the car don't want you to go poking around in its internals, right? You hit the gas pedal and the car accelerates. You don't go around poking in the injectors or such.
    Therefore, it would be convenient to allow you to use the gas pedal, but not access the injectors and whatnot.
    We call the pedals the the interface. The functions that you use to use the functionality of the object. These functions are public.
    The other stuff, like the injectors and things that shouldn't be accessed by you are private.

    So to translate that into C++, basically you create an object from a class. Then you use its members. But we only want the users of the class to access certain members. So the members they can access are public, and those that they cannot are private.

    Code:
    class MyClass
    {
    public:
        void foo() {}
    private:
        void bar() {}
    };
    
    MyClass m;
    m.foo(); // OK: foo is public.
    m.bar(); // Compile error: bar is private.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Registered User
    Join Date
    May 2010
    Posts
    70
    Quote Originally Posted by Elysia View Post
    Let's take the car analogy again.
    Your car has lots of components. An engine, an electrical system and who knows what else.
    But the engineers of the car don't want you to go poking around in its internals, right? You hit the gas pedal and the car accelerates. You don't go around poking in the injectors or such.
    Therefore, it would be convenient to allow you to use the gas pedal, but not access the injectors and whatnot.
    We call the pedals the the interface. The functions that you use to use the functionality of the object. These functions are public.
    The other stuff, like the injectors and things that shouldn't be accessed by you are private.

    So to translate that into C++, basically you create an object from a class. Then you use its members. But we only want the users of the class to access certain members. So the members they can access are public, and those that they cannot are private.

    Code:
    class MyClass
    {
    public:
        void foo() {}
    private:
        void bar() {}
    };
    
    MyClass m;
    m.foo(); // OK: foo is public.
    m.bar(); // Compile error: bar is private.
    So i dont get why object "m" cant access m.bar(), isnt it an instance of class MyClass?

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    "m" isn't trying to access bar(); you, the user, is trying to access it. And you are not "m". Therefore it yields a compile error.
    OTOH, this works:
    Code:
    class MyClass
    {
    private:
        void bar() {}
    public:
        void foo() { bar(); }
    };
    
    MyClass m;
    m.foo(); // OK: foo is public.
    It works because the call to bar is inside foo which is inside the class. So basically, if MyClass is the car, you've basically told it to accelerate and it calls appropriate functions inside itself to make this happen.
    But you can't tell it to pump more gasoline directly (as the user), because that function is private. OTOH, the car can do it since it's its internals.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Registered User
    Join Date
    May 2010
    Posts
    70
    so what im wondering is who CAN access private members then?

  11. #26
    Registered User
    Join Date
    May 2010
    Posts
    70
    Oh wait so im reading in my book, private functions can only be called upon by other functions in the class?

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cuo741
    Oh wait so im reading in my book, private functions can only be called upon by other functions in the class?
    And friend functions, and member functions of friend classes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Registered User
    Join Date
    May 2010
    Posts
    70
    Quote Originally Posted by laserlight View Post
    And friend functions, and member functions of friend classes.
    What do you mean friend functions??

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A class can say "this function is my friend". They are called friend functions. Similarly to classes. I'm sure your book will discuss them in due time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cuo741
    What do you mean friend functions??
    You'll find out if you are sociable enough with your book.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Machine Learning with Lego Mindstorms
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-30-2009, 02:34 PM
  2. Any tips for learning c?
    By skelectron in forum C Programming
    Replies: 3
    Last Post: 11-20-2005, 11:07 AM
  3. Need Help On a Simple Bank Program
    By oobootsy1 in forum C# Programming
    Replies: 9
    Last Post: 08-08-2005, 10:51 AM
  4. Fun Learning a New Language
    By UnregdRegd in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-30-2003, 10:03 PM
  5. API gurus: learning tips?
    By Shadow12345 in forum Game Programming
    Replies: 3
    Last Post: 06-10-2002, 05:32 PM