Outline CS11600: Introduction to � Data encapsulation, ADT, API Computer Programming (C++) � Struct and typedef � Classes Lecture 7 � Define directives � Private and public members � Constructors Svetlozar Nestorov � Destructors University of Chicago 1/20/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 2 Data Encapsulation Struct � Create new data type by grouping data � Abstract data types (ADT) members (of other types). � Separate interface from implementation . � General form: � All access to data is through the interface. struct newType { � Application program(ming) interface (API) Type1 member1; � Benefits Type2 member2; • Modularity, portability … }; • Data integrity � Notice ; after definition. � Design methods 1/20/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 3 1/20/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 4 Struct Examples Working with Structs � Access data members: struct Student { struct ListNode { int SID; int data; name.member ( name is a struct ) name->member ( name is a pointer to a struct ) float gpa; ListNode *next; � Dynamic allocation of struct: int age; }; new newType ( returns a pointer to a struct ) }; � Copy and assignment Student joe; Student kim = jane; (copy) Student jane = {11111, 4.0, 19}; Student *leo = new Student; ListNode head; *leo = kim; (assignment) 1/20/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 5 1/20/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 6 1
Typedef Classes � Basic building block of C++ programs � Define synonym name (alias) for a type: • Equivalent to structs. typedef Type newName; � General form: Can be used as Type � Examples: typedef int number; class ClassName { Internals (inside view) typedef int[20] controls; private: typedef double *doublePtr; …members… typedef ListNode *ListNodePtr; data and/or functions struct ListNode { public: int data; …members… ListNodePtr next; Interface (outside view) }; } 1/20/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 7 1/20/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 8 Class Definition Class Implementation � Often a class is defined in .h (or .hh ) file and � Outside of class definition, member implemented in .C or .cc file. functions are prefaced by ClassName:: � Implementation is definition of member � Access members (both data and functions) functions. similar to struct. � Start .h file with: name.member ( name is an object ) #ifndef ClassNameH Multiple #includes will name->member( name is a pointer to an object ) #define ClassNameH include .h file only once � End .h file with: #endif 1/20/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 9 1/20/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 10 Private and Public Members Constructors � Initialize object when created. � Private data members and private member • Allocate memory for private data members if functions can be invoked only within class necessary. implementation, i.e. member function definitions. � General form: � Public members can be accessed anywhere. ClassName(…); � Member function with the same name as the class Calc { int Calc::add(…) { class and no return type. void helper(…); … � Many constructors are allowed. public: helper(…); • Argument lists must differ. … int add(…); � Default constructor: either no arguments or all }; }; arguments have default values. 1/20/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 11 1/20/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 12 2
Destructors Linked List Example � Invoked when object is deleted: � Simple singly linked list • Explicit: delete � Class interface • Implicit: on function returns � Class implementation � Should deallocate all dynamically allocated memory within object. � General form: ~ClassName(); � At most one destructor; no arguments, no return type. 1/20/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 13 1/20/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 14 3
Recommend
More recommend