Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 2nd, 2006, 7:08 PM   #1
b1g4L
Programmer
 
Join Date: Dec 2005
Location: SC
Posts: 38
Rep Power: 0 b1g4L is on a distinguished road
Send a message via AIM to b1g4L
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;
    }
};
b1g4L is offline   Reply With Quote
Old Feb 2nd, 2006, 7:22 PM   #2
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3 Seif is on a distinguished road
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 ^^.
Seif is offline   Reply With Quote
Old Feb 2nd, 2006, 7:23 PM   #3
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3 Seif is on a distinguished road
actually i think i misread the question lol ignore me sorry. been a long day .
Seif is offline   Reply With Quote
Old Feb 2nd, 2006, 7:49 PM   #4
b1g4L
Programmer
 
Join Date: Dec 2005
Location: SC
Posts: 38
Rep Power: 0 b1g4L is on a distinguished road
Send a message via AIM to b1g4L
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.
b1g4L is offline   Reply With Quote
Old Feb 2nd, 2006, 8:25 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 2nd, 2006, 10:01 PM   #6
b1g4L
Programmer
 
Join Date: Dec 2005
Location: SC
Posts: 38
Rep Power: 0 b1g4L is on a distinguished road
Send a message via AIM to b1g4L
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.
b1g4L is offline   Reply With Quote
Old Feb 2nd, 2006, 10:17 PM   #7
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4 Jessehk is on a distinguished road
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
};

#endif

student.cpp
#include "student.h"

//define constructor
Student::Student() : studentName(""), studentIdNumber(0), studentGrade(0)
{
}

//define the other methods

I 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!
Jessehk is offline   Reply With Quote
Old Feb 2nd, 2006, 10:30 PM   #8
b1g4L
Programmer
 
Join Date: Dec 2005
Location: SC
Posts: 38
Rep Power: 0 b1g4L is on a distinguished road
Send a message via AIM to b1g4L
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....
b1g4L is offline   Reply With Quote
Old Feb 2nd, 2006, 10:44 PM   #9
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5 grumpy is on a distinguished road
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
};

#endif


Then 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;
}
To build a test program, you will need to compile all of the relevant source files, and link the objects into a working executable. The way you do this depends on your development environment (what compiler, what IDE if any, etc etc).

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.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:34 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC