Thread: Sereious Question: Best way to learn C++

  1. #31
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    When you're saying things like "copies of these classes" I think that means we're in trouble. A class is a data type, just like int, float, double, and struct foo. You have variables of those data types, but they are no more related to each other than two int variables are related to each other. (Those variables are often called objects, when the variable is of a class type, but that's just fancy language.)
    I told you this is where I fall flat on my face...

    Is is then fair to say that "Class" equates loosely to "TypeDef" ?

    I'm wanting to think a class is similar to a Pascal Unit....
    Code:
    // unit file
    unit : FOO;
    
    Interface: 
    function1
    function 2
    procedure 1
    
    Implementation:
    variable a 
    variable b
    
    function 1 
     begin 
       //stuff 
     end.
    ...
    
    
    // main file
    uses FOO;
    
    z :  integer;
    
    Begin
    // stuff
    
    z = function1();
    end.
    In this construct the stuff in the Interface is visible the Implementation is not... like .h and .c files in C code...

    So Classes are nothing like that?

  2. #32
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    They have a little bit of the same organization (implementation/interface - ish), but the use case seems totally different. Basically, whenever you have a "thing" (i.e. an object) that is an instance of a class. For example, a std::string is a class, in the C++ standard library. It has some internal data about which we know very little, and it has things it knows how to do (member functions, like find a substring or such like, or do the [] notation to get a particular character). (You can see the full list at string - C++ Reference if you want.)

  3. #33
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Actually you CAN do OOP within plain C. If you Google "Object Oriented Ansi C" you will get a pdf download as the first result. A 200 page book talking about how it's done. Libraries like GTK were built using OOP in plain C.

    Yes, it's similar to that.. here is a kind of example of a player character class I have been working on.

    C | #include <stdio.h> #include <stdlib.h> typedef

    edit:

    You would of course separate all structs/functions and what not into their own "class" header and source file and try to lock down variables to that source file... creating a kind of encapsulation. This was just a test source file messing with OOP concepts in plain C.
    Last edited by \007; 01-09-2011 at 09:39 AM.

  4. #34
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    They have a little bit of the same organization (implementation/interface - ish), but the use case seems totally different. Basically, whenever you have a "thing" (i.e. an object) that is an instance of a class. For example, a std::string is a class, in the C++ standard library. It has some internal data about which we know very little, and it has things it knows how to do (member functions, like find a substring or such like, or do the [] notation to get a particular character). (You can see the full list at string - C++ Reference if you want.)
    From the tutorial:
    If a class is a house, then the functions will be the doors and the variables will be the items inside the house. The functions usually will be the only way to modify the variables in this structure, and they are usually the only way even to access the variables in this structure.
    For me that's not describing a TYPE so much as a LIBRARY or UNIT...

  5. #35
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Guys... I'm sorry if I'm frustrating you... but this has been the problem all along.

    It seems everytime I think I understand this, I find out it's something else entirely.

  6. #36
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by CommonTater View Post
    From the tutorial:


    For me that's not describing a TYPE so much as a LIBRARY or UNIT...
    Then I guess you need to distinguish between the two. A type is the kind of object you can have. You can have an int, a float, or a house.

    A library is a big ol' wodge of code that does something that you (usually) don't get to see inside of, and use as a black box. The GSL from GNU, for instance.

  7. #37
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well if you're being anything it's probably steadfast. For my donation to the conversation, I'm just trying to convince you that there's programming here. I hope my contribution isn't proving to confuse what other people are saying.

    Quote Originally Posted by CommonTater View Post
    So in lingo this old brain can absorb... Methods are function calls?
    They've never been anything but function calls. Just fancy ones, with a dot operator.

    So... my list might look like this... (in very rough pseudo code)
    Code:
    Class Users
       array of username structs;   // not seen outside the class
    public   
       bool CallUser(username);    // visible outside the class
       bool AddUser(username, ipaddr);
    ...
    Then to call a user from my Window class I might say...
    Code:
      If (Users.CallUser("Fred Smith"))
       { OpenChatWindow(); }
    So... if that's a private function call inside the Window class .... how does the Window class know about the Users class?
    That's the trouble. A User variable would have to be in scope, whether you passed it as an argument to the private function, or else.

    Basically a function call from inside the class to a function in a different class?

    See this is where I've never seen a good and proper explanation... I've always wondered how the heck these things talk to each other...
    Depends on what you mean by inside. Public access is for everyone. The functions that are under that access label will do the communicating. You can call these member functions from anywhere, in places like main(), and inside other classes if necessary. For example, Window can use User::CallUser() as long as that function is public and there is an User variable in scope.

    I'll leave explaining what a class is, compared to concepts in other langs to other people, since once you get that hopefully what we've been talking about makes sense.

  8. #38
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by whiteflags View Post
    Well if you're being anything it's probably steadfast. For my donation to the conversation, I'm just trying to convince you that there's programming here. I hope my contribution isn't proving to confuse what other people are saying.
    LOL... you're no worse than anyone else...

    They've never been anything but function calls. Just fancy ones, with a dot operator.
    That's what I thought... Just imbedded into an object/blob of some sort.

    That's the trouble. A User variable would have to be in scope, whether you passed it as an argument to the private function, or else.

    Depends on what you mean by inside. Public access is for everyone. The functions that are under that access label will do the communicating. You can call these member functions from anywhere, in places like main(), and inside other classes if necessary. For example, Window can use User::CallUser() as long as that function is public and there is an User variable in scope.
    Ok, lets assume I have an instance of Users in my main function and a second one in the Window class as a private variable (am I expressing this correctly?)... both are "in scope" but are two separate copies... Will a change to the Users data list in the Main be immediately reflected into the one in Window? That is, are they sharing the same data or does each have it's own separate copy?


    I'll leave explaining what a class is, compared to concepts in other langs to other people, since once you get that hopefully what we've been talking about makes sense.
    Please do... If I can get get my head around this far enough to make sense of the C++ tutorials, I will consider that a minor miracle.... lets not complicate it with other languages...
    (For instance... I have flatly no interest in Java or Python whatsoever.)

  9. #39
    Registered User blackcoder41's Avatar
    Join Date
    Jan 2010
    Location
    Philippines
    Posts
    14
    You can imagine class a master plan or blue print of a car. It's just on paper, and it describes how the car should be created. The same model of cars can be created from a master plan but with different colors right? Just as a person have eyes, but your eyes might not be the same color as your cousins eye.

    Example
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Person
    {
        string  m_name;
        int     m_strength;
        int     m_health;
    
        public:
            Person(string name) : m_name(name), m_strength(10), m_health(100)
            {
                cout << m_name << " is born." << endl;
            }
    
            void Exercise()
            {
                m_strength++;
                m_health++;
            }
    
            void SmokeWeed()
            {
                m_strength--;
                m_health-=5;
            }
    
            void Status()
            {
                cout << "\nName:\t\t" << m_name << "\nStrength:\t" << m_strength
                     << "\nHealth:\t\t" << m_health << "\n\n";
            }
    
            virtual ~Person()
            {
                cout << m_name << " just died." << endl;
            }
    };
    
    int main(int argc, char* args[])
    {
        Person bill("Bill Gates");
        bill.Status();
        bill.Exercise();
        bill.Status();
        bill.SmokeWeed();
        bill.Status();
        return 0;
    }
    Here the person have a name "Bill Gates", and you can create another person with different name right? I just think that the member functions or methods are what the object can do, like walking, sleeping, etc. And the member variables are the object's characteristics or properties.

    If you initiated two persons here let's say bill and paul., it wouldn't affect paul if bill smoke weed but it affects bill's health.

    Hope that helps

  10. #40
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Not sure if my C example I posted helped or confused you, but it's really not too hard to wrap your head around.

    A class is a container basically. It holds data and methods. The programmer's job is simplified by allowing the programmer to work with the classes interface rather than coding or memorizing an entire library or something of that sort.

    C | #include <stdio.h> #include <stdlib.h> typedef

    The plain C program I posted above showed a "Class" of a player character, which has 2 variables, a name and a level. You could create an object (instantiate the class) by declaring it. I will show another example here in plain C.

    PC *bob = malloc(sizeof(PC)); // (C++ would do this mallocing for you)

    Then I have to initialize this new object, (C++ has constructors to do this kind of thing for you, but in C we would have to do it manually).

    bob->init = newPC; // This set up the init function
    bob->init(bob); // This initializes the player character bob

    bob->setname(bob, "bob"); // we used the setname method in the PC class to set bob's name

    bob->delPC(bob); // We freed bob's malloc'ed memory... C++ has destructors to do this for you

  11. #41
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    That is, are they sharing the same data or does each have it's own separate copy?
    Normally two instances of a class will not share data between them. Example:

    Code:
    #include <iostream>
    
    class YourClass
    {
       public:
          int yourVar;
    };
    
    int main()
    {
       YourClass a;
       YourClass b;
    
       b.yourVar = 1000;
       a.yourVar = 1;
    
       std::cout << b.yourVar << " " << a.yourVar << std::endl;
       b.yourVar = 34;
       std::cout << b.yourVar << " " << a.yourVar << std::endl;
    
    
       return(0);
    }
    Notice that the change of the variable b.yourVar does not change a.yourVar.

    Jim

  12. #42
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by CommonTater View Post



    Ok, lets assume I have an instance of Users in my main function and a second one in the Window class as a private variable (am I expressing this correctly?)... both are "in scope" but are two separate copies... Will a change to the Users data list in the Main be immediately reflected into the one in Window? That is, are they sharing the same data or does each have it's own separate copy?
    I'm not sure what confusion of ideas has led you to even ask the question. You know structs in C, right? Look at a linked list. If every node_item shared the same data field, and the same next_pointer field, where would we be?

  13. #43
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    Now... as a programmer I would never --not ever-- allow a user to modify a variable directly. Users don't have direct access to variables in C... so what's the advantage here? Seems to me all you're doing is hiding it from *yourself*...

    The idea of not having to understand the guts of an engine to drive a car has been handed to me before... However; if I'm writing the classes and the program they're in I do understand what's under the hood so once again it just seems like I'm really just hiding stuff from myself.
    Having had this wordplay with you before, I know exactly where you are coming from

    The word "user" in this context means, roughly, "programmer who is writing client code that uses the interface of your library". This could be a team mate, some programmer who bought your library, or even yourself. It certainly does not refer to an end user who is actually using the program (unless the end user is also a programmer with access to the code).

    That said, I note that in C++, private access does not actually hide the private members. Rather, it restricts access to the private members. If you really want to hide the private members, you should use the pointer to implementation (pimpl) idiom, which is another name for the opaque pointer technique that you may be familiar with in C. Thus, private access is a trade-off: the user can still view a part of the implementation (and may need to re-compile his/her client code if you change that part) in exchange for potentially more efficient code because the compiler has more information to work with.

    Quote Originally Posted by CommonTater
    Even in a large programming pool where multiple groups of multiple people each are working on a common project I still don't see the value in hiding stuff....
    If you have ever used the opaque pointer technique in C you would have done so in recognition of the value of hiding the implementation: it means that your implementation can more easily be changed without breaking client code. This same idea applies to access control in C++.
    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

  14. #44
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by blackcoder41 View Post
    You can imagine class a master plan or blue print of a car. It's just on paper, and it describes how the car should be created. The same model of cars can be created from a master plan but with different colors right? Just as a person have eyes, but your eyes might not be the same color as your cousins eye.

    Example
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Person
    {
        string  m_name;
        int     m_strength;
        int     m_health;
    
        public:
            Person(string name) : m_name(name), m_strength(10), m_health(100)
            {
                cout << m_name << " is born." << endl;
            }
    
            void Exercise()
            {
                m_strength++;
                m_health++;
            }
    
            void SmokeWeed()
            {
                m_strength--;
                m_health-=5;
            }
    
            void Status()
            {
                cout << "\nName:\t\t" << m_name << "\nStrength:\t" << m_strength
                     << "\nHealth:\t\t" << m_health << "\n\n";
            }
    
            virtual ~Person()
            {
                cout << m_name << " just died." << endl;
            }
    };
    
    int main(int argc, char* args[])
    {
        Person bill("Bill Gates");
        bill.Status();
        bill.Exercise();
        bill.Status();
        bill.SmokeWeed();
        bill.Status();
        return 0;
    }
    Here the person have a name "Bill Gates", and you can create another person with different name right? I just think that the member functions or methods are what the object can do, like walking, sleeping, etc. And the member variables are the object's characteristics or properties.

    If you initiated two persons here let's say bill and paul., it wouldn't affect paul if bill smoke weed but it affects bill's health.

    Hope that helps
    See... now that I understood... m_name, m_strength, m_health are private variables hidden from view... The methods are functions that act upon the hidden variables... this I get!

    But back to my last question.... If we had two copies of Bill in separate contexts would a call to Bill.Exercise in one context affect the variables in the other context or are they completely separate? (I don't have a running compiler at the moment so I guess I'll have to experiment with this later.)

  15. #45
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    If we had two copies of Bill in separate contexts would a call to Bill.Exercise in one context affect the variables in the other context or are they completely separate?
    Since Bill::Exercise only accesses/modifies non-static member variables, they are completely separate, just like members in C structs.
    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. Fmod question
    By spadez in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 05:26 PM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Am I too late to learn programming?
    By maccat in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 02-17-2009, 08:49 AM
  4. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM