Page 313 - Computer_Science_F5
P. 313

Computer Science  };                             1.  Read the following C++ program
                                                              Revision Exercise 4
                  // Derived class Rectangle
                  class Rectangle : public Shape {
                                                         and answer the question that follow:
                  public:
          FOR ONLINE READING ONLY
                      // Overriding the draw() method
                      void draw() override {           #include<iostream>
                                                       using namespace std;
                          cout << “Drawing a rectangle”   int main()
                     << endl;                          {
                      }                                int num, temp, sum;
                                                       cout<<”Enter a positive integer: “;
                  };                                   cin>>num;
                  // Function to draw any shape        cout<<endl;
                  void drawShape(Shape* shape) {       temp=num;
                                                       sum=0;
                      shape->draw();                   do
                  }                                    {

                  int main() {                         sum=sum+num%10;
                                                       num=num/10;
                      // Creating objects of different   }
                     shapes                            while(num>0)
                      Circle circle;                   cout<<”sum is: “<<sum<<endl;

                      Rectangle rectangle;             system(“PAUSE”);
                      // Polymorphic behavior          return 0;
                                                       }
                      drawShape(&circle);
                      drawShape(&rectangle);              (a) What are the functions of the

                      return 0;                               line #include<iostream>and
                  }                                           system(“PAUSE”);
                                                          (b) Write an error made from this
                                                              program.

                                                      2.  Why do we need to state data type
                Chapter Summary
                                                         when declaring variables in a C++
                                                         program?
            Reflect on what you have learnt from      3.  How will you guarantee and ensure

            this chapter, summarise and keep it in       that a variable’s value can never be
            your portfolio.                              changed after its initialisation?


                                                 304
                                                                for Advanced Secondary Schools



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