Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Assignment 2 Solution

This is Related to OOP concepts

Uploaded by

mabdullah1mughal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Assignment 2 Solution

This is Related to OOP concepts

Uploaded by

mabdullah1mughal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Question 1:

Step 1
1 of 2

Assuming that line 4 should've been a default \textbf{class}class constructor, it should


use the \textbf{class}class name (1 is missing):.

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.

class syntaxErrors1 { // ... };

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:

syntaxErrors2(int = 0, double = 0);


Step 2
2 of 2

In line 7, \textbf{private}private access modifier should have a colon instead of the


semi-colon at the end:

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

The \textbf{foodType class}foodType class has


6 \textbf{private}private member variables:

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

The \textbf{foodType class}foodType class has a default constructor and a


parameterized constructor:

foodType(); // default constructor foodType(string, int, double, int, double, double);


Result
2 of 2
2
Step 1
1 of 2
A constant function is defined by putting the \textbf{const}const keyword after the
brackets of the function.
Result
2 of 2
7
Step 1
1 of 2

The \textbf{fruit1}fruit1 object is initialized using the default constructor since no


parameters were given.

foodType();
Step 2
2 of 2

The \textbf{fruit2}fruit2 object is initialized using the parameterized constructor


since the parameters were given.

foodType(string, int, double, int, double, double);


Step 1
1 of 2

To replace these constructors, we will use the parameters of the parameterized


constructor with an addition of initializing them to zero in the prototype.

foodType(); foodType(string, int, double, int, double, double);


Result
2 of 2
foodType(string = 0, int = 0, double = 0, int = 0, double = 0, double = 0);

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

Add the member variable under the private access modifier:

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

Next, the initialize() function:

void counterType::initialize()
{
counter = 0;
}
Step 6
6 of 7

Then, the accessor member function getCounter():

int counterType::getCounter() const


{
return counter;
}
Step 7
7 of 7

Lastly, the increment() and the decrement() member function definitions:

void counterType::increment()
{
counter++;
}

void counterType::decrement()
{
// making sure that the value
// doesn't become negative
if (counter > 0)
counter--;
}

You might also like