C++ Language - Session 4: Class Basics - FPT University

ppt 26 trang hoanguyen 4241
Bạn đang xem 20 trang mẫu của tài liệu "C++ Language - Session 4: Class Basics - FPT University", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pptc_language_session_4_class_basics_fpt_university.ppt

Nội dung text: C++ Language - Session 4: Class Basics - FPT University

  1. Session 4 - Class Basics Session 4 - Class Basics 1/26
  2. Objectives • Class Basics – Classes – UML – Privacy – Constructors – Destructors – Arrays of Objects Session 4 - Class Basics 2/26
  3. Class Basics • A class is a derived data type the members of which are private by default. • Classes are much more common in object- oriented programming than structs. Session 4 - Class Basics 3/26
  4. CLASSES • The keyword class identifies a class. class Student { int no; char grade[14]; public: void set(int n, const char* g); void display() const; }; Session 4 - Class Basics 4/26
  5. Instance of a Class(1) • An instance of a class is called object. • The declaration of an instance allocates memory for the object. • The form of an instance declaration is Identifier instance; – Identifier is the name of the class. – instance is the name of the object for which memory is allocated. Session 4 - Class Basics 5/26
  6. Instance of a Class(2) • For example Student a, b, c, d, e; – This declaration allocates memory for five students. Each allocation includes space for the data members - no and grade - one copy for each object. Session 4 - Class Basics 6/26
  7. Instance of a Class(3) • The instances of a class share the member function code with all other instances of that class. • The program accesses this code in function calls from all instances of the class, but uses the data values stored in the object specified in the call to the function. Session 4 - Class Basics 7/26
  8. Instance of a Class(4) Session 4 - Class Basics 8/26
  9. Instance Variables • We call the data members of an object its instance variables. Instance variables may be of – built-in data type (int, double, char, etc.) – derived data type (struct, class) – pointer data type to instances of data types (primitive or derived) Session 4 - Class Basics 9/26
  10. UML(1) • The Unified Modelling Language (UML) is a popular systems language that uses standard symbols for depicting classes and objects. • Some companies use UML as their software design standard. Session 4 - Class Basics 10/26
  11. UML(2) • UML includes graphical representations for various design components. The simplest graphical representation is the class diagram. This is a rectangular box with three compartments – the top compartment contains the name of the class – the mid compartment contains the names and data types of the instance variables – the bottom compartment contains the names and return data types of the member functions Session 4 - Class Basics 11/26
  12. UML(3) • The member names are suffixed with a colon (:) followed by the data type. • The names of the private members are prefixed with a - and the names of the public members are prefixed with a +. Session 4 - Class Basics 12/26
  13. UML(4) • UML naming rules include the following: – begin a class name with an upper case letter (for example, Student, Transaction) – begin a member name with a lower case letter (for example, no, grade, set, display) – use lower case throughout a name except for the first letter of every word within the name (for example, noOfStudents) • In UML, data members are "attributes" and functions are "operations". Session 4 - Class Basics 13/26
  14. Privacy(1) • The private members of any object of a particular class are accessible to any other object of that same class. That is, privacy holds at the class level, not the object level. Session 4 - Class Basics 14/26
  15. Privacy(2) • For example: class Student { int no; char grade[14]; public: void set(int n, const char* g); void display() const; void set(const Student& st); }; void Student::set(const Student& st) { no = st.no; strcpy(grade, st.grade); } Session 4 - Class Basics 15/26
  16. Constructors(1) • A constructor is a special member function that is called during the creation of an object. • We use the constructor to initialize the instance variables and to execute any preliminary logic. Session 4 - Class Basics 16/26
  17. Constructors(2) • Object construction proceeds in the following order – allocate memory for each instance variable in the order listed in the class declaration, – execute the constructor code • The form of a constructor prototype is Identifier ( type identifier[, ] ); – Identifier is the name of the class. • A constructor does not return a value or have a return data type. Session 4 - Class Basics 17/26
  18. Constructors(3) class Student { int no; char grade[M+1]; public: Student(); void set(int, const char*); void display() const; }; Student::Student() { no = 0; grade[0] = '\0'; } Session 4 - Class Basics 18/26
  19. Constructors(4) • The compiler constructs objects in the order of their declaration. • For Example: Student harry, josee; Session 4 - Class Basics 19/26
  20. Safe Empty State(1) • Initializing the instance variables of an object ensures that the object is in a well-defined state from the moment of its creation. We refer to such a setting as a safe empty state. • If member functions are called in an 'unusual' order on an object that is in a safe empty state, the function calls will not break the object. Session 4 - Class Basics 20/26
  21. Safe Empty State(2) #include using namespace std; int main ( ) { Student harry, josee; harry.display(); josee.display(); harry.set(1234,"ABACA"); josee.set(1235,"BBCDA"); harry.display(); josee.display(); return 0; } Session 4 - Class Basics 21/26
  22. Destructors(1) • A destructor is a special member function that is called just before an object goes out of scope. • Object destruction takes the following order – execute the destructor code – deallocate memory for each instance variable in opposite order to that listed in the class declaration. Session 4 - Class Basics 22/26
  23. Destructors(2) • The destructor takes its name from the class itself and prefixes that name with the tilde symbol (~). • The form of a destructor prototype is ~Identifier ( ); – Identifier is the name of the class. • A destructor does not take any parameters, does not return a value, and does not have a return data type. Session 4 - Class Basics 23/26
  24. Destructors(3) class Student { int no; char* name; // address in freestore char grade[M+1]; public: Student(); ~Student(); void set(int n, const char* g, const char* nm); void display() const; }; Student::~Student() { if (name != NULL) delete [] name; } Session 4 - Class Basics 24/26
  25. ARRAYS OF OBJECTS • An array of objects occupies contiguous memory. • Upon creation, each element, starting from the first, calls the no-arguments constructor for itself. • Upon destruction, each element, starting from the last element created and proceeding towards the first, calls the destructor for itself. Session 4 - Class Basics 25/26
  26. Summary • Class Basics – Classes – UML – Privacy – Constructors – Destructors – Arrays of Objects Q&A Session 4 - Class Basics 26/26