Question: hi, the following code is c++ and it is running. can anyone explain every steps so that i can understand. MAIN.CPP // MAIN.CPP - Case
hi, the following code is c++ and it is running. can anyone explain every steps so that i can understand.
MAIN.CPP
// MAIN.CPP - Case Study, Student Registration
// Count the number of courses taken by the student, calculate total credits
// author KRI
//
#include
#include
#include "course.h"// Course class declaration
#include "regist.h"// Registration class declaration
using namespace std;
// Main program:
// Open an input file stream, read a Registration object,
// including its list of courses. Redisplay all information,
// and calculate the total credits taken. Write the results
// to a file stream.
int main()
{
ifstream infile( "rinput.txt" );
if( !infile ) return -1;
Registration R;
infile >> R;
ofstream ofile( "routput.txt" );
// Use a debugger and track down the calls made "behind the scene"
ofile << R
<< "Number of courses = " << R.GetCount() << ' '
<< "Total credits= " << R.GetCredits() << ' ';
// Declare and initialize a Course, and modify
// its credits.
Course aCourse( "MTH_3020", 'B', 2 );
aCourse.SetCredits( 8 );
cout << aCourse << endl; // the operator << for Course is called
return 0;
}
COURSE.H
// COURSE.H - Course class definition
// author KRI
// modified smr
#ifndef COURSE_H
#define COURSE_H
#include
#include
using namespace std;
const unsigned CourseNameSize = 10;
class Course {
public:
Course();
Course( const char * nam, char sect, unsigned cred );
// Construct a course from a name, section letter,
// and number of credits.
unsigned GetCredits() const;
// Get the number of credits.
void SetCredits( unsigned cred );
// Set the number of credits.
// These operators have been made friends. They have
// privileged access to class internals.
// Very useful for debugging the class, but not very good for class design.
// We will keep using it for now but you will have a chance in a later lab
// to redesign this class.
friend ostream & operator <<( ostream & os, const Course & C );
friend istream & operator >>( istream & input, Course & C );
private:
char name[CourseNameSize];// course name, C style string. not a C++ string object
char section;// section (letter) can be enrolment mode
intcredits;// number of credits
};
inline unsigned Course::GetCredits() const
{
return credits;
}
inline void Course::SetCredits( unsigned cred )
{
credits = cred;
}
#endif
COURSE.CPP
// COURSE.CPP - Course class implementation
#include "course.h"
Course::Course()
{
name[0] = '\0'; // it is a char * string, not a C++ string object.
}
Course::Course( const char * nam, char sect,
unsigned cred )
{
strncpy( name, nam, CourseNameSize );
section = sect;
credits = cred;
}
istream & operator >>( istream & input, Course & C )
{
input >> C.name >> C.section >> C.credits;
return input;
}
ostream & operator <<( ostream & os, const Course & C )
{
os << "Course:" << C.name << ' '
<< "Section: " << C.section << ' '
<< "Credits: " << C.credits << ' ';
return os;
}
REGIST.H
// REGIST.H - Registration class definition
// author KRI
// modified smr
#ifndef REGIST_H
#define REGIST_H
#include
#include "course.h"
using namespace std;
const unsigned MaxCourses = 10;
class Registration {
public:
Registration();
unsigned GetCredits() const;
unsigned GetCount() const;
// These operators have been made friends. They have
// privileged access to class internals.
// Very useful for debugging the class, but not very good for class design.
// We will keep using it for now but you will have a chance in a later lab
// to redesign this class.
friend ostream & operator <<( ostream & os,
const Registration & R);
friend istream & operator >>( istream & input,
Registration & R );
private:
long studentId;// student ID number
unsigned semester;// semester year, number
unsigned count;// number of courses
Course courses[MaxCourses]; // array of courses
};
inline unsigned Registration::GetCount() const
{
return count;
}
#endif
REGIST.CPP
// REGIST.CPP - Registration class implementation.
#include "regist.h"
Registration::Registration()
{
count = 0;
}
unsigned Registration::GetCredits() const
{
unsigned sum = 0;
for(unsigned i = 0; i < count; i++)
sum += courses[i].GetCredits();
return sum;
}
istream & operator >>( istream & input, Registration & R )
{
input >> R.studentId >> R.semester >> R.count;
for(unsigned i = 0; i < R.count; i++)
input >> R.courses[i];// track down which >> operator is called. Hint: look at what is being input.
return input;
}
ostream & operator <<( ostream & os, const Registration & R )
{
os << "Student ID: " << R.studentId << ' '
<< "Semester:" << R.semester << ' ';
for(unsigned i = 0; i < R.count; i++)
os << R.courses[i] << ' ';
return os;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
