0

I need serious help with an assignment.

Create a class called Integer and another called Double. These classes should have separate header and .cpp file

In order for you to be successful you will need to determine what the data section of the class consists of. Each of these classes should have the following functions (methods) equals - This is a void function function that sets the value of the object. For example, an instance of Double should set a double value

Double d;

d.equals(12.34);

add

sub

mul

div

Each of these functions should take its type as an argument and return the same. For instance, a Double should work on Doubles, an Integer should work on Integers

The Integer class should have a function called

toInt

Which returns a primitive integer. Basically, this is the data section that gets returned.

The Double class should have a function called

toDouble 

Which returns a primitive double

In your main funciton write code to test instance of your classes

now, I have a few questions. I'm sorry if it seems.... well, silly, but I am relatively new to C++, and I'm having a little difficulty understanding what i'm supposed to do, and how to do it. of course I'm not looking for anyone to do the assignment for me, I do want to learn how to do it on my own, but I do realize I need some help.

from what I gather, the assignment is asking that I create two cpp files, one demonstrating integer and the other double, and i need to then call the files to the main.

however, I feel like something isn't quite clicking. I can't seem to call the cpp files to the main, and this is actually the first time I've worked with multiple cpp files, so I'm having a little difficulty understanding where to start.

again, I apologize if this seems silly, but I could really use some help

I have one last issue with this. I just can't find the problem in this code, and I don't know what it wants from me.

    //main.cpp

#include <cstdlib>
#include <iostream>
#include "DoubClass.h"
#include "intClass.h"
using namespace std;

int main() {
    toDouble doubleObject;

    toInterger intergerObject; 
}

//DoubClass.h

#ifndef DOUBCLASS_H
#define DOUBCLASS_H

class DoubClass {
public:
    toDouble();

};

#endif  /* DOUBCLASS_H */

//DoubClass.cpp

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

using namespace std;

DoubClass::toDouble() {
        double Dn1 = 19.83; 
        double Dn2 = 28.74;

 cout << "The sum of " <<Dn1<< " and " <<Dn2<< " is " <<Dn1 + Dn2<< endl;
 cout << "The difference of " <<Dn1<< " and " <<Dn2<< " is " <<Dn1 - Dn2<< endl;
 cout << "The quotient of " <<Dn1<< " and " <<Dn2<< " is " <<Dn1 / Dn2<< endl;
 cout << "the product of " <<Dn1<< " and " <<Dn2<< " is " <<Dn1 * Dn2<< endl;
}

The err code i get is

In file included from DoubClass.cpp:9:0:
DoubClass.h:13:14: error: ISO C++ forbids declaration of ‘toDouble’ with no type
DoubClass.cpp:13:21: error: ISO C++ forbids declaration of ‘toDouble’ with no type
make[2]: *** [build/Debug/Cygwin_4.x-Windows/DoubClass.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

I didn't include the intClass (yes, I know one isn't capitalized and the other is, mistake on my part), but it's almost perfectly identical, just for int rather than double.

Does anyone have any idea what is happening?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • 1
    Do you how to do all of this in *one* file? – Beta Feb 02 '14 at 08:16
  • 2
    Why not read http://stackoverflow.com/questions/6995572/using-multiple-cpp-files-in-c-program as well as the course notes – Ed Heal Feb 02 '14 at 08:18
  • Thanks for the link, admittedly I'm not the brightest when it comes to programming. And I can add, subtract, multiply, and divide, but the instructions just aren't clicking for me. I've also been off my meds for a while, trying not to rely on them, so there might be something to that. – user3262292 Feb 02 '14 at 08:24
  • Why stop taking them? – Ed Heal Feb 03 '14 at 04:55
  • a big reason is that i'm currently uninsured, but I also just don't like the idea of having to rely on them. – user3262292 Feb 03 '14 at 05:00
  • @user3262292 - I love being in a civilized part of the planet where health care is free. – Ed Heal Feb 03 '14 at 08:47

3 Answers3

0

Write a header file (or two) with the classes defined in, implement the class methods in separate cpp files, then in the main.cpp just include the header(s) and you will be able to access the classes and methods.

RamblingMad
  • 5,332
  • 2
  • 24
  • 48
0

Create the header files with the forward declaration of the classes Double and Integer. You could either put both the classes in the same header or create two header files. Once you do that in your main file;

#include <iostream>
#include "path to header"

for ex; if header and main are in the same folder

#include "./header1.cpp"

if header is in a seperate folder 'head' in the same folder as main

#include "./head/header1.cpp"

likewise a folder above the folder with main

 #include "../head/header1.cpp"
 #include "../header1.cpp"

I am assuming you'll be using a single header, just add another include for another header file.

Stephen Jacob
  • 889
  • 1
  • 15
  • 33
0

ToDouble() needs to return a type. In this case, you want to be returning a double type. Your DoubClass should look like:

class DoubClass {
public:
    double toDouble();

};

Your implementation should look like:

double DoubClass::toDouble() {
    ...
}

I hope that helps.

Edit: I guess I shouldn't assume that a question is new just because it's on the front page... Oh well.

Kaslai
  • 2,445
  • 17
  • 17