Đề bài : Cho lớp trừu tượng Message , có một phương thức ảo/ trừu tượng là void Print() .
class Message {
public:
virtual void Print() const = 0;
};
Hãy viết một lớp HelloMessage thừa kế lớp Message , ghi đè phương thức void Print() để in ra dòng chữ “Hello, World!”.
Code của em :
class HelloMessage: virtual public Message {
public:
void Print(){
cout<<"Hello, World!";
}
};
Lỗi :
prog.cpp: In function 'int main()':
prog.cpp:26:17: error: cannot allocate an object of abstract type 'HelloMessage'
pMessage = new HelloMessage;
^
prog.cpp:16:7: note: because the following virtual functions are pure within 'HelloMessage':
class HelloMessage: virtual public Message {
^
prog.cpp:14:15: note: virtual void Message::Print() const
virtual void Print() const = 0;
Ai biết em sai gì chỉ em với ạ . Em tìm mấy lỗi này trên stackoverflow rồi mà đọc vẫn không hiểu ~~~
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?