what is the syntax for set and get when you want to initialize 2 private string in a constructor?
what is the syntax for set and get when you want to initialize 2 private string in a constructor?
What do you mean ?
Do you mean how to write a constructor which receives two arguments ?
yeah!!!at last!!!
How do you write a constructor that receives one argument? Now, add one more argument to it. (They are separated by comma.)
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Nice, thanks for that.Another one what is the syntax for two set and get functions?
Ok heres my program, whats wrong with it?Its an exercies from C++ How to program 5e.I've just patterned it from the examples and I havent fully grasped the concept yet.So i dont fully understand what I did.
Code:#include <iostream> using std::cout; using std::endl; #include <string> using std::string; class GradeBook { public: GradeBook( string course, string instructor ) { setCourseName( course ); setInstructorsName( instructor ); } void setCourseName( string course ) { courseName = course; } string getCourseName() { return courseName; } void displaycourse() { cout <<getCourseName(); } void setInstructorsName ( string instructor ) { instructorsName = instructor; } string getInstructorsName() { return instructorsName; } void displayinstructor() { cout <<getInstructorsName(); } private: string courseName; string instructorsName; }; int main() { GradeBook namecourse( "# ICST101 Computer Programming I " ); GradeBook nameinstructors( "Dr. Allan Sioson " ); cout <<"Welcome to the grade book for: " << namecourse.getCourseName(); cout <<"This course is presented by: "<<nameinstructors.getInstructorsName(); return 0; }
For starters, your constructor is defined to take two arguments, yet you only give it one in main().
Second, why bother calling the set functions in your constructor? You should really use an "initialization list". It would look like so:
Code:class gradebook{ public: Gradebook( string one, string two ) : frist( one ), second( two ) {} // etc... private: string first; string second; };
Last edited by dudeomanodude; 07-25-2008 at 06:54 AM.
Ubuntu Desktop
GCC/G++
Geany (for quick projects)
Anjuta (for larger things)
A few more things:
1. It's a little redundant to have a getInstructorName() function and also the display() which simply pipes the getInstructorName() fucntion to cout.
It's only a style thing, but I'd rather do like this:
Code:class Gradebook{ public: // etc.... string getInstructorsName(){ return instructorsName; } // etc... }; // And display it in main() like this: int main(){ cout << object.getInstructorsName() << "\n"; // etc... }
Ubuntu Desktop
GCC/G++
Geany (for quick projects)
Anjuta (for larger things)
Since I'm really bored at my desk right now, I decided to clean things up for you a bit. Look itover and learn from it...
Note that by giving the "get" functions in your class allows users to write functions like displayMessage(). Now, that could've just as well been written into the class, I only wanted to illustrate how that works and also wanted you to see passing an object by reference. The point of pass-by-reference is to stop copying of an object as it's passed by simply giving it's memory address ( which is what the "&" does - it requests the refence to that object, in this case "sampleCourse" ).Code:#include <iostream> #include <string> using std::cout; using std::endl; using std::string; // Much smaller class now // class GradeBook { public: GradeBook( string course, string instructor ) : courseName( course ), instructorsName( instructor ) {} string getCourseName() { return courseName; } string getInstructorsName() { return instructorsName; } private: string courseName; string instructorsName; }; // Write display function outside of class: // void displayMessage( GradeBook& mBook ) // Note that argument is passed by reference { cout << "Welcome to the grade book for: " << mBook.getCourseName() << "\nThis course is presented by: " << mBook.getInstructorsName(); } int main() { // Constructor takes two arguments: // GradeBook sampleCourse( "# ICST101 - Computer Programming I", "Dr. Allan Sioson" ); displayMessage( sampleCourse ); // sampleCourse is passed by reference return 0; }
Last edited by dudeomanodude; 07-25-2008 at 07:13 AM.
Ubuntu Desktop
GCC/G++
Geany (for quick projects)
Anjuta (for larger things)
Thats very nice,it helped me a lot. But it has an intsructions
3.11 (Modifying Class GradeBook) Modify class GradeBook (Figs. 3.113.12) as follows:
1.Include a second string data member that represents the course instructor's name.
2.Provide a set function to change the instructor's name and a get function to retrieve it.
3.Modify the constructor to specify two parametersone for the course name and one for the instructor's name.
4.Modify member function displayMessage such that it first outputs the welcome message and course name, then outputs "This course is presented by: " followed by the instructor's name.
Code:1 // Fig. 3.11: GradeBook.h 2 // GradeBook class definition. This file presents GradeBook's public 3 // interface without revealing the implementations of GradeBook's member 4 // functions, which are defined in GradeBook.cpp. 5 #include <string> // class GradeBook uses C++ standard string class 6 using std::string; 7 8 // GradeBook class definition 9 class GradeBook 10 { 11 public: 12 GradeBook( string ); // constructor that initializes courseName 13 void setCourseName( string ); // function that sets the course name 14 string getCourseName(); // function that gets the course name 15 void displayMessage(); // function that displays a welcome message 16 private: 17 string courseName; // course name for this GradeBook 18 }; // end class GradeBookAnd can you include comments like the other one.Code:1 // Fig. 3.12: GradeBook.cpp 2 // GradeBook member-function definitions. This file contains 3 // implementations of the member functions prototyped in GradeBook.h. 4 #include <iostream> 5 using std::cout; 6 using std::endl; 7 8 #include "GradeBook.h" // include definition of class GradeBook 9 10 // constructor initializes courseName with string supplied as argument 11 GradeBook::GradeBook( string name ) 12 { 13 setCourseName( name ); // call set function to initialize courseName 14 } // end GradeBook constructor 15 16 // function to set the course name 17 void GradeBook::setCourseName( string name ) 18 { 19 courseName = name; // store the course name in the object 20 } // end function setCourseName 21 22 // function to get the course name 23 string GradeBook::getCourseName() 24 { 25 return courseName; // return object's courseName 26 } // end function getCourseName 27 28 // display a welcome message to the GradeBook user 29 void GradeBook::displayMessage() 30 { 31 // call getCourseName to get the courseName 32 cout << "Welcome to the grade book for\n" << getCourseName() 33 << "!" << endl; 34 } // end function displayMessage
Yes...i know.Im not asking for the answer just the correct syntax.I just showed the instructions because i cant explain it my self. Because like I said I havent fully grasped the concept yet. but I would appreciate it if you can explain it to me, maybe use a analogy - thats what my instructor would do but im in awol. So I guess you could be a substitute? please
Last edited by freddyvorhees; 07-25-2008 at 10:00 PM.
1.Include a second string data member that represents the course instructor's name.
-It's already there.
2.Provide a set function to change the instructor's name and a get function to retrieve it.
-You had that originally.
3.Modify the constructor to specify two parametersone for the course name and one for the instructor's name.
-It's already there. And I would advise against how you had it originally calling the set functions in the constructor.
4.Modify member function displayMessage such that it first outputs the welcome message and course name, then outputs "This course is presented by: " followed by the instructor's name.
-You should be able to negotiate that with what's there already.
Ubuntu Desktop
GCC/G++
Geany (for quick projects)
Anjuta (for larger things)
So did my original code followed those instructions?