What are Accessor Functions Explain with an example-00467
This subjective question is related to the book/course vu cs302 Digital Logic Design. It can also be found in vu cs302 Mid Term Solved Past Paper No. 1.
Question 1: What are Accessor Functions, Explain with an example?
…
int rollNo;
public:
void setRollNo(int aRollNo){
rollNo = aRollNo;
}
};
Avoiding Error
void Student::setRollNo(int aRollNo){
if(aRollNo < 0){
rollNo = 0;
}(Page93)
Answer:
Accessor functions are used to access private data of the object, we provide accessor functions to get and set private data members of the class.
Example
class Student{…
int rollNo;
public:
void setRollNo(int aRollNo){
rollNo = aRollNo;
}
};
Avoiding Error
void Student::setRollNo(int aRollNo){
if(aRollNo < 0){
rollNo = 0;
}(Page93)