One big thing you have to understand is that a function (ie main()) is not a class.
Classes hold variables and functions which can be accessed/used via the "dot" (.) operator.
Here is an example of an Event_Time class that prints out the current date and time when you call the function CurrentTime().
#include <iostream>
#include <ctime>
using namespace std;
class Event_Time
{
time_t current_seconds;
tm *timeinfo;
char buffer[80];
public:
void CurrentTime()
{
current_seconds = time(NULL);
timeinfo = localtime(¤t_seconds);
strftime( buffer, 80, "Event Date: %x", timeinfo );
cout << buffer << endl;
strftime( buffer, 80, "Event Time: %X %Z", timeinfo );
cout << buffer << endl;
}
};
int main()
{
Event_Time event; //creates a new event
event.CurrentTime(); //displays the current time
return 0;
}
Notice how there is no assignment done outside of functions within the class (if you want to declare and initialize at the same time outside of functions within the class then it must be a static constant variable (ie static const int x = 5 ).
If you want to initialize variables right when the object is made then you either call a function that assigns or you define a constructor for the class.
Here is an example using a constructor.
#include <iostream>
using namespace std;
class myClass
{
int myVariable;
public:
myClass() //Constructor. Notice that it does not have a return type. This will replace the default constructor.
{
myVariable = 5;
}
myClass( int in ) //This overloads the constructor which allows you to have multiple constructors (both are able to be used).
{
myVariable = in;
}
void ShowNumber()
{
cout << myVariable << endl;
}
};
int main()
{
myClass aClass; //myVariable gets assigned the value of 5
myClass anotherClass(10); //myVariable gets assigned the value of 10
aClass.ShowNumber(); //outputs 5
anotherClass.ShowNumber(); //outputs 10
return 0;
}
There is lots of information on classes. The website you are using for examples has a section on classes .