i have 2 class, unit and result. The unit have a function 'readData' that read from a file and write into the class variables. When i call the function from unit class eg. 'u.getUnitName()' i return the correct 'unitName' as per from the file.
But if i call the getUnitName from the result class, it return the 'unitName' from the 'Unit' class constructor.
#include "result.h"
usingnamespace std;
int main()
{
Unit u1;
result r1;
u1.readData(u1);
cout << u1.getUnitName() << endl;
// return the correct unitName same as "rinput.txt".
cout << r1.getUnitName() << endl;
// return the unitName "nil" from 'unit' default constructor.
}
need advice on how can i retrieve the correct unitName from the getter in result class. My intention is to retrieve all the data from 'unit' and retrieve it in the 'result' class and there will be a 'writeData' function in it to write the unitName, read from the file for a variable call 'mark' and write and output into "routput.txt".
i've tried declaring 'unit u1' public in the result.h and call r1.u1.readData() in my main. The result was reverse. Now my 'u1.getUnitName()' return nil and my 'r1.getUnitName' return the unitName from the file.
What could be the explanation to this? As i couldnt understand the logic.
In your original post, line 5 reads from "rinput.txt" into u.unitName. Then line 7 overwrites the value you just read in u.unitName with whatever is in this->unitName. The same is true for u.unitId and u.credits.
So the reason your getter "isn't working" may be because you've stored the wrong data in the first place.
dhayden i think i got your point. I tried to store the data in 'u' rather in the this->unitName. So that is why my getter isn't retrieving the data i wanted.
i tried expanding my result class, i tried to declare unitName, unitId, credits as private variables in my result class to store the data i retrieved from unit class. Am i on the right track? by re-creating similar var that has already been declared in unit class.
i thought that ofstream can be used in the same way as the ifstream in this case but i was wrong.
i tried calling r1.getUnitName(), it return the correct data but once this function is called, the output is just some random numbers. No errors. Is this another case of wrong data storage again??
my task given is to output unit information and mark as for each unit FROM the result class. So naturally i will think that the writeData function should exist.
#ifndef COURSE_H
#define COURSE_H
#include <iostream>
#include <string.h> // C string library
usingnamespace std;
constunsigned CourseNameSize = 10;
class Course {
public:
Course();
Course( string uName, string uId , int cred );
int GetCredits() const; // Get the number of credits.
string getUnitName () const;
string getUnitId () const;
void SetCredits( int cred ); // Set the number of credits.
void setUnitName(string uName);
void setUnitId(string uId);
private:
string unitName; // course name, C style string. not a C++ string object
string unitId; // section (letter) can be enrolment mode
int credits; // number of credits
};
ostream & operator <<( ostream & os, const Course & C );
istream & operator >>( istream & input, Course & C );
#endif
I stepped backwards to test my getters before proceeding to the IO. when i call for both getUnitName from the 2 classes it returns different values. I know that there might be a problem within the overloaded operator(im tasked to unfriend all the overloaded operator).
Am i right to say that the value is passed to the C.setUnitName(unitName) in the overloaded operator and is not set on the this->unitName therefore no matter how i call it from another class i got back the value from the default constructor?
if so is there any ways to call the values that are set in the overloaded operator over from another class?
Have you chosen to turn the name of class “Unit” into “Course”, but keeping the file name “unit.h” (all lowercases)? Or have you changed it in “COURSES.H” (all uppercases)?
#include <string.h>
Is there a specific reason why you like <string.h> better than <cstring>?
im tasked to unfriend all the overloaded operator
If a function is not friend of a class, it should not appear in a class header, IMHO.
You are free to use the names you want, of course, but I think that now that your class name is “Course”, those identifiers are becoming misleading. Are you sure you’ll easily be able to re-read your code let’s say five months hence?
Giving us a minimum compilable code that we could comfortably copy and paste to make tests, along with some examples of your input and output files would make it *so* much easier to help you...