Interface and implementation In C++ it is more common to separate the class interface from its implementation The interface lists the class and its members(data and functions) The implementation provides implementations of the functions
16 Interface and Implementation ◼ In C++ it is more common to separate the class interface from its implementation. ◼ The interface lists the class and its members (data and functions). ◼ The implementation provides implementations of the functions
IntCell:: Intcell( int initialvalue class Intcell storedvalue( initialvalue d()const xplicit Intcell( int initialvalue =0) I return storedvalue; t read( oid write( int x void Intcell: write( storedvalue int storedvalue; Iintcellh ntce∥p The interface is typically placed in a file that ends withh. The member functions are defined as Return Type Function Name(parameterList The implementation file typically ends with. cpp,CC, or C. The member functions are defined as follows Return Type ClassName: FunctionName(parameterList Scoping operato
17 class IntCell { public: explicit IntCell( int initialValue = 0 ); int read( ) const; void write( int x ); private: int storedValue; } IntCell::IntCell( int initialValue ) : storedValue ( initialValue ) { } int IntCell::read( ) const { return storedValue; } void IntCell::write( ) { storedValue = x; } The interface is typically placed in a file that ends with .h. The member functions are defined as: ReturnType FunctionName(parameterList); The implementation file typically ends with .cpp, .cc, or .C. The member functions are defined as follows: ReturnType ClassName::FunctionName(parameterList) { …… } IntCell.h IntCell.cpp Scoping operator
Object Pointer declaration Declaration Intcell p; //defines a pointer to an object of class Intell The indicates that p is a pointer variable; it is allowed to point at an IntCell object The value of p is the address of the object that it points at P is uninitialized at this point The use of uninitialized pointers typically crashes programs
18 Object Pointer Declaration ◼ Declaration IntCell * p; //defines a pointer to an object of class IntCell • The * indicates that p is a pointer variable; it is allowed to point at an IntCell object. • The value of p is the address of the object that it points at • P is uninitialized at this point • The use of uninitialized pointers typically crashes programs
Dereferencing Pointers Dynamic object creation p= new Int Cell 8888 8888 In C++ new returns a pointer to the newly created object Garbage collection C++ does not have garbage collection When an object that is allocated by new is no longer referenced, the delete operation must be applied to the bject delete p
19 ◼ Dynamic object creation p = new IntCell; In C++ new returns a pointer to the newly created object. ◼ Garbage collection ◼ C++ does not have garbage collection ◼ When an object that is allocated by new is no longer referenced, the delete operation must be applied to the object delete p; p 8888 8888 Dereferencing Pointers
Dereferencing Pointers(cont) Using a pointer We can get the value of the object pointed at by a pointer either by using operator * or by using operator -> Intcell a a = *p; //variable a gets the value of object pointed at by p b= p->read()i//the value of the data member storedvalue of // the object pointed at by p is assigned to b
20 ◼ Using a pointer We can get the value of the object pointed at by a pointer either by using operator *, or by using operator -> IntCell a; int b; …… a = *p; //variable a gets the value of object pointed at by p b = p->read( ); //the value of the data member storedValue of // the object pointed at by p is assigned to b Dereferencing Pointers (cont)