The code sample of classes in the tutorial. :o

This is a discussion on The code sample of classes in the tutorial. :o within the C++ Programming forums, part of the General Programming Boards category; Its been less than 24 hours and guess what I'm back to bug you again hehe Well, I was happily ...

  1. #1
    Dumb to the power of 999. Wall's Avatar
    Join Date
    Aug 2004
    Posts
    13

    The code sample of classes in the tutorial. :o

    Its been less than 24 hours and guess what I'm back to bug you again hehe

    Well, I was happily reading through the section on classes in this sites tutorial. I went to test out the sample code of classes and I got an error compiling it. It says there's a syntax error on line 1 before ::

    Here's the code.

    Code:
    void Aclass::aFunction()
    {
     cout<<"Whatever code";
    }
    #include <iostream.h>
    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();
    		//These functions will be defined outside of the class
    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; 
    		//Initializes it to zero
    }
    Computer::~Computer()
    {		//Destructors do not accept arguments
    } 
    //The destructor does not need to do anything.
    void Computer::setspeed(int p) 
    {       //To define a function outside put the name of the function 
    	//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 clas
      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.
      return 0;
    }
    Last edited by Wall; 08-20-2004 at 08:47 AM. Reason: Syntaz? :o
    My head hurts.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Delete everything from the file before the #include statement. That looked outta place from the get-go. There is no class Aclass. I think that's code that belongs in another example.

  3. #3
    Dumb to the power of 999. Wall's Avatar
    Join Date
    Aug 2004
    Posts
    13
    Oh thanks it worked.
    I found that odd-looking myself, but I just thought since it was there it was supposed to be there.
    My head hurts.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I've actually seen a couple of problems like this in the tuts the past couple of days.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Voip sample code
    By eryell in forum Networking/Device Communication
    Replies: 8
    Last Post: 10-01-2009, 11:10 PM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  3. is this C++ code sample?
    By chico1001 in forum C++ Programming
    Replies: 18
    Last Post: 03-03-2006, 03:05 PM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM

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