Page 1 of 1

## 4 Replies - 332 Views - Last Post: 07 November 2017 - 05:07 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=407433&amp;s=167437d3e6a170ff9588f9d1d92fb4a1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 KING iMm0rt4lxM

Reputation: 1
• Posts: 44
• Joined: 08-November 15

Posted 06 November 2017 - 04:35 PM

My project says --

Exercise 1: A number class (20 points)

Develop a class called Number that represents an integer value. The class should have a constructor that takes the integer value as its only argument. The constructor should store this value in a private member variable.

Define a public member function called isPrime that takes no argument and returns a bool. If the number is a prime number, then isPrime returns true, otherwise it returns false.

Define another member function called isDivisibleBy that takes a single integer argument and returns a bool. If the integer passed into isDivisibleBy is a divisor for the number, then the function returns true, otherwise it returns false.

Test your implementation of the Number class using the following test code.
```#include <iostream>

#include <cassert>

#include "Number.h"

using namespace std;

int main()

{

Number nine(9);

assert(!nine.isPrime());

assert( nine.isDivisibleBy(1));

assert(!nine.isDivisibleBy(2));

assert( nine.isDivisibleBy(3));

assert(!nine.isDivisibleBy(8));

assert( nine.isDivisibleBy(9));

Number seven(7);

assert( seven.isPrime());

assert( seven.isDivisibleBy(1));

assert(!seven.isDivisibleBy(2));

assert(!seven.isDivisibleBy(3));

assert( seven.isDivisibleBy(7));

Number seventySeven(77);

assert(!seventySeven.isPrime());

Number seventyThree(73);

assert(seventyThree.isPrime());

Number twentySeven(27);

assert(!twentySeven.isPrime());

assert(!twentySeven.isDivisibleBy(2));

assert(twentySeven.isDivisibleBy(3));

assert(twentySeven.isDivisibleBy(9));

cout << "All tests passed.\n";

return 0;

}

```

Your solution to this exercise should contain the following three files.

ex1.cpp

contains the test code given above

Number.h

contains the Number class declaration

Number.cpp

contains the implementation of the constructor and isPrime

********************** QUESTION *****************

My compiler says I have an "undefined reference" whenever I run the program. But I don't understand why is not getting my class Number.

I tried looking for the error, my main idea is that the code inside Number.cpp is wrong. Any advice would be much appreciated.

```// Main File - Exercise1.cpp

#include <iostream>
#include <cassert>
#include "Number.h"

using namespace std;

int main()
{
Number nine(9);

assert(!nine.isPrime());

assert( nine.isDivisibleBy(1));

assert(!nine.isDivisibleBy(2));

assert( nine.isDivisibleBy(3));

assert(!nine.isDivisibleBy(8));

assert( nine.isDivisibleBy(9));

/* Number seven(7);

assert( seven.isPrime());

assert( seven.isDivisibleBy(1));

assert(!seven.isDivisibleBy(2));

assert(!seven.isDivisibleBy(3));

assert( seven.isDivisibleBy(7));

Number seventySeven(77);

assert(!seventySeven.isPrime());

Number seventyThree(73);

assert(seventyThree.isPrime());

Number twentySeven(27);

assert(!twentySeven.isPrime());

assert(!twentySeven.isDivisibleBy(2));

assert(twentySeven.isDivisibleBy(3));

assert(twentySeven.isDivisibleBy(9));

cout << "All tests passed.\n";*/

return 0;

}

```

```// Class File - Number.h

#ifndef NUMBER_H
#define NUMBER_H

#include <iostream>
using namespace std;

class Number
{
public:
Number(int);
bool isPrime();
bool isDivisibleBy(int n);
private:
int value;
};

#endif // NUMBER_H

```

```// Number.cpp

#include <iostream>
#include "Number.h"

using namespace std;

Number::Number(int n)
{
// constructor
value = n;
}

bool Number::isPrime()
{
// isPrime details
bool isPrime = true;
for (int i = 2; i < n / 2; ++i)
{
if (n % i == 0)
{
isPrime = false;
break;
}
}
return isPrime;
}

bool Number::isDivisibleBy(int n)
{
// function definition
bool isDivisibleBy == true;
if (n % n != 0)
{
isDivisibleBy = false;
}

return isDivisibleBy;
}

```

This post has been edited by Skydiver: 06 November 2017 - 05:46 PM
Reason for edit:: Fixed botched code tags. Thanks for trying! You are ahead of the game.

Is This A Good Question/Topic? 0

### #2 Martyr2

• Programming Theoretician

Reputation: 5239
• Posts: 14,031
• Joined: 18-April 07

Posted 06 November 2017 - 04:58 PM

Look at line 32 where you define your isDivisibleBy variable. You used two equal signs, not one.

### #3 KING iMm0rt4lxM

Reputation: 1
• Posts: 44
• Joined: 08-November 15

Posted 06 November 2017 - 07:13 PM

I made it work, I needed to add "Number.cpp" into my main file. I just tried it, don't know why yet. But I'll look into it

### #4 ndc85430

• I think you'll find it's "Dr"

Reputation: 849
• Posts: 3,407
• Joined: 13-June 14

Posted 06 November 2017 - 10:10 PM

No, that isn't what is usually done. You should just include the file when compiling the program (e.g. on the command line when you specify the name of the file containing main). You'll need to tell us which compiler you're using.

### #5 baavgai

• Dreaming Coder

Reputation: 7197
• Posts: 15,004
• Joined: 16-October 07