Information Hiding in C++ Two labels public and private Determine visibility of class members A member that is public may be accessed by any method in any class Amember that is private may only be accessed by methods in its class Information hiding Data members are declared private, thus restricting access to internal details of the class Methods intended for general use are made public
11 ◼ Two labels: public and private ◼ Determine visibility of class members ◼ A member that is public may be accessed by any method in any class ◼ A member that is private may only be accessed by methods in its class ◼ Information hiding ◼ Data members are declared private, thus restricting access to internal details of the class ◼ Methods intended for general use are made public Information Hiding in C++
Constructors A constructor is a special method that describes how an instance of the class(called object) is constructed Whenever an instance of the class is created. its constructor is called C++ provides a default constructor for each class, which is a constructor with no parameters. But, one can define multiple constructors for the same class, and may even redefine the default constructor
12 ◼ A constructor is a special method that describes how an instance of the class (called object) is constructed ◼ Whenever an instance of the class is created, its constructor is called. ◼ C++ provides a default constructor for each class, which is a constructor with no parameters. But, one can define multiple constructors for the same class, and may even redefine the default constructor Constructors
Destructor A destructor is called when an object is deleted either implicitly, or explicitly(using the delete operation The destructor is called whenever an object goes out of scope or is subjected to a delete Typically, the destructor is used to free up any resources that were allocated during the use of the object C++ provides a default destructor for each class The default simply applies the destructor on each data member. But we can redefine the destructor of a class a C++ class can have only one destructor One can redefine the destructor of a class AC++ class can have only one destructor
13 ◼ A destructor is called when an object is deleted either implicitly, or explicitly (using the delete operation) ◼ The destructor is called whenever an object goes out of scope or is subjected to a delete. ◼ Typically, the destructor is used to free up any resources that were allocated during the use of the object ◼ C++ provides a default destructor for each class ◼ The default simply applies the destructor on each data member. But we can redefine the destructor of a class. A C++ class can have only one destructor. ◼ One can redefine the destructor of a class. ◼ A C++ class can have only one destructor Destructor
Pointers A pointer is a variable which contains addresses of other variables Accessing the data at the contained address is called 4104) dereferencing a pointer"or following a pointer (4100) 4096 pointer (4096) 7
14 Pointers ◼ A pointer is a variable which contains addresses of other variables ◼ Accessing the data at the contained address is called “dereferencing a pointer” or “following a pointer” n (4096) y (4100) x (4104) 4096 7 pointer
A Pointer Example Box diagram Memory Layout The code void doubleT(int x, a工 int a 16 8192 (8200) double工七 nt main(int argc, const char argv[l (8196 int a =16: double工七 16 doublet(g, &a)i (8192) return 0 x9 a gets 18
15 A Pointer Example The code void doubleIt(int x, int * p) { *p = 2 * x; } int main(int argc, const char * argv[]) { int a = 16; doubleIt(9, &a); return 0; } Box diagram Memory Layout x 9 p (8200) x (8196) a 16 main doubleIt p a (8192) 16 9 8192 main doubleIt a gets 18