Classes help!

This is a discussion on Classes help! within the C++ Programming forums, part of the General Programming Boards category; I searched the forums and came up with nothing (I was amazed at that fact). The tutorial didn't help me ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    21

    Question Classes help!

    I searched the forums and came up with nothing (I was amazed at that fact). The tutorial didn't help me understand classes. Their example kept referring to processorspeed and it never changed anything. First they said it was equal to 0, then equal to p.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Computer // Standard way of defining the class
    {
    public:
      // This means that all of the functions below this(and any variables)
      //  are accessible to the rest of the program.
      //  NOTE: That is a colon, NOT a semicolon...
      Computer();
      // Constructor
      ~Computer();
      // Destructor
      void setspeed ( int p );
      int readspeed();
    protected:
      // This means that all the variables under this, until a new type of
      //  restriction is placed, will only be accessible to other functions in the
      //  class.  NOTE: That is a colon, NOT a semicolon...
      int processorspeed;
    };
    // Do Not forget the trailing semi-colon
    
    Computer::Computer()
    {
      //Constructors can accept arguments, but this one does not
      processorspeed = 0;
    }
    
    Computer::~Computer()
    {
      //Destructors do not accept arguments
    }
    
    void Computer::setspeed ( int p )
    {
      // To define a function outside put the name of the class
      //  after the return type and then two colons, and then the name
      //  of the function.
      processorspeed = p;
    }
    int Computer::readspeed()  
    {
      // The two colons simply tell the compiler that the function is part
      //  of the class
      return processorspeed;
    }
    
    int main()
    {
      Computer compute;  
      // To create an 'instance' of the class, simply treat it like you would
      //  a structure.  (An instance is simply when you create an actual object
      //  from the class, as opposed to having the definition of the class)
      compute.setspeed ( 100 ); 
      // To call functions in the class, you put the name of the instance,
      //  a period, and then the function name.
      cout<< compute.readspeed();
      // See above note.
    }
    setspeed is equal to readspeed? This doesn't make sense! Can someone please explain this and maybe give a simple, more common example. Thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    setspeed is equal to readspeed?
    setspeed and readspeed are member functions, not values. Since they do different things, they are not "equal".

    However, processorspeed is a member variable. setspeed is used to set its value, readspeed is used to return its value. So, if you use setspeed to set processorspeed to 100, when you use readspeed to get its value, you will get 100 (assuming everything else remains unchanged).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    setspeed() assigns a value to the processorspeed member variable, then readspeed() returns the value of processorspeed. I don't know how much more clear you can make it than that?

    BTW, some notes on programming style I noticed:
    - I've almost always seen code comments appear above the line that it's talking about, not below it. So your comments might appear a bit weird to most people.
    - These days most people use mixed case for function & variable names. They usually begin each word with a capital letter. (ex. SetSpeed(), ReadSpeed()...) It just makes it easier to read.
    - It's a good idea to prefix your class member variables with something to make them stand out from regular function variable names. Many people like to use a "m_" prefix to mean "member variable" (ex. m_ProcessorSpeed). This way, when you're reading a large piece of code, and you see a variable that starts with m_, you immediately know it's defined in the class, not in the function, and that changing its value will affect the whole object's state.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    21
    Thanks for the help, I'm starting to get it. And by the way I just coppied the code from the tutorial. \/

    BTW, some notes on programming style I noticed:
    - I've almost always seen code comments appear above the line that it's talking about, not below it. So your comments might appear a bit weird to most people.
    - These days most people use mixed case for function & variable names. They usually begin each word with a capital letter. (ex. SetSpeed(), ReadSpeed()...) It just makes it easier to read.
    - It's a good idea to prefix your class member variables with something to make them stand out from regular function variable names. Many people like to use a "m_" prefix to mean "member variable" (ex. m_ProcessorSpeed). This way, when you're reading a large piece of code, and you see a variable that starts with m_, you immediately know it's defined in the class, not in the function, and that changing its value will affect the whole object's state.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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