Page 312 - Computer_Science_F5
P. 312

class SavingsAccount that adds           and triangles, where each shape
                   an attribute for interest rate and a     has a calculateArea() method?
                   method to apply interest. Explain        Discuss how you would design the
                   how abstraction helps in managing        classes and utilize polymorphism       Chapter Four: Object oriented programming with C++
                   the complexity of the system.            to calculate the area of each shape
          FOR ONLINE READING ONLY
               2.  Design a C++ program for a library       without explicitly checking the type
                   management system. Create a base         of the object.
                   class LibraryItem with attributes    6.  This  code demonstrates  how
                   like title, author, and ISBN. Include    inheritance, encapsulation,
                   methods to get the details of the        polymorphism, and abstraction can
                   item. Then, derive classes Book and      be used together to create flexible
                   Magazine from LibraryItem, adding        and reusable code in C++. Study the
                   specific attributes and methods for      code carefully and extract block of
                   each. How does abstraction make          codes that represents inheritance,
                   the system more manageable and           encapsulation, polymorphism, and
                   user-friendly?                           abstraction. Evaluate each block of
               3.  In a banking system, how would           code in each pillar:
                   you use inheritance to model               #include <iostream>
                   different types of accounts such as        using namespace std;
                   SavingsAccount, CheckingAccount,
                   and LoanAccount, all inheriting            // Base class
                   from a base class BankAccount?             class Shape {
                   Discuss how you would structure the        public:
                   classes to maximize code reusability           // Virtual function
                   and minimize redundancy.                       virtual void draw() {
               4.  Consider a class representing a                    cout << “Drawing a shape”
                   bank account. How would you                   << endl;
                   use encapsulation to ensure that               }
                   the account balance can only be            };
                   modified through specified methods,
                   such as deposit() and withdraw(),          // Derived class Circle
                   while preventing direct access to          class Circle : public Shape {
                   the balance variable from outside          public:
                   the class?                                     // Overriding the draw() method

               5.  How would you use polymorphism                 void draw() override {
                   to implement a system for                          cout << “Drawing a circle”
                   processing different types of                 << endl;
                   shapes, such as circles, rectangles,           }



                                                    303
               Student’s Book  Form Five



     Computer Science Form 5.indd   303                                                     23/07/2024   12:34
   307   308   309   310   311   312   313   314   315   316   317