how can i pass a class variable to a public function of the class and then return the class variable with the modifications?
This is a discussion on passing class within the C++ Programming forums, part of the General Programming Boards category; how can i pass a class variable to a public function of the class and then return the class variable ...
how can i pass a class variable to a public function of the class and then return the class variable with the modifications?
You don't need to pass a class variable to a class function. The class function has access to all class data variables already. like this:
if you want to display modified variable directly from ModifyData you could do this:Code:#include <iostream.h> class Sample { public: Sample(); int getData(); void ModifyData(); private: int data; }; Sample::Sample() : data(3) {} int Sample::getData() { return data; } void Sample::ModifyData() { data += 4; } int main() { Sample sample; cout << "data before modification = " << sample.getData() << endl; sample.ModifyData(); cout << "data after modification = " << sample.getData() << endl; return 0; }
Code:#include <iostream.h> class Sample { public: Sample(); int getData(); int ModifyData(); private: int data; }; Sample::Sample() : data(3) {} int Sample::getData() { return data; } int Sample:: ModifyData() { data += 4; return data; } int main() { Sample sample; cout << "data before modification = " << sample.getData() << endl; cout << "data after modification = " << sample.ModifyData() << endl; return 0; }