Help with Classes and OOP

Hi,myname is Martin and this is my first post here.I'm not the kind of person that asks for help but i am really stuck since 1 week on this. I'm fallowing the book (for my own knownledge) "c++, How To Program, early object oriented programming). In chapter 2, i was writing easy **** code, that didn't required thinkin, but in chapter 3, this seems to be real programming. I just can't figure out how to write what they ask. They give me this fallowing code and they want me to include a second string data member and then provide a get and set functions to it. I've seemed to try everything possible to achieve it, but nothing works. (fyi im using Devc++ as a compiler). I don'T want you to do it for me, i just want some tips on how to declare this new string data member so it can actualy works.I understand the concept but the way to write it doesn't work for me. I'll show you the code.

---------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Fig. 3.11: GradeBook.h
// GradeBook class definition. This file presents GradeBook's public 
// interface without revealing the implementations of GradeBook's member
// functions, which are defined in GradeBook.cpp.
#include <string> // class GradeBook uses C++ standard string class
using namespace std;

// GradeBook class definition
class GradeBook
{
public:
   GradeBook( string ); // constructor that initializes courseName
   void setCourseName( string ); // function that sets the course name
   string getCourseName(); // function that gets the course name
   void displayMessage(); // function that displays a welcome message   
   
private:
   string courseName; // course name for this GradeBook
}; // end class GradeBook   

---------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Fig. 3.13: fig03_13.cpp
// GradeBook class demonstration after separating 
// its interface from its implementation.
#include <iostream>
#include "GradeBook.h" // include definition of class GradeBook
using namespace std;

// function main begins program execution
int main()
{
   // create two GradeBook objects
   GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );
   GradeBook gradeBook2( "CS102 Data Structures in C++" );


   // display initial value of courseName for each GradeBook
   cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
      << "ngradeBook2 created for course: " << gradeBook2.getCourseName() 
      << endl;
} // end main

-------------------------------------------------------------------


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
// Fig. 3.12: GradeBook.cpp
// GradeBook member-function definitions. This file contains
// implementations of the member functions prototyped in GradeBook.h.
#include <iostream>
#include "GradeBook.h" // include definition of class GradeBook
using namespace std;

// constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string name )
{
   setCourseName( name ); // call set function to initialize courseName
} // end GradeBook constructor

// function to set the course name
void GradeBook::setCourseName( string name )
{
   courseName = name; // store the course name in the object
} // end function setCourseName

// function to get the course name
string GradeBook::getCourseName()
{
   return courseName; // return object's courseName
} // end function getCourseName

// display a welcome message to the GradeBook user
void GradeBook::displayMessage()
{
   // call getCourseName to get the courseName
   cout << "Welcome to the grade book forn" << getCourseName()
      << "!" << endl;
} // end function displayMessage 
Last edited on
fyi im using Devc++ as a compiler


DevC++ is an IDE, not a compiler. And it's an evil IDE (I seriously can't count how many people I have to tell this per week... DevC++ is outdated. And not just a bit, but really dusty. Also, it's buggy as hell and doesn't have a fully featured debugger).

Oh and no, this isn't anything that requires thinking yet. In this case, getter/setter methods are one liners. No brain required.

I suppose set/getTeacherName are the methods you are required to write? Basically, you just have to copy&paste the existing methods and change some of the names, you don't really have to do anything yourself yet.

Though trying to get into OO before you actually understood how to write a function is probably not a good idea.
Devc++ is the only IDE i can install at work, so im pretty much stuck with it.
We'll i've tried the copying method, but it didn't work. At a point, i though i would have to create a new gradebook::gradebook constructor (with an other name after the binary scope operator) or just keep it like that and declare the TeacherName into this body?. So that's pretty much where i'm stuck

1
2
3
4
GradeBook::GradeBook( string name )
{
   setCourseName( name ); // call set function to initialize courseName
} // end GradeBook constructor 



Oh yeah and also. The setget function in the Headers. Do i have to put completly new lines with the void setTeacherName()
etc..?
Last edited on
To add to what hanst99 said:

http://cplusplus.com/forum/articles/36896/

And can you replace the output tags with code tags please?
You don't really have to create a new constructor, though it would probably be a good idea to do so.


Do i have to put completly new lines with the void setTeacherName()
etc..?


I don't really get what you're trying to say here, but if you mean to ask whether you have to declare your new functions in the header file: Yes.
Fuck yeah, Code::Block works, i can install it here.

Awesome a new ide to work with. Thanks a lot, ill do tries with the new informations i got here and the new IDE
<3<3<3

can'T believe how fast the responses are, ive made that post 15 minutes ago :P
I am rather doing the work of other people cause I can avoid working on my own that way :O
I will probably reply monday if everything worked fine that way and if i could get to the next # in my book. Thanks!

Go back to your code it may gets you rich one day :P
yes it worked!!!!!!!!!!!!! i'm so happy

I'll link to you what i've done, it was pretty simple actualy


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include "GradeBook.h" // include definition of class GradeBook
using namespace std;

// function main begins program execution
int main()
{
   // create two GradeBook objects
   GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );
   GradeBook gradeBook2( "CS102 Data Structures in C++" );
   CourseTeacher courseTeacher1( "\nName Teacher for this course is : Rogers" );
   CourseTeacher courseTeacher2( "\nName Teacher for this course is : Marie-Georges" );

   // display initial value of courseName for each GradeBook
   cout << "gradeBook1 created for course: " << gradeBook1.getCourseName() << courseTeacher1.getTeacherName()
      << "\ngradeBook2 created for course: " << gradeBook2.getCourseName() << courseTeacher2.getTeacherName()
      << endl;
} // end main 



----------------------------

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
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include "GradeBook.h" // include definition of class GradeBook
using namespace std;

// constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string name )
{
   setCourseName( name ); // call set function to initialize courseName
} // end GradeBook constructor

// function to set the course name
void GradeBook::setCourseName( string name )
{
   courseName = name; // store the course name in the object
} // end function setCourseName

// function to get the course name
string GradeBook::getCourseName()
{
   return courseName; // return object's courseName
} // end function getCourseName

// display a welcome message to the GradeBook user
void GradeBook::displayMessage()
{
   // call getCourseName to get the courseName
   cout << "Welcome to the grade book for\n" << getCourseName()
      << "!" << endl;
} // end function displayMessage



CourseTeacher::CourseTeacher( string name )
{
   setTeacherName( name ); // call set function to initialize courseName
} // end GradeBook constructor

// function to set the course name
void CourseTeacher::setTeacherName( string name )
{
   teacherName = name; // store the course name in the object
} // end function setCourseName

// function to get the course name
string CourseTeacher::getTeacherName()
{
   return teacherName; // return object's courseName
} // end function getCourseName 



------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string> // class GradeBook uses C++ standard string class
using namespace std;

// GradeBook class definition
class GradeBook
{
public:
   GradeBook( string ); // constructor that initializes courseName
   void setCourseName( string ); // function that sets the course name
   string getCourseName(); // function that gets the course name
   void displayMessage(); // function that displays a welcome message
private:
   string courseName; // course name for this GradeBook
}; // end class GradeBook

class CourseTeacher
{
public:
   CourseTeacher( string ); // constructor that initializes courseName
   void setTeacherName( string ); // function that sets the course name
   string getTeacherName(); // function that gets the course name
private:
   string teacherName;
}; // end class GradeBook 
Topic archived. No new replies allowed.