Hướng dẫn c++ oop exercises and solutions pdf - c ++ oop bài tập và giải pháp pdf
Ngày đăng:
19/10/2022
Trả lời:
0
Lượt xem:
102
CSE 428: Giải pháp bài tập về lập trình hướng đối tượng và C ++SuperScript "(d)" là viết tắt của "khó khăn". Các bài tập tương tự như các bài tập được đánh dấu bằng "(d)" có thể xuất hiện trong các kỳ thi ứng cử, nhưng không phải trong các bài kiểm tra tiêu chuẩn của CSE 428.(d)" stands for "difficult". Exercises similar to those marked with "(d)" might appear in candidacy exams, but not in the standard exams of CSE 428. Show
Dưới đây là kết quả của việc biên dịch và thực thi bằng G ++ // FILE Exercise_1.C #include < iostream.h > class C { protected: int x; public: void f(){ cout << "executes C::f()" << '\n'; } virtual void g(){ cout << "executes C::g()" << '\n'; } }; class C1: public C { protected: int x1; public: void h(C *); }; class C2: public C1 { public: int x2; }; class C3: public C { protected: int x3; public: void f(){ cout << "executes C3::f()" << '\n'; } void g(){ cout << "executes C3::g()" << '\n'; } }; void C1::h(C *obj){ C2 *obj2 = new C2; C3 *obj3 = new C3; //obj->x = 0; obj2->x = 2; //obj3->x = 3; cout << "method obj->f() from C1::h: "; obj->f(); cout << "method obj->g() from C1::h: "; obj->g(); cout << "method obj3->f() from C1::h: "; obj3->f(); } void main(){ C1 obj1; //obj1.x = 0; cout << "method obj1.f() from main: "; obj1.f(); //obj1.x1 = 1; //obj1.x2 = 2; C3 o3; obj1.h(&o3); } /* Errors (before commenting the lines producing errors) % g++ Exercise_1.C Exercise_1.C: In method `void C1::h(class C *)': Exercise_1.C:32: member `x' is a protected member of class `C' Exercise_1.C:34: member `x' is protected Exercise_1.C: In function `int main(...)': Exercise_1.C:41: member `x' is protected Exercise_1.C:43: member `x1' is a protected member of class `C1' Exercise_1.C:44: `class C1' has no member named `x2' */ /* Result of execution % a.out method obj1.f() from main: executes C::f() method obj->f() from C1::h: executes C::f() method obj->g() from C1::h: executes C3::g() method obj3->f() from C1::h: executes C3::f() */ template < class T > class cell { protected: T info; public: void set(T x){ info = x; } T get() { return info; } };Xác định lớp con colored_cell bằng cách mở rộng ô lớp với:
Dung dịchtemplate < class T > class colored_cell: public cell< T >{ protected: char color; public: void set_color(char c){ color = c; } char get_color() { return color; } T get() { if (color != 'w') return info; else return 0; } }; C / \ / \ C1 C3 | | C2 Dung dịchPhân cấp lớp được biểu thị bằng biểu đồ sau:C / \ / \ C1 C3 | | C2 Dung dịchPhân cấp lớp được biểu thị bằng biểu đồ sau:C / \ / \ C1 C3 | | C2
Dung dịch
|