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.
// 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
usingnamespace 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
// 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
usingnamespace 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
// 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
usingnamespace 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
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..?
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
usingnamespace 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
#include <iostream>
#include "GradeBook.h" // include definition of class GradeBook
usingnamespace 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
#include <string> // class GradeBook uses C++ standard string class
usingnamespace 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