Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a set of codes that I'm supposed to make the necessary changes as per question below. I was told to remove the friend

I have a set of codes that I'm supposed to make the necessary changes as per question below. I was told to remove the "friend" - have tried but kept getting error. Any help is greatly appreciated please!

Question from Lab 03:

  1. Use Registration.zip from the previous exercise. Rename the Course class to Unit as it will be used to represent a unit at Murdoch.Modify the class so that it has unit name, unit id and credits. Section name is not used. The output operators use the new attributes to display. Provide appropriate set and get methods for the class. Create a data file with unit information relevant to Murdoch units. Modify the main program so that the new data file is used with the correct unit information. Test your program to make sure it works as before.
  2. Create a new class call Result. Result will contain unit and the marks (one floating point number) obtained for that unit. Provide appropriate set and get methods for the class. Think about how the class will be used. The registration class will be modified to have a list of Results instead of a list of Courses. Modify the data file to cater for marks. The output operator for Result should output the unit information and the marks. The output for registration in the output data file will contain the Student ID, Semester, unit information and marks for each unit. The output should be in a similar format as the output in the previous exercise except it will have more relevant information. A sample run is given below. Student ID: 12345678

Semester: 1

Unit ID: ICT283

Unit Name: Data_Structures_And_Abstractions

Credits: 3

Marks: 90

Unit ID: ICT289

Unit Name: Computer_Graphics

Credits: 3

Marks: 97

Number of Units = 2

Total Credits = 6

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( 5 ); 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 // C string library

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 int credits; // 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions