I agree with declan, and if you are a beginner, it is a good idea to just quickly look at struct in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <iostream>
struct Employee
{
int nID;
int nAge;
float fWage;
};
void PrintInformation(Employee sEmployee)
{
using namespace std;
cout << "ID: " << sEmployee.nID << endl;
cout << "Age: " << sEmployee.nAge << endl;
cout << "Wage: " << sEmployee.fWage << endl << endl;
}
int main()
{
Employee sJoe; // create an Employee struct for Joe
sJoe.nID = 14;
sJoe.nAge = 32;
sJoe.fWage = 24.15;
Employee sFrank; // create an Employee struct for Frank
sFrank.nID = 15;
sFrank.nAge = 28;
sFrank.fWage = 18.27;
// Print Joe's information
PrintInformation(sJoe);
// Print Frank's information
PrintInformation(sFrank);
return 0;
}
|
ID: 14
Age: 32
Wage: 24.15
ID: 15
Age: 28
Wage: 18.27
|
Note that every time we process the information via the function, we process a similar thing, the content may be different, however.
Not the greatest way to explain a class, but this is how I first understand classes.
To really make the point simpler:
Classes treat things as objects. The reason people use OO over functions or older styles in a larger program is for portability. It is a lot easier, quicker, and more securer to pull out the information from a class, than a function.
For a small program you might not really need it.
It really depends on the programmer when to use it.
Last but not the least, the programming itself also shapes a bit about how one uses OO techniques. In python almost everything that I do deals is OO (explicitly).