- Determine any syntax errors in the class declarations. List the error and a reasonable correction next to the statement.
-
class CL
{
private
int a:
float x=4.5;
public
void CL(int a, float b=0.0);
PrintCL(void);
}
-
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;
}
-
class CL
{
CL(int x):data(x)
{}
private:
int data;
};
- The directives private and public can occur only once in the declaration of a class.
- True
- False
- A class does not absolutely need to have a constructor.
- True
- False
- A default constructor must have a parameter void.
- True
- False
- 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
};
- Give a declaration of the method Pay. Include the const directive where appropriate.
- Give a declaration of the method UpdateHours. Include the const directive where appropriate.
- Use an initializer to assign the int level to ovtlevel.
Payroll::Payroll(int level, int hours):_____
{<code>}
- Which code segment completes the constructor.
- {} //No additional code
-
regular=hours;
overtime=hours-regular;
-
regular=(hours>ovtlevel)?ovtlevel:hours;
overtime=regular_hours;
-
regular=(hours>ovtlevel)?ovtlevel:hours;
overtime=(hours>ovtlevel)?hours-ovtlevel:0;
- For each declaration give the initial values
Regular Overtime
------- --------
Payroll emp1(30, 15);
Payroll emp2(40);
Payroll emp3(40,60);
Payroll emp4;
- 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
- Declare an array of Payroll objects that have overtime levels of 35, 20 and 40 respectively.
- 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); _______
- 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?
- all declarations
- only 1 and 2
- only 1 and 3
- only 2 and 3.
- 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()?
- CLobj(3,5); obj.Sum() is ____
- CLobj(3); obj.Sum() is ----
- CLobj; obj.Sum() is -----
-
Clr(2,5);
CLobj(r.Sum()); obj.Sum() is ----
- 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.
- The output at A is
- 2
- 3
- 8
- 4
- The output at B is
- 3
- 4
- 2
- 0