Page 114 - C++
P. 114

Make- of type string
        Model- of type string
        Price- of type long int
        Rating- of type char
        Public Members:
        Function Read_Data to read an object of HandSet type.
        Function Display() to display the details of an object of HandSet type.
        Function RetPrice() to return the value of Price of an object of HandSet type.
        Ans:  class handset
        {      char m_type[30],mode_type[30];
               long int price;
               char rating;
               public:
               void read_data()
               {gets(m_type);gets(mode_type);
                cin>>price;
                 cin.get(rating);
               }
               void display()
               {       cout<<m_type<<mode_type<<price<<rating;
               }
               long int retprice()
               {       return price;
               }
        };

        2 (d) Consider the following class counter:
               class counter
               { protected :
               unsigned int count;
               public :
               counter()
               { count = 0; }
               void inc_count()
               { count++; }
               int get_count()
               { return count; }
               };
        Write code in C++ to publically derive another class new_counter from class counter. Class new_counter
        should have the following additional function members in the public visibility mode:
        (i) A parameterized constructor to initialize the value of count to the value of parameter.
        (ii) dec_count() to decrease the value of data member count by 1.
        (iii) Reset() to set the value of data member count to 0.
        Ans:
        class new_counter:   public counter                //class new_counter inherited from counter in public
        mode
        {      public:
               new_counter(unsigned int a)
                       {count=a;}
               void dec_count()
                       {count--;}
               void reset()
                       {count=0;}
        };
         Extra description: when inherited in public mode all public of base comes in public of derived and all
        protected of base comes in protected of derived. So count member which is protected in class counter comes
   109   110   111   112   113   114   115   116   117   118   119