Professional Documents
Culture Documents
Object-Oriented Programming
• this pointer:
- Implicit parameter passed to a member
function
- points to the object calling the function
• const member function:
- does not modify its calling object
CpClass c1(5);
if (true) c1.p
{
5
CpClass c2;
c2 = c1;
} c2.p
// c1 is corrupted
// when c2 goes
// out of scope and
// its destructor
// executes
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11-21
Problems of Sharing Dynamic Storage
class CpClass
{
int *p;
public:
CpClass(const CpClass &obj)
{ p = new int; *p = *obj.p; }
CpClass(int v=0)
{ p = new int; *p = v; }
~CpClass(){delete p;}
};
class string
{
public:
string(char *); //convert
…
};
class Parent
Objects of Parent have
{ members
int a;
void bf(); int a; void bf();
};
class Child : public
Parent Objects of Child have
members
{
int c; int a; void bf();
void df(); int c; void df();
};
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11-54
11.11 Protected Members and Class
Access
protected
private: x base class x inaccessible
protected: y protected: y
public: z protected: z
public
private: x base class x inaccessible
protected: y protected: y
public: z public: z