Thông tin tài liệu
C++ language Từ Trung Hiếu overview the mixed language the superset of C the subset of Visual C++ principles C program elements are functions and variables C++ program elements are objects comments line comment block comment operators bitwise: & ^ | ~ >> >= < [] control statements condition: if, switch loop: for, while, while jump: goto, return, break, continue block: {} common functions string: strcpy, strcat, strlen, strcmp math: cos, sin, pow, sqrt, exp, log, rand file: fopen, fclose, feof, fseek, ftell comon libraries string : both C and C++ memory : both C and C++ stdlib : both C and C++ stdio : both C and C++ conio : both C and C++ math: both C and C++ iostream: native to C++ native to C++ the pointer this the type reference the concept class the keyword friend levels of hidding Object Oriented Programming with C++ Từ Trung Hiếu cdp4ex - inheritance a[0] = new Circle(1, 1, 10); a[1] = new Circle(20, 30, 10); a[2] = new Rectangle(10, 30, 30, 20); a[3] = new Rectangle(10, 100, 30, 40); a[4] = new Circle(10, 100, 30); for(int i=0; iprint(); } GraphicDevice, Printer, Monitor Viết lớp GraphicDevice · hàm Line(x1, y1, x2, y2) không làm · hàm Rectangle(x1, y1, x2, y2) khơng làm · hàm Ellipse(x1, y1, x2, y2) khơng làm · hàm SetPixel(x, y) khơng làm Viết lớp Printer · thừa kế public từ lớp GraphicDevice · hàm Line(x1, y1, x2, y2) in dòng "ve duong thang tren may in" · hàm Rectangle(x1, y1, x2, y2) in dòng "ve hcn tren may in" · hàm Ellipse(x1, y1, x2, y2) in dòng "ve elip tren may in" · hàm SetPixel(x, y) in dòng "cham diem tren may in" Viết lớp Monitor · thừa kế public từ lớp GraphicDevice · hàm Line(x1, y1, x2, y2) in dòng "ve duong thang tren man hinh" · hàm Rectangle(x1, y1, x2, y2) in dòng "ve hcn tren man hinh" · hàm Ellipse(x1, y1, x2, y2) in dòng "ve elip tren man hinh" · hàm SetPixel(x, y) in dòng "cham diem tren man hinh" Trong hàm main void main() { Printer p; Monitor m; m.Line(1, 1, 10, 10); p.Line(1, 1, 10, 10); } Image, ImageEx Viết lớp image có · thành phần width, height kiểu long mức protected · mảng item[100][100] số long mức protected · hàm SetPixel(x, y, color) đặt giá trị cho item[y][x] = color · hàm GetPixel(x, y) trả giá trị item[y][x] · hàm SetSize(w, h) đặt giá trị width=w height=h · hàm print in giá trị màu ma trận Viết lớp imageEx · thừa kế public từ image · hàm Line(x1, y1, x2, y2) vẽ đường thẳng dựa SetPixel(x, y, color) file:///E|/Baigiang/moodledata/lap%20trinh%20C%20Eng/bt/cdp4ex%20-%20inheritance.htm[19/03/2013 2:41:41 PM] cdp4ex - inheritance Trong hàm main, void main() { imageEx img; img.SetSize(10, 10); img.Line(0, 0, 9, 9); img.Line(9, 0, 0, 0); img.print(); } Person, Student, Employee Viết lớp Person gồm · thành phần name kiểu char[128] mức protected · thành phần yearbirth kiểu int mức protected · thành phần email kiểu char[30] mức protected · hàm print() in giá trị name, yearbirth, email · hàm tạo Person(n, yb, em) khởi tạo giá trị cho name=n, yearbirth=yb, email=em Viết lớp Student · thừa kế public từ lớp Person · thành phần fsem ssem kiểu double mức protected · hàm print() in tất thành phần fsem, ssem thành phần thừa kế · hàm tạo Student(n, yb, em, fs, ss) Viết lớp Employee · thừa kế public từ lớp Person · thành phần salary kiểu double mức protected · hàm print() in thành phần thừa kế salary · hàm tạo Employee(n, yb, em, sal) Trong hàm main void main() { Student st; Employee em; st = Student("Nguyen Van A", 1983, 6.0, 7.3); em = Employee("Tran Van B", 1970, 5000000); st.print(); em.print(); st.Person::print(); em.Person::print(); } File, TextFile, BinaryFile file:///E|/Baigiang/moodledata/lap%20trinh%20C%20Eng/bt/cdp4ex%20-%20inheritance.htm[19/03/2013 2:41:41 PM] cdp4ex - inheritance Viết lớp File · thành phần file kiểu FILE* mức protected · hàm OpenFile(char* fname, char* fmode) mở tập tin fname với fmode ghi vào file · hàm Close() đóng tập tin file Viết lớp TextFile · thừa kế public từ File · có Write(long x) sử dụng fprintf(file, "%ld\n", x); · có Write(double x) sử dụng fprintf(file, "%lf\n", x); Viết lớp BinaryFile · thừa kế public từ File · có Write(long x) sử dụng fwrite(&x, sizeof(x), 1, file); · có Write(double x) sử dụng fwrite(&x, sizeof(x), 1, file); Trong hàm main void main() { TextFile t; BinaryFile b; t.OpenFile("dulieu.txt", "wt"); t.Write(1234L); t.Write(1234.56); t.Close(); b.OpenFile("dulieu.txt", "wb"); b.Write(1234L); b.Write(1234.56); b.Close(); } file:///E|/Baigiang/moodledata/lap%20trinh%20C%20Eng/bt/cdp4ex%20-%20inheritance.htm[19/03/2013 2:41:41 PM] polymorphism Từ Trung Hiếu contents about polymorphism rules of message dispatching rules of virtual declaration heterogeneous array abstract functions heterogeneous array problem: a general interface for all derived classes solution: virtual function virtual function a function whose meaning / semantics depends on the constructor function call depends on the initiation class, not the declaration class eg of virtual function the class of initiation, not the class of declaration, determines the message v1, v2 belong to vector v1 = new vector(); v2 = new vector3d(); v1->print(); //vector::print v2->print(); //vector3d::print construction / destruction constructor CANNOT be virtual -> virtual function called in constructor destructor SHOULD be virtual normal function using a virtual function can be virtual heterogeneous array Declaration: array of pointer Using: scanning thru the array Destruction: like other operation Creation: hardest operation abstract functions writing data to file, printer, monitor, memory is known as writing data to output device if those devices have the common interface, we need to design only one function WriteData(OutDev dev) static function acting much the same as standalone function, but bound to a class can not use the pointer this can not access data member CAN access static data member invocation with class name or object name Class::StaticFunction Object.StaticFunction Object->StaticFunction static data member declared with static keyword static int x; shared among instants of the class must be defined cdp5ex - polymorphism cdp5ex - polymorphism (Từ Trung Hiếu, 10-may-2006) Vector, Vector3d Viết lớp Vector có thành phần sau · thành phần x, y kiểu double, khai báo mức protected · hàm tạo Vector(a=0, b=0) đặt giá trị cho x=a y=b · hàm ảo print() in giá trị x y Viết lớp Vector3d · thừa kế từ lớp Vector theo mức thừa kế public · có thêm thành phần z kiểu double, khai báo mức protected · hàm tạo Vector3d(a=0, b=0, c=0) gọi hàm tạo Vector(a, b) gán z=c · hàm ảo print() in giá trị x, y, z Trong hàm main void main() { Vector* v[10]; int i; for(i=0; i
Ngày đăng: 21/03/2021, 18:24
Xem thêm: Giới thiệu môn học ngôn ngữ lập trình c