Presentation is loading. Please wait.

Presentation is loading. Please wait.

Private.

Similar presentations


Presentation on theme: "Private."— Presentation transcript:

1 Private

2 The private designator
‘private’ means that the variable is not accessible to anything outside of the class most data members of classes belong in the private section so that it cannot be accessed directly data hiding encapsulation

3 public vs. private class Item class Item { { public: private:
string name; } class Item { private: string name; } Item a; a.name = “Aristotle” Item a; a.name = “Aristotle”

4 Accessors So, if data in a private section cannot be accessed, how can it ever be used? Answer: any function in the class has access to the private data members, so create functions that are public, that others can use: accessor functions (i.e. getName()) can return the value to the client code mutator functions (i.e. setName()) can change the value of the private

5 accessors and mutators
class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; }; // mutator private: string name; } Item a; a.setName(“Aristotle”); cout << a.getName() << endl;

6 Result Functions that do not belong to the class cannot access it’s private members unless they use the accessor and mutator functions of the object of that class.

7 Good outside functions
class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator private: string name; } void print(const Item& x) { cout << x.getName() << endl; } Item a; a.setName(“Aristotle”); cout << a.getName() << endl; print(a); This will compile without any problems because the accessor getName() is public.

8 Bad outside functions class Item { public:
string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator private: string name; } void print(const Item& x) { cout << x.name() << endl; } Item a; a.setName(“Aristotle”); cout << a.getName() << endl; print(a); This will not compile! Member ‘name’ is private member of class ‘Item’

9 However... There is one exception to watch out for.
It happens whenever an object is passed into a method contained in another object of the same sort. In this case, private data can be accessed directly!

10 Internal functions and private
class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.getName() << endl; } private: string name; } Item a , b; a.setName(“Aristotle”); b.setName(“Plato”); a.print(b); This will compile without any problems because print is a member function of Item a and getName is a member function of Item b.

11 Direct access to private
class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.name << endl; } private: string name; } Item a , b; a.setName(“Aristotle”); b.setName(“Plato”); a.print(b); This will also compile and run! But why? Isn’t name supposed to be in the private portion of x? How can this function get at it?

12 Answer The answer to this mystery revolves around the way we conceive of ADTs. When an ADT is instantiated it becomes an object. An ADT can be instantiated many times as many objects. Here is one way (the wrong way) to think of what is going on...

13 An easy misconception a b c d Item a , b, c, d;
class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.name << endl; } private: string name; } a class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.name << endl; } private: string name; } b class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.name << endl; } private: string name; } Item a , b, c, d; a.setName(“Aristotle”); b.setName(“Plato”); a.print(b); c class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.name << endl; } private: string name; } d

14 An easy misconception a b If this were going on it makes
class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.name << endl; } private: string name; } b class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.name << endl; } private: string name; } If this were going on it makes sense that x.name should look impossible. After all, x is an object being passed into something that is trying to get directly at its private members! Item a , b, c, d; a.setName(“Aristotle”); b.setName(“Plato”); a.print(b);

15 An easy misconception a b Item a , b, c, d; a.setName(“Aristotle”);
class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.name << endl; } private: string name; } b class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.name << endl; } private: string name; } Item a , b, c, d; a.setName(“Aristotle”); b.setName(“Plato”); a.print(b); However, THIS IS NOT WHAT IS HAPPENING.

16 What is going on? Remember that Item is a class.
Both a and b are Items. They inherit their characteristics from the class definition. a ‘is-a’ Item, and b ‘is-a’ Item.

17 So, instead of this conception
class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.name << endl; } private: string name; } a class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.name << endl; } private: string name; } b class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.name << endl; } private: string name; } Item a , b, c, d; a.setName(“Aristotle”); b.setName(“Plato”); a.print(b); c class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.name << endl; } private: string name; } d

18 We should use this one! a b Aristotle Plato Item a , b;
class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.name << endl; } private: string name; } a b Aristotle Plato Item a , b; a.setName(“Aristotle”); b.setName(“Plato”); a.print(b);

19 a and b share pointers to functions
class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.name << endl; } private: string name; } Although a and b have different data members, they share the same pointers to functions of the Item class. ... void print(const Item& x) { cout << x.name << endl; } a b Aristotle Plato

20 a and b share pointers to functions
class Item { public: string getName() const {return name;} // accessor void setName(string n) { name = n; } // mutator void print(const Item& x) { cout << x.name << endl; } private: string name; } There is only one print method. When it runs, it checks to see if the object it is being called on is of the Item class. If it is, then print has automatic access to the private data members of the class, by definition. ... void print(const Item& x) { cout << x.name << endl; } // is legal a b Aristotle Plato


Download ppt "Private."

Similar presentations


Ads by Google