![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
system driver / header file
Here is a simple program that models a student. It can hold a student name, ID #, and grade. I believe I have the class setup correctly. I'm having a problem writing a system driver file & header file. Could someone explain to me how two construct each of these?
#include <iostream>
using namespace std;
class Student
{
private:
//global variables
string studentName;
int studentIdNum;
double studentGrade;
public:
Student () //default constructor
{
studentName = "";
studentIdNum = 0;
studentGrade = 0;
}
string getName () //method returns student name
{
return studentName;
}
int getIdNum () //method returns ID number
{
return studentIdNum;
}
double getGrade () //method returns grade
{
return studentGrade;
}
void setName (string name) //method sets student name
{
studentName = name;
}
void setIdNum (int idNum) //method sets ID number
{
studentIdNum = idNum;
}
void setGrade (double grade) //method sets grade
{
studentGrade = grade;
}
}; |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3
![]() |
hi. im not too shit hot on class's but in the public so may be wrong but to construct a student you need to put Student:
tudent() to construct it then say int Student::getIdNum() {method} for all the methods.however dont take my word for it as my subname would suggest, i am a noob ^^. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3
![]() |
actually i think i misread the question lol ignore me sorry. been a long day
. |
|
|
|
|
|
#4 |
|
Programmer
|
thanks anyway. I'm just looking how to write a driver file and header file for this Student class.....as separate files, but in the same directory. I appreciate any help.
|
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The term 'driver file' means nothing. Perhaps you mean a file containing the code to use the class. Typically, one would define the class in a header (.h) file and the using code in a source (.cpp) file. The header is included at the top of the source file. This causes it to be copied and pasted (invisibly to you) into the source file at the point of inclusion.
If you don't understand what I'm saying, post back and I'll post an example. Seif, if you don't know a correct answer, please refrain from giving one. Misleading answers are worse than no answer. With no answer the questioner will continue to seek. With a wrong answer the questioner feels as if he's got what he needs, and produces unworkable pieces of crap. See my lounge post, "Please Be Careful...".
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Programmer
|
I think I was thinking of a test driver. Maybe thats it. What I'm saying is that I don't know how to write a header file. I pulled this example of the Student class because it was easy and I figured someone could should me how to go about writing a header file for the Student class.
|
|
|
|
|
|
#7 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4
![]() |
A header file (usually *.h) contains (in most cases) declarations, not definitions.
In other words, the way to make the class you wrote into a header file would be to declare the class (maybe in a student.h file), then define the methods in a *.cpp file, and then finally write a program that utilizes the class. for example: (code has been mostly snipped) student.h
#ifndef STUDENT_H_
#define STUDENT_H_
//previous prevents the header file from being
//included more than once
#include <iostream>
class Student
{
private:
//etc
public:
//etc
};
#endifstudent.cpp
#include "student.h"
//define constructor
Student::Student() : studentName(""), studentIdNumber(0), studentGrade(0)
{
}
//define the other methodsI would also recommend that you don't use namespace std; in a header file.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#8 |
|
Programmer
|
thanks. Now, how would I create a student object from another .cpp file? I tried the following:
#include <iostream>
using namespace std;
int main ()
{
Student stud = new Student;
stud.setName("Bob");
return 0;
}I'm coming from a background in Java, if your were wondering.... |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5
![]() |
Put the class declaration into a header file (eg student.h). A couple of things to remember;
1) Never have a "using namespace" directive in a header file. Doing so can break code that #include's that header file, and can make you extremely unpopular if other people wish to reuse your code. 2) Unless you have a specific reason to inline your member functions, don't. Or, to put it another way, by default you should remove the bodies of all member functions from within your class declaration. 3) Unless you have a specific reason not to, employ an include guard on the header file. The working of an include guard is a macro with a name that is unique to the header file (no other header files or source files declare it or use it). An example header file might be; #ifndef STUDENT_H_INCLUDED // this is the include guard
#define STUDENT_H_INCLUDED
#include <string>
class Student
{
private:
//global variables // Your description of these as "global variables" is incorrect
std::string studentName;
int studentIdNum;
double studentGrade;
public:
Student (); //default constructor
std::string getName (); //method returns student name
int getIdNum (); //method returns ID number
double getGrade (); //method returns grade
void setName (std::string name); //method sets student name
void setIdNum (int idNum); //method sets ID number
void setGrade (double grade); //method sets grade
};
#endifThen structure a source file (eg student.cpp) along the following lines #include <system_headers_needed_for_student_class>
#include "student.h"
#include "other_headers_needed_to_make_student_class_work"
// instantiate any static members of the class. Not relevant in your example,
// as you have no static members.
// For example, if the Student class has a static member of type
// X named x that you want initialised using an integer value 0....
X Student::x(0);
// Provide definitions (the bodies) for all member functions of Student
// Remove bodies from your class declaration and implement them separately.
// For example;
Student::Student () : studentName(""), studentIdNum(0), studentGrade(0) //default constructor
{
// note that, for constructors, it is better to use initialiser lists rather
// than assigning members in the constructor body.
// Only use the constructor body for doing things other than initialising
// members of the class.
}If, by "driver file" you mean a little test program to test your student class, create a file named (say) test.cpp containing #include <system_headers_needed_to_make_test_work>
#include "student.h"
#include "any_other_headers_needed_to_make_test_work"
int main()
{
// code to test the Student class goes here
return 0;
}Unrelated to your question, consider using the const keyword. For example, your member getGrade() does not change any data members of a Student, so within the class declaration, would be better used as; double getGrade() const; Yes, it's obvious you've come from a Java background. Your programming style is showing all sorts of the worst coding practices that, unfortunately, Java tends to encourage. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|