4 Replies - 247 Views - Last Post: 07 November 2017 - 05:07 AM Rate Topic: -----

#1 KING iMm0rt4lxM  Icon User is offline

  • New D.I.C Head

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

Overloading Constructors

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
  • +

Replies To: Overloading Constructors

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5073
  • View blog
  • Posts: 13,697
  • Joined: 18-April 07

Re: Overloading Constructors

Posted 06 November 2017 - 04:58 PM

Look at line 32 where you define your isDivisibleBy variable. You used two equal signs, not one.
Was This Post Helpful? 0
  • +
  • -

#3 KING iMm0rt4lxM  Icon User is offline

  • New D.I.C Head

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

Re: Overloading Constructors

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
Was This Post Helpful? 0
  • +
  • -

#4 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,468
  • Joined: 13-June 14

Re: Overloading Constructors

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.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6979
  • View blog
  • Posts: 14,600
  • Joined: 16-October 07

Re: Overloading Constructors

Posted 07 November 2017 - 05:07 AM

If you're defining isDivisibleBy then it's a pretty safe bet that your isPrime would be expected to call it...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1