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;
}