Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do setter and getter functions for a pointer class variable?

let say i have a student class with a program class pointer for programEnrolled? how to i do the getter and setter and how do i access the member inside the programEnrolled (programName, programFees) through programEnrolled?

and when should i use a pointer function?

class clsStudent
{
private:
    string studentName;
    string studentID;
    clsProgram *programEnrolled;
};

class clsProgram{
private:
    string programName;
    double programFees;
    string programCode;
};
like image 532
Alexius Lim Avatar asked Jun 26 '26 02:06

Alexius Lim


1 Answers

Why you need pointers in your program at all? By the way here is an example:

class clsStudent
{
public:
   void setProgram(clsProgram *x) { programEnrolled=x; }
   clsProgram *getProgram() const { return programEnrolled; }

  ...
};

clsStudent student;
student.getProgram()->programName;
like image 81
masoud Avatar answered Jun 28 '26 16:06

masoud



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!