Page 303 - Computer_Science_F5
P. 303

Computer Science  object name. To access the attributes of the class (miche and amountOfWaterBucket),
           you can use the dot syntax (.) on the object as shown in an example.


                    Program Example 4.41:
          FOR ONLINE READING ONLY
            Creating object in C++


                  #include <iostream>
                  #include <string>
                  using namespace std;
                  class Mmea {       // The class
                    public:             // Access specifier
                         int miche;        // Attribute (int variable)
                         int amountOfWaterBucket;  // Attribute (int variable)
                  };
                  int main() {
                       Mmea yourObj;  // Create an object of class Mmea
                       // These codes will access attributes and set values
                       yourObj.miche = 180;
                       yourObj.amountOfWaterBucket =  5;
                      // These codes will print attribute values
                      cout << yourObj.miche  << “\n”;
                      cout << yourObj.amountOfWaterBucket ;
                    return 0;
                  }





                                                      3.  Judge the following codes:
                 Exercise 4.17
                                                             class Animal {
             1.  Create a Book class that have                   int y;
                attributes called author, title,  and        };
                ISBN. Show methods that will                 int main()
                display book details.                        {
             2.  Create a Car class with attributes            Animal h;
                called make, model,  year,  and                cout << h.y;
                speed. Include methods such as                 return 0;
                accelerate and brake.                        }


                                                 294
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   294                                                     23/07/2024   12:34
   298   299   300   301   302   303   304   305   306   307   308