[C++] Need some serious help creating a 'class' and linking multiple source codes

Mothergoose729

Senior member
Mar 21, 2009
409
2
81
I am taking object orientated programming right now, and I already missed the second assignment. This idea of data objects and classes is not at all coming easy to me. The programming is supposed to be done in shell/ linux environment (debian I believe, hosted on a power server) but there no reason I can't test first in Dev C++ and create the appropriate makefile and .cpp files in linux.

It is a "rational" class. It takes two "long integers" and manipulates them like a rational number. This is the barebones here, I am just trying to get adding working. I need a private member function that "sends a message to the object" of what the greatest common denominator is of two objects of class rational and add them displaying sum in lowest terms (1/16 and 1/22 for example, needs to add them and reduce).

Anyway, here is my source code. It doesn't link correctly, so I don't know if there are any logic errors. My questions are below:

rational.h -header
Code:
#ifndef _RATIONAL_H_ 
      #define _RATIONAL_H_ 

      #include <iostream>
      using namespace std;

      class Rational
      {
        long _p; 
        long _q; 
        
        void gcm (const Rational&);

      public: 

        Rational ( long = 0, long = 1 ); 
        void add(const Rational&);
        //void sub(const Rational&);
        //void mult(const Rational&);
        //void div(const Rational&);
        void display() const;

      };
      #endif

rational.cpp - implementation
Code:
/*
Your name here
*/


#include "rational.h"

Rational::Rational ( long p, long q )
{
  _p = p;
  _q = q;

}

void Rational::gcm(const Rational& n)
{
    long greater;
    long least;
    long temp;
    
    if (_p < _q)
    {
            greater = _p;
            least = _q;
    }
    else
    {
        greater = _q;
        least = _p;
    }
    
    while (least!=0)
    {
          greater -= least;
          if (greater < least) 
          {
                   temp = greater;
                   greater = least;
                   least = temp;
          }
    }
    
    _p /= greater;
    _q /= greater;
}      
          

void Rational::add(const Rational& n)
{
   int lcm;
     
  _p = (n._p * _q) + (_p * n._q);
  _q *= n._q;
  
  
}

void Rational::display() const
{

  cout << _p <<"/" << _q << endl;

}

main.cpp
Code:
#include <iostream>
#include "rational.h"
#include "rational.cpp"



using namespace std;

int main()
{
    Rational num(3,4);
    Rational num2(3,4);
    
    num.add(num2);
    num.display();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

my attempt to build and compile it in dev C++ outputs these linker errors:

linker errors
Code:
  multiple definition of `Rational::Rational(long, long)' 
  first defined here 
  multiple definition of `Rational::Rational(long, long)' 
  first defined here 
  multiple definition of `Rational::gcm(Rational const&)' 
  first defined here 
  multiple definition of `Rational::add(Rational const&)' 
  first defined here 
  multiple definition of `Rational::display() const' 
  first defined here 
  ld returned 1 exit status 
 C:\Dev-Cpp\Makefile.win [Build Error]  [Project1.exe] Error 1

Truthfully I have a really foggy understanding of the way a class works. I am quite comfortable with procedural, where ever data item goes to one place and I can follow the naming and the passing easy. I really don't understand at all how a class updates the object. My professor is having us follow Meyers in his book "effective C++". Any serious programmer should know this I guess. Help getting the template down for this much would be great, then I can start worrying about operator overloading and the rest of the class functionality.

Decent programmers, take pity on my please .
 

dinkumthinkum

Senior member
Jul 3, 2008
203
0
0
The very first thing you should do is remove the line that says #include "rational.cpp". You should not be using "#include" with .cpp files. The way to combine multiple .cpp files is to compile them to object files and then link those together.
 

Mothergoose729

Senior member
Mar 21, 2009
409
2
81
The very first thing you should do is remove the line that says #include "rational.cpp". You should not be using "#include" with .cpp files. The way to combine multiple .cpp files is to compile them to object files and then link those together.

Well eventually I have to port these to debian and create a makefile instead. Have I been wasting my time trying to code this in dev C++ first? How will this syntax work with a make file.
 

dinkumthinkum

Senior member
Jul 3, 2008
203
0
0
I personally think you are wasting your time with DevC++, but that's because I'm happy and familiar with GCC and Makefiles.

The syntax of your C++ files does not change just because you build with 'make'. What I said applies regardless of whether you use DevC++ or GCC.

The first thing you should do is to learn how to use GCC by hand:

Code:
gcc -c rational.cpp
gcc -c main.cpp
gcc -o program main.o rational.o

You can express dependencies with a Makefile like so:
Code:
program: rational.o main.o
rational.o: rational.cpp rational.h
main.o: main.cpp rational.h

Each line can be read as saying that the target on the left depends on the files on the right. Now there are many default settings which should get you going to start, but they can be tweaked later. Such as the default rule for compiling C++ files, or various flags which you may want to supply. Note: GCC should recognize .cpp files as C++ files, though the more typical extension in Linux is .cc or .C
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
Please, for the love of all that is good and holy, stop using dev-C++. It is a dead project, has stability issues, is fairly outdated, and has several stellar alternatives available (code::blocks, Visual studios, Eclipse, Netbeans, ect).

As for learning makefiles... That is semi important, but not an absolute necessity.
 

Mothergoose729

Senior member
Mar 21, 2009
409
2
81
Ah, i see. I guess I should practice the correct way. Thank you.

Ok, I am going to copy my work to nano, make a few changes, and create a make file in linux. See how that goes. I will post back with my problems (that I am sure I will have) when I do .

EDIT: I am trying to make the switch to code blocks, but my last course was orientated around Dev C++ so I went to it first.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |