QUIZ 3
  1. Determine any syntax errors in the class declarations. List the error and a reasonable correction next to the statement.
    1. class CL
      {
      	private
      		int a:
      		float x=4.5;
      	public
      		void CL(int a, float b=0.0);
      		PrintCL(void);
      }
      
    2. 
      
      class CL
      {
      	private 
      		int data;
      	public
      		CL(int n=5);
      		void Update(int m)const;
      };
      CL;CL(int n=5):n(data)
      {}
      
      void CL;Update(int m)const
      {
      	data=m;
      }
      
    3. class CL
      {
      		CL(int x):data(x)
      		{}
      	private:
      		int data;
      };
      
  2. The directives private and public can occur only once in the declaration of a class.
    1. True
    2. False
  3. A class does not absolutely need to have a constructor.
    1. True
    2. False
  4. A default constructor must have a parameter void.
    1. True
    2. False
  5. For the Payroll class answer a series of questions. The class has a constructor that is passed the number of hours that the employee works in the week and the hour level at which overtime begins for the employee. The constructor sets data members regular and overtime that maintains the number of regular hours worked and the number of overtime hours worked. The constructor uses a default of 40 for the overtime level and 0 for hours worked. The method UpdateHours takes a parameter hours and adds it to the regular and overtime hours of the employee. For instance after working 35 regular hours UpdateHours(10) would add ten hours to the employee total. Assuming an overtime level at 40, regular hours would be set at 40 and overtime hours to 5. The constant method Pay passes a method rate (dollar per hour) parameter and returns the employee salary based on time and a half for overtime. Hence at $10 per hour a worker with 40 regular hours and 5 hours overtime would earn Pay is: 40*10 + 5*(1.5*10)=400+5*15=$475.
    class Payroll
    {
    	private:
    		int regular;	//number of regular hours worked
    		int overtime;	//number of overtime hours accumulated.
    		int ovtlevel;	//level at which overtime begins
    	public
    		Payroll(int level=40, int hours=0);
    		_____ declaration of Pay	//A
    		_____ declaration of UpdateHours //B
    };
    
    1. Give a declaration of the method Pay. Include the const directive where appropriate.
    2. Give a declaration of the method UpdateHours. Include the const directive where appropriate.
    3. Use an initializer to assign the int level to ovtlevel.
      Payroll::Payroll(int level, int hours):_____
      {<code>}
      
    4. Which code segment completes the constructor.
      1. {} //No additional code
      2. regular=hours;
        overtime=hours-regular;
        
      3. regular=(hours>ovtlevel)?ovtlevel:hours;
        overtime=regular_hours;
        
      4. regular=(hours>ovtlevel)?ovtlevel:hours;
        overtime=(hours>ovtlevel)?hours-ovtlevel:0;
        
    5. For each declaration give the initial values
      				Regular		Overtime
      				-------		--------
      
      Payroll emp1(30, 15);	
      Payroll emp2(40);
      Payroll emp3(40,60);
      Payroll emp4;
      
    6. Assume the declaration for Boss. Payroll Boss(40, 20); Write C++ statements that access the object and use one of the methods.
      • ______ //update the hours for Boss by 7 hours.
      • _____ //print the salary assuming Boss earns $20 per hour
    7. Declare an array of Payroll objects that have overtime levels of 35, 20 and 40 respectively.
    8. For each declaration give the output for the Pay method.
      Payroll emp1(30,30);
      	cout<< emp1.Pay(10);	_______
      Payroll emp2(40,50);
      	cout <<emp2.Pay(8);	_______
      Payroll emp3(25);
      	cout<<emp3.Pay(10);	_______
      
  6. Consider the two class declarations.
    class One				class Two
    {					{
    	private:				private:
    		int value;				float fvalue;
    	public:					public:
    		One(int v=0);				Two(float v);
    		int GetValue(void);			float GetValue(void);
    };					};
    and the array declarations
    One A[20];		//declaration 1
    Two B[2]={Two(1.0),Two(2.0)}	//declaration 2
    Two C[20];	//declaration 3
    
    Which declarations are valid?
    1. all declarations
    2. only 1 and 2
    3. only 1 and 3
    4. only 2 and 3.
  7. Consider the class declaration
    class CL
    {	
    	private:
    		int x,y;
    	public:
    		CL(int m=0, int n=5):x(m),y(n)
    		{}
    
    		int Sum(void)
    		{
    			return x+y;
    		}
    };
    
    For each declaration, give the value for obj.Sum()?
    1. CLobj(3,5); obj.Sum() is ____
    2. CLobj(3); obj.Sum() is ----
    3. CLobj; obj.Sum() is -----
    4. 
      Clr(2,5); 
      CLobj(r.Sum());	 obj.Sum() is ----
      
  8. Read the program and choose the output from among the choices.
    #include <iostream.h>
    class CL
    {
    	private:
    		int ma;
    		int n;
    	public:
    		CL(void);
    		void Cmp(int elem);
    		int Fetch(void);
    };
    
    CL::CL(void):ma(0),n(0)
    {}
    
    void CL::Cmp(int elem)
    {
    	n++;
    	if(elem>ma)
    		ma=elem;
    }
    
    int CL::Fetch(void)
    {
    	return n>0?ma/n:0;
    }
    
    void main(void)
    {
    	int q;
    	CL s, t;
    	
    	for(int i=0;i<5;i++)
    	{
    		cin >>q;
    		S.Cmp(q);
    	}
    	cout << s.Fetch() << endl;	//A
    	cout << t.Fetch() << endl;	//B
    }
    
    Input is 3, 8, 15, 7, 10.
    1. The output at A is
      1. 2
      2. 3
      3. 8
      4. 4
    2. The output at B is
      1. 3
      2. 4
      3. 2
      4. 0