Assignment 2 Solution
Assignment 2 Solution
Step 1
1 of 2
syntaxErrors1();
Step 2
2 of 2
Lastly, the \textbf{class}class lacks a semi-colon at the end of the curly brackets in
line 10.
Question 2:
Step 1
1 of 2
Assuming that the line 6 should've been a constructor (because a member function
cannot be the same name as the \textbf{class}class), the \textbf{void}void should
be excluded:
private:
Question 3:
Step 1
1 of 2
Members of a \textbf{class}class include member functions and variables. There are
10 \textbf{public}public member functions and 6 \textbf{private}private member
variables.
Result
2 of 2
16
Step 1
1 of 2
private: string name; // 1 int calories; // 2 double fat; // 3 int sugar; // 4 double
carbohydrate; // 5 double potassium; // 6
Result
2 of 2
6
Step 1
1 of 2
foodType();
Step 2
2 of 2
Question 4:
Step 1
1 of 7
Firstly, start by naming the class and putting the class structure together:
class counterType
{
private:
// ...
public:
// ...
};
Step 2
2 of 7
class counterType
{
private:
int counter;
public:
// ...
};
Step 3
3 of 7
Add the member function prototypes under the public access modifier:
class counterType
{
private:
int counter;
public:
void setCounter(int);
void initialize();
int getCounter() const;
void increment();
void decrement();
};
Step 4
4 of 7
Now, let's define the member functions. Starting with the setCounter() member
function:
void counterType::setCounter(int x)
{
// checking if the value is positive
if (x >= 0)
counter = x;
}
Step 5
5 of 7
void counterType::initialize()
{
counter = 0;
}
Step 6
6 of 7
void counterType::increment()
{
counter++;
}
void counterType::decrement()
{
// making sure that the value
// doesn't become negative
if (counter > 0)
counter--;
}