In all the practice exercises I do involving classes all I am doing is calling classes to call functions or variables...but it is a little more confusing than just calling functions outright. It is like I am calling a class just to call a function! Why is there a need for classes then? Are they used just for organization...or something else?
They make for better programming practices and readability. Think of a class representing a student. The class could hold the information for the student's name, address, current grades, etc. Along with storing information, you could create functions to call and change the information as needed. Since all the information would pertains to a student, all the functions regarding the student's information are placed in a class to keep them away from other parts of the program.
Probably not a good explanation, but I hope it helps clarify the point of classes a little :)
#include <iostream>//messing around w/classes basics
#include <string>
using std::cout;
using std::string;
using std::endl;
using std::cin;
class example
{
public:
int func(int x);
private:
int i;
};
example myExampleClass;
int main()
{
int b;
cout <<"Enter one for letters, two for numbers: ";
cin>>b;
myExampleClass.func(b);
return 0;
}
//end main
void func(int x)
{
switch(x)
{
case 1: cout<<"You got letters";
break;
case 2: cout<<"123456789";
break;
}
}