Page 301 - Computer_Science_F5
P. 301

Computer Science  Applying string library             Implementation of key OOP
               Activity 4.9:
                                                         features in C++


                                              Classes in C++
             function in C++ program
          FOR ONLINE READING ONLY
            Write a C++ program to show
                                              constructing objects. They serve as a blueprint
            application of string library     In C++, classes serve as blueprints for
                                              by bundling together  data  and methods that
            functions. In your program        manipulate that data. Essentially, a class defines
            include the following functions:  a specific type and outlines the structure and
             (a) strlen( )                    behavior  of  objects  that  belong  to  that  type.

             (b)strcopy( )                    Visualise  it  as  a  template  that  specifies  the
             (c) strcat( )                    characteristics and capabilities an object will
             (d) strcmp( )                    possess.

             (e) strchr( )                    To illustrate,  consider a real-life  example:  a
             (f) strstr( ).                   car represents an object.  This car possesses
                                              attributes  like  weight  and  color, as well  as
                                              methods  such as drive  and brake. Attributes
                 Exercise 4.16                and methods can be compared to variables and
                                              functions that are associated with a class. They
             1.  Differentiate  between       are commonly known as “class members”.
                string and array.
             2.  Why do we use the null       A class, as a custom data type can be utilised in
                character ‘/0’ in string      a program. It acts as a constructor for objects
                initialisation?               or a “blueprint” that outlines how to create
             3.  Write a program that         objects. The syntax for class in C++ is like this:
                displays the statement             class ClassName {
                “C++ programming                   public:
                language”.                             // public members

             4.  Can we declare an array to        private:
                hold the mixture of both               // private members
                and doubles and ints?
                Explain.                           protected:
             5.  Explain what will happen              // protected members
                if you attempt to access an        }; E
                element of an array using a
                negative index?               Program Example 4.40 shows a sample code
                                              for structure of a class


                                                 292
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   292                                                     23/07/2024   12:34
   296   297   298   299   300   301   302   303   304   305   306