Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Multiple constructors ? (exercise from Deitel's C++ Book)

Posted on 2006-10-25
12
Medium Priority
?
652 Views
Last Modified: 2012-08-14

Hi,

I'm a novice working through the Deitel, C++ How to Program book.

I just finished reviewing the chapter on classes and I'm stuck on the following exercise:

 (Modifying Class GradeBook) Modify class GradeBook (Fig. 3.11–Fig. 3.12) as follows:
 a) Include a second string data member that represents the course instructor’s name.
 b) Provide a set function to change the instructor’s name and a get function to retrieve it.
 c) Modify the constructor to specify two parameters—one for the course name and one for the instructor’s name.
 d) 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.
 Use your modified class in a test program that demonstrates the class’s new capabilities.
-----------------------------------------------------------------------------------------------------------------

I had success in (a) (b) but (c) is not working as expected.  I think its a syntax error in the constructor, any how here is the code I have:

gradebook.h

#include <string>
using std::string;

// 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

      void setInstructorName( string );
      string getInstructorName();

private:
      string courseName; // course name for this gradebook
      string instructorName;
};


gradebook.cpp

#include <iostream>
using std::cout;
using std::endl;

#include "gradebook.h"

// constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string name )
{
      setCourseName( name );
      setInstructorName (name );

}

// function to set the course name
void GradeBook::setCourseName( string name )
{
      if (name.length() <= 5)
            courseName = name;

      if (name.length() > 5)
      {
            courseName = name.substr(0, 5);

            cout << "Name \ " << name << "\ exceeds maximum length (5).\n"
             << "Limiting courseName to first 5 characters.\n" << endl;
      } //end if statement
}

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

//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;
}

void GradeBook::setInstructorName( string name )
{
      instructorName = name;
}

string GradeBook::getInstructorName()
{
      return instructorName;
}

lesson03.cpp

#include <iostream>
using std::cout;
using std::endl;

#include "GradeBook.h"

int main()
{
      GradeBook gradebook1( "12345" );
      GradeBook gradebook2( "654321" "bob" );


      cout << "gradebook1 created for course: " << gradebook1.getCourseName()
            << "\ngradebook2 created for course: " << gradebook2.getCourseName()
            << " and the instructor is called " << gradebook2.getInstructorName()
            << endl;

      return 0;
}

Thanks for any guidance.
0
Comment
Question by:andyw27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
12 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 1000 total points
ID: 17807319
Hi andyw27,
>  GradeBook gradebook2( "654321" "bob" );

The above line should have a comma between the two arguments:
GradeBook gradebook2( "654321", "bob" );


David Maisonave (Axter)
Cheers!
0
 
LVL 30

Expert Comment

by:Axter
ID: 17807334
andyw27,
> GradeBook::GradeBook( string name )

The above constructor needs to be given two arguments.
GradeBook::GradeBook( string CourseName, string InstructorName ) //Always give a well descriptive name for your arguments.  Name really doesn't say much about an argument.


David Maisonave (Axter)
0
 
LVL 30

Expert Comment

by:Axter
ID: 17807344
andyw27,
> class GradeBook
> {
> public:
>      GradeBook( string );

Don't forget to also change your head (class declaration.
class GradeBook
{
public:
     GradeBook(  string CourseName, string InstructorName );

Argument names are not required in function declaration, but it's good practice to always give argument names, because it's easier for other developers to determine what type of argument they need to pass.    

David Maisonave (Axter)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 30

Expert Comment

by:Axter
ID: 17807358
andyw27,
> using std::string;

You should avoid putting using clause in a header.
Instead, use fully qualified names for all std objects in the header.

David Maisonave (Axter)
0
 

Author Comment

by:andyw27
ID: 17807363

David - thanks for response.

I changed as suggested, still getting an error.  What should the variables name by for the get and set for instructor, at the moment they are:

void GradeBook::setInstructorName( string iname )
{
      instructorName = iname;
}

string GradeBook::getInstructorName()
{
      return instructorName;
}

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

// constructor initializes courseName with string supplied as argument
GradeBook::GradeBook( string name, string iname )
{
      setCourseName( name );
      setInstructorName (iname );

}
0
 

Author Comment

by:andyw27
ID: 17807378

ah, you third post solved the problem I was getting.
0
 
LVL 30

Expert Comment

by:Axter
ID: 17807384
>>What should the variables name by for the get and set for instructor, at the moment they are:
If you made your member variables have an m_ prefix (as in hungarian notation), then you could do the following:

void GradeBook::setInstructorName( string instructorName)
{
     m_instructorName = instructorName;
}

>>I changed as suggested, still getting an error.
Please post the exact error you're getting.
0
 

Author Comment

by:andyw27
ID: 17807388

One other thing I noticed.

If I have a constructor that contains two parameters, in this case:
      setCourseName( name );
      setInstructorName (iname );

When I create an object do I have to always set both of these, for example I tried the following:

      cout << "gradebook1 created for course: " << gradebook1.getCourseName()
            << "\ngradebook2 created for course: " << gradebook2.getCourseName()
            << " and the instructor is called " << gradebook2.getInstructorName()

I didn't want to define a value for instructorName for the object gradebook1?
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 1000 total points
ID: 17807575
You can have a default value for a constructor parameter just like any function.

class GradeBook {
    const string courseName;
    const string instructorName;
public:
    GradeBook(const string& courseName,const string& instructorName = "");
    // ..
};

GradeBook::GradeBook(const string& courseName,const string& instructorName) : courseName(courseName),instructorName(instructorName)
{
}

int main()
{
    GradeBook latin("Latin","Mr Chips");
    GradeBook biology("Biology");
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question