![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Programming Guru
![]() ![]() ![]() |
A student record eh? As in a structure?
struct student { string name; int grade; }; If not, need more specifics.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#12 |
|
Programmer
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 4
![]() |
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;
}
//...
//...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;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! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|