Page 170 - C++
P. 170

Note: ‐ Assume all the required header files are already being included in the code.                 (2)

        ‐ The function random(n) generates an integer between 0 and n‐1

        void main()

        {

        clrscr();

        randomize();

        int PICKER;

        PICKER=1+random(3);

        char COLOR[][5]={”BLUE”,”PINK”,”GREEN”,”RED”};

        for(int I=0;I<=PICKER; I++)

        {

               for(int J=0; J<=I;J++)
                       cout<<COLOR[J];


               cout<<endl;
        }


        getch();
        }



        I)                         (II)                       (III)                      (IV)
        PINK                       BLUE                       GREEN                      BLUE
        PINKGREEN                  BLUEPINK                   GREENRED                   BLUEPINK
        PINKGREENRED               BLUEPINKGREEN                                         BLUEPINKGREEN
                                   BLUEPINKGREENRED
        Ans. (II) will be the output. Minimum value is 0 and Maximum value is 2 that can be assigned to variable
        PICKER.

        Q2 (a) What is the difference between the constructor and normal function?                           (2)

        Ans.

        Constructor                                      Normal Function
        1. Constructor has same name as class name.      1. A normal function can have any legal name
                                                         but not class name.
        2. Constructor does not have any return type     2. A function can have any return type value.
        not even void.
        3. Constructor is automatically called.          3. A function is explicitly called.
        4. Constructor cannot be static.                 4. A Function can be static.
   165   166   167   168   169   170   171   172   173   174   175