Yêu cầu:
Xây dựng lớp cơ sở CPerson với các thuộc tính:
+ char *name : tên
+ int age: tuổi
+ CPerson(): hàm tạo không đối số
+ CPerson(char *str_name, int age): hàm tạo đối số
+ ~CPerson(): hàm hủy
+void show_person_info(): hàm hiển thị thông tin lớp CPerson
Lớp dẫn xuất CStaff() kết thừa từ lớp CPerson với các thuộc tính:
+ char *code : mã nhân viên
+ CStaff() : CPerson(): hàm tạo không đối số
+ CStaff(char *name, int age, char *code) : CPerson(name, age): hàm tạo có đối số
+ ~CPerson(): hàm hủy
+void show_staff_info(): hàm hiển thị thông tin của lớp CStaff
//************************************************************ //* Author: VNCODING //* History //* 2014/12/03 first create VNCODING //*************************************************************/ #include <iostream> #include <string.h> using namespace std; // derived classes #define SIZE 3 class CPerson { private: char *name; int age; public: CPerson() { name = NULL; age = 0; } CPerson(char *str_name, int age) { this->name = strdup(str_name); this->age = age; } ~CPerson() { if(name != NULL) delete name; } void show_person_info() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; class CStaff : public CPerson { private: char *code; public: CStaff() : CPerson() { code = NULL; } CStaff(char *name, int age, char *code) : CPerson(name, age) { this->code = strdup(code); } ~CStaff() { if(code != NULL) delete code; } void show_staff_info() { cout << "Staff information" << endl; CPerson::show_person_info(); cout << "Code: " << code << endl; } }; void main() { CStaff *obj_staff; obj_staff = new CStaff("Vu Hong Viet", 27, "20073476"); obj_staff->show_staff_info(); delete obj_staff; system("pause"); }
Kết quả:
Leave a Reply
You must be logged in to post a comment.