classes and oop confusion

This is a discussion on classes and oop confusion within the C++ Programming forums, part of the General Programming Boards category; hey i just got back into c++ after some slacking off and sheer lazyness and have once again run into ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    classes and oop confusion

    hey i just got back into c++ after some slacking off and sheer lazyness and have once again run into the same problem i had before and it was never clarified in my high school class(crap teacher who's idea of teaching was "hey harry i did this correctly right?" harry taught the class basically and was lazy about it so we got nowhere fast anyways) but yea and well no matter what i read i can never seem to understand classes and oop to well so any assistance on starting it off on the right foot would be appreciated i even read the tutorial here and like 3 or 4 different book explnanations but its like there is something i keep missing and im not sure what i kind of have the syntax down but not the logic so can anyone help? thanx
    hooch

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The concepts are actually pretty simple:

    Classes - Classes are used to group together data and behavior. For example, the builtin int type knows how the data is stored for its variables, but also knows what operations can be performed on that data. Classes give us programmers the ability to write our own data types like that more easily. A class is like the int type and an object is like a variable of int.

    Encapsulation - The concept of grouping together data and behavior that classes help to implement.

    Inheritance - The ability of one class to extend another by being given data and behavior defined in another class. The new (child) class can also define its own data and behavior as well. For example, I inherited blond hair from my mother (data), but my foul temper is my own (behavior).

    Polymorphism - Also called dynamic-binding, polymorphism is the ability of an object of a child class to act as if it were an object of its parent class. For example, if I had a class hierarchy:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class base {
    public:
      virtual void whoami() { cout<<"I am base"<<endl; }
    };
    
    class derived: public base {
    public:
      void whoami() { cout<<"I am derived"<<endl; }
    };
    
    int main()
    {
      base    b;
      derived d;
    
      b.whoami();
      d.whoami();
    
      base *rb = &b;
      base *rd = &d;
    
      rb->whoami();
      rd->whoami();
    }
    Despite being a pointer to base, rd still prints the correct string. Dynamic-binding chose the proper class type. Without dynamic-binding (through the use of virtual functions), this wouldn't have worked:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class base {
    public:
      void whoami() { cout<<"I am base"<<endl; }
    };
    
    class derived: public base {
    public:
      void whoami() { cout<<"I am derived"<<endl; }
    };
    
    int main()
    {
      base    b;
      derived d;
    
      b.whoami();
      d.whoami();
    
      base *rb = &b;
      base *rd = &d;
    
      rb->whoami();
      rd->whoami();
    }
    That's basically all there is to it. Actually using object-orientation is more complex, but the underlying principles are relatively simple.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    so in so many words let me see if i figured this out cant promise i did but im going to try:
    classes allow programmers to create their own types such as int?
    but my question is all the variable types of c++ seem to be able to be easily manipulated to allow for most problems to be solved you have numbers of all sorts and strings of all sorts which everything is a string and number right? so i think that is where i am confused what purpose does creating your own data type serve to me it makes sense just use what is already there or am i wrong and in day to day on the job programming people use classes constantly?

    pointers you confused me there that was my other area i got confused in pointers that and some arrays but im trying to stick to the classes for the moment as i kinda understand those to some degree so unless it serves purpose to this ill deal with it later so as not to confuse myself.

    now i have read classes allow to you make your own header files i read this ina book and it explained how so does that serve any real purpose cause i dont quite understand its purpose?

    yes i kno im rather confused right now but hopefully that will chance soon as i really enjoy programming alright thanx
    hooch

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >it makes sense just use what is already
    Yes, that's a good habit to get into. However, consider a balanced binary tree data structure. This is not a trivial data structure and many programmers just aren't up to the challenge of writing one of their own. While you can use the tools given to you by C++ to create one, it makes more sense to take all of that complexity and hide it in a data type. This makes using it much easier. Back to the int analogy, would you really want to hand code the underlying implementation of an int each time you want to use it? Of course not, that would make your job much harder and your code would be bound to the machine you wrote it on.

    What classes do (ignoring inheritance for the moment) is hide complex logic so that:

    1) The client of the class (you or I) doesn't have to understand or worry about the implementation. We just use the interface, which should be simple.

    2) The implementation can be changed without affecting the interface such that if a class bound to a certain machine is ported to another, any client code will work properly without modification.

    >in day to day on the job programming people use classes constantly?
    Yes, we do. Many useful classes are written to make us more productive. A good example is a socket class. Writing networking code using sockets is tedious, so if all of the tediousness is hidden in a class so that I can simply say:
    Code:
    Socket another_computer ( address );
    As opposed to about 80 or so lines of creating, opening, connecting and error checking manual socket code, this makes my job considerably easier and the code more transparent. Even if you don't know how to write low level socket code, you can still figure out what it going on when it is hidden in a class.

    >now i have read classes allow to you make your own header files
    You can make your own headers without classes, perhaps you misunderstood what was being said in the book.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    i gather from this one handy feature of this is classes allow portability of programs from system to system?

    so am i far off or closer to understand if i say a class can kinda be a function in the sense it creates code that can be referred to constantly without the ever so annoying and painful rewriting 1000x over? cept this time a class is a little more powerful? so my question now is when a class creates a data type i notice its still bound to in the creation the standard c++ data types jus they seem modified now i think i understand its purpose now but in the creation of the data types how do those new data types differ from current c++ ones cause to me they just seem c++ all over again just certain strings attached and they they perform a job and dont need to be rewritten over over? if that was confusing sorry heres an example:

    ina game you might be making have a class for various moves you can do and how they might can change a very specific points in the game but they require the basic punch line of what was made in the class? like punch,kick, etc...? am i getting closer? sorry when i think this out i ramble alot im trying to simplify my thoughts alright but let me know if i got closer to this alright thanx
    hooch

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >so am i far off or closer to understand if i say a class can kinda be a function
    Somewhat. Classes are similar to functions in that they offer a service to client code. They are more powerful than functions because they can maintain a state as well as perform a service. The only way to do this with functions is by using static variables, which is usually a bad idea on multi-threaded systems. Your analogy is sound, but the concept of a class is closer to a structure that allows member functions as well as member variables: A record containing data and the operations that can be performed on that data.

    >how do those new data types differ from current c++ ones
    They do more with less code. Take the C++ string class; to create a dynamic C-style string and initialize it, you would be forced to do something like this:
    Code:
    #include <iostream>
    #include <cstring>
    #include <new>
    
    using namespace std;
    
    int main()
    {
      char *s;
      int  n;
    
      try {
        n = 15;
        s = new char[n];
        strcpy ( s, "This is a test" );
      }
      catch ( bad_alloc& ex ) {
        cerr<< ex.what();
      }
      cout<< s <<endl;
      delete [] s;
    }
    That is the manual way of doing it. But the C++ string class does more, with less code:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      string s ( "This is a test" );
    
      cout<< s <<endl;
    }
    Not only do you now have a dynamic string, you also have a whole slew of operations to perform on that string contained inside of s with only a single line of code. The same cannot be said of the manual method.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    i see the great deal of difference in coding and the class i presume then just allows you to pull that "s" back up anytime you please and that s im guessing came from a previously created class so to summarize before i go off and attempt this on my own and start playing with it:

    classes allow for creating portable code
    classes allow for simplifying things because they create a permanent function to use rather than rewriting things 1000x times over
    classes allow for creating own data types

    but im guessing one of the MAIN reasons is the portability feature so a string of a certain value will be a string of a certain value no matter what system it may be played on which this is done by creating our own data types which im still gonna have to practice with pretty badly but unless i get my mom to let me install my compiler on here and live to tell the tail after the arguement it could be a while so hopefully ill remeber but thanx for your time and i guess this the basics behind it?
    hooch

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i guess this the basics behind it?
    It looks like you've got it.

    >thanx for your time
    I'm happy to help.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP flu
    By Hussain Hani in forum General Discussions
    Replies: 15
    Last Post: 06-27-2009, 02:02 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Data Mapping and Moving Relationships
    By Mario F. in forum Tech Board
    Replies: 7
    Last Post: 12-14-2006, 09:32 AM
  4. OOP and Classes Help
    By -Dan- in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2004, 12:24 AM
  5. Questions on Classes
    By Weng in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2003, 05:49 AM

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