Initializing Objects with constructors

This is a discussion on Initializing Objects with constructors within the C++ Programming forums, part of the General Programming Boards category; Code: Im very new to C++ and so im stuck. Im trying to do a problem and Im really curious ...

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

    Initializing Objects with constructors

    Code:
    Im very new to C++ and so im stuck. Im trying to do a problem and Im really curious how to solve it 
    
    3.11 (Modifying Class GradeBook) Modify class GradeBook (Figs. 3.113.12) as follows:
    
    Include a second string data member that represents the course instructor's name.
    
    Provide a set function to change the instructor's name and a get function to retrieve it.
    
    Modify the constructor to specify two parametersone for the course name and one for the instructor's name.
    
    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.
    
    3.11
    
     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
    
    
    3.12
    
     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  }
    
    If you can show me how to do this I would really appreciate it, an can you please include an explanation thats what i needed the most,and can you make it light on the terms(lol).
    
    Thanks!!!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. Use CODE tags only around your code.
    2. Can you, instead of just posting your assignment, please provide us with a distinct question as to what part you are stuck on?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Objects, Constructors, Pointers, References, Values ???
    By BlackSlash12 in forum C++ Programming
    Replies: 24
    Last Post: 12-14-2007, 05:26 PM
  2. Global objects and exceptions
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2006, 07:37 AM
  3. Replies: 60
    Last Post: 12-20-2005, 10:36 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. Constructors for Composed Objects
    By gozlan in forum C++ Programming
    Replies: 13
    Last Post: 10-18-2002, 03:37 PM

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