two parameter in a constructor

This is a discussion on two parameter in a constructor within the C++ Programming forums, part of the General Programming Boards category; what is the syntax for set and get when you want to initialize 2 private string in a constructor?...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    91

    two parameter in a constructor

    what is the syntax for set and get when you want to initialize 2 private string in a constructor?

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    What do you mean ?

    Do you mean how to write a constructor which receives two arguments ?

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    yeah!!!at last!!!

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Nice, thanks for that.Another one what is the syntax for two set and get functions?

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by freddyvorhees View Post
    Nice, thanks for that.Another one what is the syntax for two set and get functions?
    The same as any other function. Although Set functions usually take a parameter and return nothing, while a Get function returns something and takes no parameters.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    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;
     }

  8. #8
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    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)

  9. #9
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    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)

  10. #10
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    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...
    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;
    }
    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" ).
    Last edited by dudeomanodude; 07-25-2008 at 07:13 AM.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  11. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    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 GradeBook
    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
    And can you include comments like the other one.

  12. #12
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Do your own homework.

    Don't do his homework for him.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  13. #13
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    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.

  14. #14
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    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)

  15. #15
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    So did my original code followed those instructions?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Logical parameter in parent constructor
    By g4j31a5 in forum C++ Programming
    Replies: 4
    Last Post: 11-29-2006, 03:20 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Constructor with Parameter not Firing
    By BillBoeBaggins in forum Windows Programming
    Replies: 4
    Last Post: 08-26-2004, 02:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21