Page 254 - Computer_Science_F5
P. 254

Example of constant declaration:
                         const int averageMarks = 50;

                         const double bankRate = 0.15;

              The following Program Example 4.11 demonstrates an example of a C++ program          Chapter Four: Object oriented programming with C++
          FOR ONLINE READING ONLY
              that uses constant. The program computes the area of the circle, and it displays the
              output on the screen. The output is shown in Figure 4.25.

                      Program Example 4.11:

               A C++ program that uses constant



                   #include<iostream>

                   using namespace std;

                   int main()
                   {

                          const double PI = 3.142;                   //declare PI as a constant

                          double radius, area;
                          cout<<”Enter a radius: “;

                          cin>>radius;

                          area = PI*radius*radius;             //compute the area

                          cout<<”The area of the circle is “<<area<<endl; //display the output
                          return 0;

                   }


                Output:











                                        Figure 4.25: Output of the program




                                                    245
               Student’s Book  Form Five



     Computer Science Form 5.indd   245                                                     23/07/2024   12:33
   249   250   251   252   253   254   255   256   257   258   259