displaying objects using overloaded constructors

Hello everyone,
I have created a class called "course". In main, I have created 3 objects called, course course1, course course2, and course course3. Course1 and 2 have predefined data; whereas, course3's data is from the user. I need to display all three courses, so the user can choose which one they want to manipulate. printCourse() is supposed to do all the displaying and prompting.

Mt question is how do I display my three objects (course1,course2,course3)? So far I am able to print out only one object(course1) or what ever object I choose. For example, on line 72 I can choose course2 instead of course1 and it will print out course2. How do I get all three objects to be passed into function printCourse()?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <iostream>
using namespace std;

//constants
const int MAX_CAPACITY = 30;
const int MIN_CAPACITY = 1;
const string COURSE_NUM = " course number: ";
const string COURSE_TITLE = " course title: ";

//prototype
string messagePrompt(string);
string courseNumPrompt();
bool courseNumValidity(string);
//int capacityValidation();

// clockType class specification
class course
{
  private:
    string courseNum;  			//stores the course number (e.g. CS361)
    string courseTitle; 		//stores the course title (e.g. "Control structures")
    int capacity; 				//stores the capacity of the classroom in number of students
    int numEnrolledStudents;	//stores the number of enrolled students 
    int stuID [MAX_CAPACITY];	//stores the 5 digit student id, with a max of 30 in the array
  public:
  	//constructors
    course();											//Default constructor
    course(string crseNum, string title, int cap);		//Constructor with parameters
    
    //mutator functions
    void setCourseNum();		//manipulates data for the course number
    void setCourseTitle();		//manipulates data for the course title
    void setCapacity() const;	//manipulates data for the classroom capacity
    //Accessor functions
    void getCourseNum();		//retrieves the course number
    void getCourseTitle();		//retrieves the course title
    void getCapacity();			//retrieves the capacity
    void getNumEnrolled();		//retrieves the number of enrolled students
    //facilitator functions
    string printCourse();			//display course data
    void printStudentIds();		//display all student ID's on student Id list
    void addOneStudent();		//add one student to course
    void dropOneStudent();		//drop one student from course
};


//**************************************************************************
//  FUNCTION:       main
//  DESCRIPTION:    Creates two clockType class objects and manipulates them.
//  OUTPUT:	
//      Return Value:	0 for success
//  CALLS TO:	    setTime, getTime, printTime, incrementSeconds, 
//                  incrementMinutes, incrementHours, equalTime
//**************************************************************************
int main()
{
	//variables
	string tempCourseNum;
    string tempCourseTitle;
    int cap;
    
    //constuctors
    course course1("CS361", "Control Structures", MAX_CAPACITY);  //constructor with parameters
	course course2("CS362", "Data Structures", 10);  			  //constructor with parameters
    course course3; 	//default constructor
    
	course3.setCourseNum();		//prompt and validate user for course number
	course3.setCourseTitle();	//prompt user for course title
	cout << endl;
	
	//course printCourse(course1,course2,course3);
	course1.printCourse ();

    return 0;
}//end main
//********************************************************************* 
// Function:         course::course 
// Description:      Default constructor
// Output:			 course object, initialized to all 0s
//********************************************************************* 
course::course() 
{
    courseNum = " ";
    courseTitle = " ";
    capacity = MIN_CAPACITY;
    numEnrolledStudents = 0;
}
//**************************************************************************** 
// Function:         course::course 
// Description:      Constructor with parameters
// Input:	Parameters: crseNum, title, cap - course number, title, and capacity
// Output:			 course object, set to parameter values
//****************************************************************************
course::course(string crseNum, string title, int cap)
{	
	courseNum = crseNum; 
	courseTitle = title; 
	capacity = cap;	
	//cout << "course3 = "<< courseNum<<" " <<courseTitle<<endl;
}
//**************************************************************************** 
// Function:         course::setCourseNum 
// Description:      retrieves the course number
// Input:	Parameters: N/A
// Output:			 N/A
//****************************************************************************
void course::setCourseNum()
{	
	courseNum = courseNumPrompt();	
	//cout << "course = "<< courseNum<<endl;
}
//**************************************************************************** 
// Function:         course::setCourseTitle 
// Description:      retrieves the course title
// Input:	Parameters: N/A
// Output:			 N/A
//****************************************************************************
void course::setCourseTitle()
{
	courseTitle = messagePrompt(COURSE_TITLE);
	//cout << "course = " <<courseTitle<<endl;
}
//******************************************************************************
//void course::getCourseNum()
//{
	//variables
//	string tempCourseNum;
	
//	tempCourseNum = courseNum;
//}
//****************************************************************************
void getCourseTitle()
{
	
}
//**************************************************************************** 
// Function:         messagePrompt 
// Description:      prompts the message for course title
// Input:	Parameters: prompt message (const)
// Output:			 response - returns the respnse
//****************************************************************************
string messagePrompt(string prompt)
{
	//variable
	string response;
	
	cout << "Please enter the students" << prompt;
	cin >> response;
	
	return response;
}
//*************************************************************************
//  FUNCTION:	 courseNumPrompt
//  DESCRIPTION: prompts for user to inpout student ID. Calls function 
//				 testValidity to test if the format is valid
//  INPUT:       NA:    
//  OUTPUT: 	 return:	id - returns the either NOT_FOUND (-1) or 
//								 the array number for the target ID 
//***************************************************************************
string courseNumPrompt()
{
	int cnt1 = 0;
	bool goodID = false;
	string id = " ";
	
	cout << endl << endl;
	cout << "Enter course number (e.g. CT234): " << endl;
    cin  >> id;
    
    for (cnt1; cnt1 < id.length(); ++cnt1)
        id[cnt1] = toupper(id[cnt1]);

    while (!goodID)
    {
        if (courseNumValidity(id) == true)
        {
        	cout << endl;
            cout << "INVALID ID FORMAT" << endl;
            cout << "Must start with two characters (A to Z) and end with three digits" << endl;
            cout << "Enter course number: " << endl;
	    	cin  >> id;
        }
        else
        {
        	cout << endl;
            cout << "VALID ID FORMAT" << endl;
            goodID = true;
        } 
    }
    return id;
}
//*************************************************************************
//  FUNCTION:	 courseNumValidity
//  DESCRIPTION: Validates the user input for student ID
//  INPUT:       Parameter:    id - text file to be read from
//
//  OUTPUT: 	 return:    id - returns either true or false to function
//								 userInput
//***************************************************************************
bool courseNumValidity(string id)
{
    bool invalidLetter = false;
    for (unsigned int i= 0; i < id.length()-1; i++)
    {
        if (i < 2 && isdigit(id[i]))
            invalidLetter = true;
        if (i > 1 && isalpha(id[i]))
            invalidLetter = true;
    }
    if (id.length()!= 5)
    	invalidLetter = true;
    	
    return invalidLetter;
}
//*****************************************************************************
string course::printCourse()
{
	//variable
	string crseNum1;
	
	cout << "Choose a course to manage:" << endl;
	cout << "\t" << courseNum << " - " << courseTitle<< endl;
	cout << "\t" << courseNum << " - " << courseTitle<< endl;
	cout << "\t" << courseNum << " - " << courseTitle<< endl;
	cout << "Enter the course number (e.g.CS200) oe E to exit: ";
	cin >> crseNum1;
	
	return crseNum1;
}
Last edited on
You can call them after each other.

1
2
3
course1.printCourse();
course2.printCourse();
course3.printCourse();


If you store the objects in an array it is possible to only mention printCourse() once if you place it inside a loop.
Oh yea, that makes sense. Store the courses in an array and then display it through a for loop. I was so wrapped up in figuring out classes and objects, I didn't even think about that. Thank you.
So I found out that I am not supposed to use any other arrays or structs. Also, I can't call the course1,2,3 one after another. It needs to display all three objects at the same time, so I need to somehow pass in all three courses and display them. Is there any way of doing that?
You could have a function that takes three courses as arguments and carries out the printing of them.
so something like:

main:
course.printCourse(course1,course2,course3); //function inside main with
//parameters of all 3 objects

 
string printCourse(course, course, course); //inside of class course 

1
2
//outside function
string course::printCourse(string crse1, string crse2, string crse3)


I actually tried this, but it doesn't compile. I am sure I am not coding this correctly but I'm not sure how else to do it. I have a good idea of work you are talking about but I'm just not to sure how to implement it. Thank you for your help.
Hi,

You need the function to be outside of any of the classes. That is, it is not a member function of the class.

1
2
3
4
5
std::string Print3Courses(const course& Course1, 
                                              const  course& Course2, 
                                              const course& Course3) {

}


inside of that function make use of the class interface to do the work. This is why course::printCourse() is misleading.

Notice how each parameter is a const reference?

The course::printCourse() function is misleading, it should print out the details of the course, not ask for the course number - that doesn't make sense - you are dealing with 1 object at this point.

Try to avoid having trivial get / set functions for each member variable. The initial values of member data can be set with a parameter list in a constructor - this is better than the assignment which you are doing now. set functions are only necessary if one needs to change a value after the object is constructed, often there is no need for this.

Functions that don't change the state (values of the member variables) should be marked const

Consider placing the class definition (lines 17 to 44) in it's own header file (course.hpp). I like to use .hpp rather than .h as the extension because it means it has c++ code in it. #include this header file into any .cpp file which uses the class. Place the definitions of the member functions in a .cpp file with the same name as the class (course.cpp)

Thanks everyone for your input. This gives me a good idea on how to tackle this.
Topic archived. No new replies allowed.