- For each function call, give the version of f that will be called. Select from the choices.
- Form 1
- Form 2
- ambiguous
double f(double x) //form 1
int f(int x) //form 2
-
void main(void)
{
short A;
short B;
B=f(A);
}
Selection_____
void main(void)
{
float A;
int B;
B=f(A);
}
Selection_____
-
void main(void)
{
int B;
B=f('Q');
}
Selection_____
void main(void)
{
long A;
int B;
B=f(A);
}
Selection_____
- Trace the program and determine the output.
#include<iostream.h>
char Median(char c[], int n)
{
return c[n/2];
}
float Median(float f[], int n)
{
int m=n/2;
return n%2!=0?f[m]:(f[m-1]+f[m])/2.0;
}
int Median(int i[], int n)
{
int m=n/2;
return n%2!=0?i[m]:(i[m-1]+i[m])/2;
}
void main(void)
{
int a[]={4, 9, 13, 21, 30, 33, 40};
cout << Median(a, 7)<<endl; Output_____
float f[]={2.0,3.5, 5.5, 6.0};
cout<< Median(f,4)<< Output_____
char c[]={`a',`e',`f',`h',`k',`t'};
cout<<Median(c,6)<<endl; Output_____
char c2[]={0,0,0,`a',0, 0,0,`e',0,0,0,`g'};
cout<<char(Median((int *)c2,3))<< endl; Output____
- Trace the program and give the output for the designated statements. Then answer parts A and B.
#include<iostream.h>
class OpOver
{
private:
int one, two;
public:
OpOver(int x=0, int y=0);
OpOver operator-(void); //unary minus
OpOver operator +(const OpOver &y);
friend OpOver operator *(const int c, const Opover &x);
friend ostream & operator<<(ostream& ostr, const OpOver &x);
};
OpOver::OpOver(int x, int y):one(x), two(y)
{}
OpOver OpOver::operator-(void)
{
return OpOver(-two, -one);
}
OpOver OpOver::operator+(const OpOver &y)
{
return OpOver(one+y.one, two-y.two);
}
Opover operator *(const int c, const OpOver &x)
{
return Opover(c*x.two,c*x.one);
}
ostream& operator<<(ostream& ostr, const OpOver &x)
{
os<<`('<<x.one<<","<<x.two<<`)';
return ostr;
}
void main(void)
{
OpOver x(1,2),y(3,4),z;
z=x+y;
cout<<z<<endl; Output___
cout<<-x<<endl; Output___
cout<<3*y<<endl; Output___
}
- In the class OpOver, the operator * could also be overloaded as a member function using the declaration
OpOver operator *(const OpOver &x);
- true
- false
- In a method what is the meaning of the designation friend ______
- In parts A and B, write the code segment that overloads operators for the record type Rec.
struct Rec
{
long key;
int data;
};
- Overload the relational operator "<=". The comparison is to be done using the key field.
- Overload the output operator"<<". A record should be printed with the format Key:<key> Data:<data><newline>
- Use the rational class.
- Trace the program and give the output at the designated places.
#include<iostream.h>
#include "rational.h"
void main(void)
{
rational R1(3,-6),R2,R3;
cout<<"R1= "R1; //output is _____
R1.reduce();
cout <<"R1= "R1; //output is___
R2=12.5;
R2.Reduce();
cout<<"R2= "R2; //output is____
cout <<"Enter a rational number R2: "
cin>>R2;
//how do you input the rational number corresponding to 1.4___
R1=Rational(5,2);
R3=R1-R2; //output is _____
cout<<R3;
if(R1==1.25)
cout<<"valid output";
//is the previous conditional statement valid?____
}
- Identify the Rational methods that performs each of the following conversions.
- Convert 4.5 to 9/2 //method______
- Convert -7 to -7/1 //method_____
- Convert 9/-4 to -9/4 //method_____
- Convert -3/4 to -0.75 //method_____
- Read the program and answer parts A to E.
#include<iostream.h>
#include<iomanip.h>
//maintain time of day
class Time
{
private:
int hour;
int minute;
public:
(1) Time(int h=0< int m=0);
(2) Time(double h);
(3) Time operator+ (int m);
(4) operator double(void);
(5) friend ostream& operator<<(ostream& ostr, const Time& t);
};
Time::Time(int h, int m):hour(h), minute(m)
{}
Time::Time(double h)
{
hour=int(h);
//round to the nearest integer value
minute=int((h-hour)*60+0.5);
}
Time Time::operator +(int m)
{
Time tmp=Time(hour, minute);
tmp.minute+=m; //raw minutes total
tmp.hour+=tmp.minutes/60; //total hours
tmp.minute %=60 //minutes left after hours are computed
tmp.hour %=24; //maintain 24 hour time
return tmp;
}
Time::operator double(void)
{
return hour+minute/60.0;
}
ostream& operator<<(ostream& ostr, const Time& t)
{
int temphour=t.hour;
cout.fill(`0'); //when justify, fill with `0'
temphour=temphour%12; //convert to standard time
if(temphour==0)
temphour=12; //hour 0 is read as 12
cout<<temphour<<":"<<setw(2)<<t.minute;
if(t.hour<12)
cout<<"A.M.";
else
cout<<"P.M>";
cout.fill(` '); //reset fill character to blank
return ostr;
}
void main()
{
Time getUp=6.75, lunch(12,30), gotoBed=2;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout<<"Get up at"<<getUp <<endl; //Output 1___
cout<<"Class starts at "<<getUp+15<<endl;//Output 2___
cout<<"Lunch at "lunch<<endl; //Output 3___
cout<<"Lunch starts at "<<double(lunch)-double(getUp)
<<"hours after getting up"<<endl; //Output 4___
cout<< " go to bed at " <<gotoBed<<endl; //Output 5___
- Which declaration specifies conversion from integer to Time (see the public methods)?
- (1)
- (2)
- (3)
- (4)
- Which declaration specifies conversion from floating point to time
- (1)
- (2)
- (3)
- (4)
- Which declaration specifies conversion from time to floating point?
- (1)
- (2)
- (3)
- (4)
- The operator "<<" is a friend because
- It must be implemented as a member function
- It cannot be overloaded
- It needs access to private variables over time.
- its priority would otherwise conflict with that of "+".
- Fill in the missing lines of the Program run.
Output 1:_________
OUTPUT 2: _______
Output 3:______
Output 4:_____
Output 5:_______
- The class M performs actions on measurement in feet and inches.
#include <iostream.h>
class M
{
private:
int f;
double i;
public:
M(int x, int y)
{
f=x+y/12;
i=y%12;
}
M(void)
{
f=i=0;
}
M(double x)
{
f=int(x);
i=int((x-f)*12.0);
}
M operator+(M x)
{
return(M(f+x,f,i+x.i));
}
operator double(void)
{
return(f+i/12.0);
}
void print(void)
{
cout <<f<<" "<<i<<endl;
}
};
void main(void)
{
M A(3.6), B(2,10), C(3,6),D;
double x;
C.print();
D=A+B;
D.print();
x=A;
cout<<x<<endl;
}
- List all class members that are converters?_____
- Which choices specifies the output of the program
- 3 7 , 6 4, 3.5
- 3 6, 46, 2.5
- 46, 36, 6.5
- 58, 210, 35
- Is the statement C=5.5 valid?_____
- Yes
- No
If so what is printed by C.print()?_____
- Suppose that CL is a class with structure
class CL
{
private:
<Data>
public:
CL(_);
.....
CL operator+(const CL& x);
.....
};
Assume A, B, and C are objects of class CL.
CL A, B;
The operation C=A+B is implemented as
- A.operator+(A,B)
- A.operator+(B)
- A+B.operator+()
- B.operator+(A)