View Single Post
Old Oct 1st, 2004, 7:18 PM   #12
Ravilj
Programmer
 
Ravilj's Avatar
 
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 5 Ravilj is on a distinguished road
There are two ways of building a student record:
1. using a structure like you have done, this is similar to the pascal record.
2. creating a class called student.

The later will allow you to make use of functions that are specific to student. This is generally the accepted way but is quite diificult to implement.

You generally implement a class as follows for a student record
This would be defined in a header file say Student.h
class Student {
public:
Student(); * *// default constructor
Student( string name , int grade ); // constructor

string getName( void );
int getGrade( void );

void setName( string name );
void setGrade( int grade );

private:
string _name;
int _grade;
};

1st of anything listed under public can be accessed outside the class ie: by the main program directly as long as the obejct is in scope or exists.
Anything under private can only be accessed directly inside the class or using interface functions (getName, getGrade, setGrade, setName) which you define.

Obviously you will have to declare the functions. You generally add the class definition above in a header file and than the declarations in a cpp file of the same name as the header file so:
#include "student.h"

// default constructor declaration
Student::Student()
{
_name = "";
_grade = 0;
}

Student::Student( string name , int grade)
{
_name=name;
_grade=grade;
}

string Student::getName( void )
{
return _name;
}

//...
//...
And such on giving the rest of the declarations. You use the name of the class and than the scope operator to tell the compiler that these functions are defined in the Student class. As you can see these functions can access the private data members _name and _grade since your working "inside" the class (for lack of a better description).

Now you ask what good does this do for me, well now you can make an object called student.
Student pupil1; // uses the default constructor
Student pupil2( "John Smith" , 1 ); // uses other constructor

// to access member functions of the class:
pupil1.setName("Ryan");
cout << pupil2.getName() << endl << pupil2.getGrade() << endl;
and so on... Oops forgot to mention what constructors do :/ When you define an object (pupil1 and pupil2) of the class Student, the compiler calls the function 'Student' that we defined under public in the class. We call this special function a constructor ie: it constructs the object. As you can see we have 2 of them the first takes no parameters the second does and you can see this implemented with pupil1 using the default constructor with no parameters and pupil2 with parameters.

Anyway I think you feel like you just fell down the rabbits hole to quote The Matrix (how appropriate for a programming forum). Class design is a difficult thing yet it forms the basis for object oriented programming. This is just the tip of the ice berg.

Feel free to ask any more questions Hope that helped.
__________________
Ravilj's OpenGL Terrain aka WinTerrain Last Updated: 17/01/2005!
Ravilj is offline   Reply With Quote