Explain the difference between public data members and private data members. Give an example in the main function to show that one C++ statement is valid and the other is invalid (you can use Grade class in Problem 5.
2 What output would be produced by the following program?
#include
using std::cout; using std::endl;
int main()
{
int a[] = {3, 1, 0, 2, 4};
int b[] = {50, 40, 30, 20, 10};
for (int i=0; i<5; i++}
cout << b[ a[i] ] << endl;
return 0;
}
3 The following code has many errors. Please point out and correct them.
#include
using namespace std;
class Date
{
public
Date()
{
month = day = 0;
}
setDate(int m, int d)
{
month = m;
day = d;
}
int getDay();
{
return day
}
private
int month, day;
}
int main()
{
Date d;
setDate(10,20);
cout << getDay() << endl;
return 0;
}
4 What output would be produced by the following program?
.........................
int main()
{
ifstream fin;
fin.open("list.dat");
int count = 0;
double next, total=0.0;
cout << fixed << setprecision(2);
while (fin >> next)
{
count++;
total += next;
cout << setw(7) << next << endl;
}
fin.close();
cout << "The sum of " << count << " numbers is "
<< total << endl;
return 0;
}
The file list.dat contains the following five numbers (and nothing more):
10.0001 1.1e2 1.0e-6
5.0007 -5.0008