0

How to access the data prsnt in private??

19th Apr 2017, 3:56 PM
sai Geethika
sai Geethika - avatar
5 Answers
+ 12
Make member functions inside the class with public access to get and set the private part's variable values... Or you may use friend functions to allow the particular function to access the private data variables... Eg: class Example { private: int data; public: int get() {return data;} void set(int d) {data=d;} friend int view() {return data;} }; int main(void) { Example ex; ex.set(2); int c=ex.get(); int d=view(); cout<<c<<d; //Prints 22, as both c and d are 2. }
19th Apr 2017, 4:36 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
using friend function
19th Apr 2017, 5:13 PM
sowmya
+ 1
Most of the time, you need to be a friend to get into someone's privates.
19th Apr 2017, 8:49 PM
Denis Felipe
Denis Felipe - avatar
+ 1
using some other functions (methods) in public section example : #include <iostream> #include <string> using namespace std; class car { private: long price = 25000; public: void showPrice() { cout << price << endl; } }; int main() { car BMW; BMW.showPrice(); // outputs 25000 return 0; }
22nd Jun 2017, 4:58 PM
Mario Bahaa
Mario Bahaa - avatar
0
tq
26th Apr 2017, 12:30 AM
sai Geethika
sai Geethika - avatar