[Buổi 28][Kế thừa][Lab] Bài 2: Constructor Chain


LÀM BÀI

Points: 100
Time limit: 1.0s
Memory limit: 256M

Author:
Problem types
Allowed languages
C++

Constructor Chain

Bối cảnh

Khi tạo object derived, phần base phải được khởi tạo trước.

Hãy quan sát constructor chain bằng output marker.

Yêu cầu

  1. Base constructor in BASE x.
  2. Derived constructor gọi base constructor bằng initializer list rồi in DERIVED y.
  3. Tạo một Derived từ hai token x,y.

Yêu cầu tổ chức code

Phải dùng initializer list để gọi constructor của base.

Online Judge chấm output. Với bài nhạy về cấu trúc OOP, giảng viên review thêm source code để xác nhận học viên luyện đúng kỹ năng.

Input

Một dòng x y.

Output

Hai dòng marker theo đúng thứ tự.

Ràng buộc

|x|,|y| ≤ 30.

Ví dụ 1

Input

A B

Output

BASE A
DERIVED B

Giải thích

Khi tạo object Derived, constructor của Base luôn chạy trước. Vì thế input A B tạo output BASE A trước rồi mới DERIVED B. Thứ tự này phản ánh đúng constructor chain: phần base của object phải được khởi tạo hoàn chỉnh trước phần riêng của derived.

Ví dụ 2

Input

Person Student

Output

BASE Person
DERIVED Student

Giải thích

Tương tự, Person Student khiến constructor Base nhận Person và in trước. Sau đó constructor Derived mới xử lý Student. Kết quả hai dòng BASE Person rồi DERIVED Student kiểm tra đúng thứ tự khởi tạo inheritance.

Thông tin học tập

  • Module: M08
  • Buổi: B28
  • Loại bài: LAB
  • Độ khó: Medium
  • Concepts: inheritance, constructors, constructor chain, initialization
  • Giới hạn kiến thức: B01-B28
  • Time limit: 1 second(s)
  • Memory limit: 256 MB
  • Point: 100

Comments

There are no comments at the moment.

Zalo