Fundamentals of C++ Programming - 06: Class - Dustin Nguyen, PhD

Class:
❖ Concept and definition
❖ Encapsulation
❖ Static, const members
❖ Constructor/Destructor
❖ Overloading operators
❖ Pointer and dynamic allocation
❖ Friendship and inheritance 
pdf 50 trang xuanthi 27/12/2022 1540
Bạn đang xem 20 trang mẫu của tài liệu "Fundamentals of C++ Programming - 06: Class - Dustin Nguyen, PhD", để tải tài liệu gốc về máy hãy click vào nút Download ở trên.

File đính kèm:

  • pdffundamentals_of_c_programming_06_class_dustin_nguyen_phd.pdf

Nội dung text: Fundamentals of C++ Programming - 06: Class - Dustin Nguyen, PhD

  1. Outcomes ❖ Understand the concept of Class. ❖ Understand advantages of Object Oriented Programming (OOP). ❖ Be able to program using OOP technique. 2
  2. Class ❖ C++: not a pure object-oriented language ❖ OOP: a programming paradigm based on the concept of “objects” ❖ Programs are designed by making them out of objects that interact with one another. ❖ E.g.: C++, Python, Smalltalk, Delphi, Java, C#, Perl, Ruby, PHP, etc. 6
  3. Class ❖ OOP’s terminologies ❖ Object is an instant of a particular class ❖ Procedures are known as methods ❖ Variables are known as fields, members, attributes, or properties ❖ Class: a user defined datatype which groups together related pieces of information ❖ Data + Methods 8
  4. Class ❖ class [: ]{ : ; : ; } [ ]; ❖ Default access specifier: private 10
  5. Class ❖ Encapsulation ❖ Packaging related stuff together ❖ User need to know only public methods/data of the object: interface ❖ Interfaces abstract away the details of how all the operations are performed ❖ “Data hiding”, “black box”. 12
  6. Class ❖ Example: Student.cpp Student.h #include #include “Student.h” class Student { int ID; void Student::doHomework(HW_t &personalHW) { char name[50]; if (hasTime()) playLoL(); public: else if (deadlineTomorrow()) overnight(); void setName(char* pStr) { else goToSleep(); if (pStr && strlen(pStr) > 0) } strcpy(name, pStr); } void Student::takeExam(Exam_t test, float* grade) { char* getName() { return name; } if (isEasy(test)) *grade = 7 + (random() % 30) * 0.1f; void doHomework(HW_t &); else if (!isHard(test)) void takeExam(Exam_t, float*); *grade = 5 + (random() % 30) * 0.1f; }; else *grade = (random() % 50) * 0.1f; } 14
  7. Class ❖ Example test.cpp Student.h class Student { #include int ID; #include “Student.h” char name[50]; public: #define MAX_MEMBERS 100 void setName(char* pStr) { if (pStr && strlen(pStr) > 0) int main(int narg, char argv) { strcpy(name, pStr); Student sList[MAX_MEMBERS]; } for (int i = 0; i < MAX_MEMBERS; i++) { void setID(int i) { ID = i; } sList[i].setName(“unknown”); char* getName() { return name; } sList[i].setID(i); void doHomework(HW_t &); } void takeExam(Exam_t, float*); return 0; }; } 16
  8. Class ❖ Pointer to class ❖ this: a pointer to the object itself class Student { int ID; char name[50]; public: void setName(char* pStr) { if (pStr && strlen(pStr) > 0) strcpy(this->name, pStr); } char* getName() { return name; } void doHomework(HW_t &); void takeExam(Exam_t, float*); }; 18
  9. Class ❖ Static members: class Student { #include “student.h” int ID; #include char name[50]; public: Student::nStudents = 0; static int nStudents; int main(int narg, char argv) { void addStudent() { ID = nStudents++; } Student A, B; void delStudent() { cleanup(); nStudents ; } A.addStudent(); int getNStudents() { return nStudents; } B.addStudent(); void setName(char* pStr) { cout 0) name, pStr); B.delStudent(); } cout<< “Number of students after remove B: ” char* getName() { return name; } << Student::getNStudents() << “\n”; void doHomework(HW_t &); } void takeExam(Exam_t, float*); }; 20
  10. Constructor/Destructor
  11. Constructor/Destructor ❖ Constructor: ❖ Can not be called explicitly as member functions ❖ Default constructor: take no parameters ❖ Is called when an object is declared but is not initialized with any arguments. ❖ E.g.: Student A(“Tam”), B(“Thu”, 1.7), C; CSE_Course KTLT(A, B, C); 24
  12. Constructor/Destructor ❖ Constructor: ❖ Member initialization ❖ E.g.: Student() : ID(-1) { . . . } Student() : ID{-2} { . . . } ❖ This initialization technique is required when a class contains other objects which has no default constructor (has to be initialized with parameters) 26
  13. Constructor/Destructor ❖ Copy constructor ❖ ([const] &); ❖ Student::Student(const Student& ref) : ID(ref.ID), name(ref.name) {} ❖ Shallow copy vs. deep copy ❖ E.g.: Student b(a), c = a; 28
  14. Constructor/Destructor ❖ Move constructor ❖ ([const] &&); ❖ Student b = Student(“NV”);// move-construction ❖ Move assignment ❖ & operator = ([const] &&); ❖ Vector a(1, 2), b(0, -1); a = a + b;// move-assignment 30
  15. Overloading operators ❖ Like functions, operators can be overloaded ❖ Vector a, b, c; c.x = a.x + b.x; c.y = a.y + b.y; c /= 2; ❖ Allow the definition of specific operations on complex data structures 32
  16. Overloading operators ❖ Example: Vector.h class Vector { float x, y; Vector.cpp public: Vector() { x = y = 0; } #include Vector(float x, float y); #include “Vector.h” Vector operator + (const Vector &a); Vector::Vector(float x, float y) { float getX() { return x; } this->x = x; float getY() { return y; } this->y = y; }; } test.cpp Vector Vector::operator + (const Vector &a) { Vector c; #include c.x = this->x + a.x; #include “Vector.h” c.y = this->y + a.y; int main(int narg, char argv) { return c; Vector a0(1.0f, 0), a1(0.5f, 1.0f); } a0 = a0 + a1; cout << “a0 + a1: (” << a0.getX() << “, ” << a0.getY() << “)\n”; return 0; } 34
  17. Pointer and dynamic allocation ❖ Like other structures, objects can be allocated at runtime ❖ Using new and delete operators ❖ cout > N; Student *sList = new Student[N]; if (sList != NULL) { for (int i = 0; i < N; i++) sList[i].setID(i + 1); delete [] sList; } 36
  18. Pointer and dynamic allocation ❖ C++ vs. Java ❖ Be a responsible person! ❖ Practice memory management methods 38
  19. Friendship and inheritance ❖ Friendship ❖ Friends are functions or classes declared with the friend keyword ❖ A non-member function can access private and protected members of class if it is declared as a friend of class. ❖ E.g.: class Student { . . . public: friend Student duplicateStudent(Student& a); }; 40
  20. Friendship and inheritance ❖ Inheritance ❖ Allow classes extend features from the base classes. ❖ The derived class inherits the members of the base class, on top of which it can add its own members. Triangle Square Rectangle Shape Polygon Parallelogram Rhombus Ellipse Circle 42
  21. Friendship and inheritance ❖ Example: Shape.h class Shape { int id; public: Polygon.h Shape() { id = 0; } #include “Shape.h” ~Shape(); class Polygon : public Shape { void draw(); int nVertex; }; Vector2D* pVertex; public: Ellipse.h Polygon(int n) : Shape(), nVertex(n) {} ~Polygon(); #include “Shape.h” void draw(); class Ellipse : public Shape { }; float theta; Vector2D center, len; public: Ellipse(); ~Ellipse(); void draw(); }; 44
  22. Friendship and inheritance ❖ Inheritance ❖ protected: all public members of the base class are inherited as protected in the derived class. ❖ private: all base class members are inherited as private in the derived class. 46
  23. Discussion ❖ C vs C++: ❖ Class vs. struct, union ❖ C++ has blurred the concept of struct in C. ❖ Structured programming vs OOP ❖ Polymorphism ❖ Templates 48
  24. Quiz & homework 50