Need help with simple separate compilation

Hey guys,

I'm a student, and I have the following assignment:

Write a complete C++ object oriented program
solution. You will need to write the following three files:

A .h header file describing the class. Include
- 2 or 3 data members
- at least one constructor function
- set and get functions
- one function to do a calculation and
return the answer through the
function call
- one function to display the fields in the object

A .cpp C++ implementation file that includes the
code for the functions prototyped in the header file

A driver program to test your class. Include 3 or 4 objects
and use those objects to call the function that are given
in the header file.


I'm not looking for a handout, here. I've done as much as I can figure out with what is in the textbook and what I've been able to find online. Unfortunately, the professor chose to skip explaining this during class and instead simply gave us a comparatively-complicated example from which we were expected to just "get it". Anyway...

What I'd like to do is, using the proper three parts, have 2 variables x and y, have the user cin values for those, add them, then cout the sum. Simple enough with just a straight program, but this needs to be modular and I'm really lost.

I have the following so far:

Header.h:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

class basicMath
{
public:

	double Sum(double x, double y);
        return (x+y);

}sum;


Drive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "Header.h"

using namespace std;

int main()
{
	basicMath x, y;

	cout << "Enter the first number: ";
	cin >> x;

	cout << "Enter the second number: ";
	cin >> y;
}


Imp.cpp
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "Header.h"

using namespace std;

double Sum(double x, double y)
{
	return x + y;
}



Now, I know I'm doing something horribly wrong here because I'm trying to do the addition twice. Can anyone break down for me what part of the class needs to go into the header, driver, and implementation parts of the program, then maybe I can get this straight.

I appreciate any help you can offer!
My recommendation would be to make x and y private member variables. Then have your constructor with two input parameters to start and a set function to change them later on. Your get and set and display functions public to access, change, and for output.

Your sum function is never actually called in main.
Header.h, general:
- line 1 & 2, is not needed, commonly header files will contain function/class/structure declarations
- usually we* put header guards on the first two lines, and the last line of *h file e.g
1
2
3
4
#ifndef HEADER_H
#define HEADER_H
//class/function/structure declarations
#endif 

*the IDE will do this for you, just use the "add files to project" wizard on your IDE

Header.h, class declaration:
- in practice, I'm advising you against declaring an instance of your class in header files (it is allowed), do it in main.cpp or implementation.cpp
- lines 8&9 do no follow your program requirements, consult your lecture notes/lecturers/text books

Imp.cpp, class implementation:
- no need for <iostream> or using directive if you're not using them couts and cins etc, #include "Header.h" is enough

Drive.cpp, main:
- line 8: precisely why you do not need to declare an instance of your class in your header file, now you have not two, but three instances of your class
- line 11 & 14, will throw an error probably about the ">>" operator is not defined for the object, yadda-yadda
- again, consult your lecture notes/lecturer/text books
Also x and y are not ints or doubles, they are objects. Use a constructor or set function.
Let me give you a push in the right direction.

First, a class can have both functions and variables. I think this is what prof wants is something that has both.

1
2
3
4
5
6
7
8
class myClass
{
  private:
    double first; //This is a variable in myClass.
    double second; 
  public:
    double Sum(double x, double y); //This is a function in myClass.
}


So this is an example of a class. Not sure how far you have gotten in this or how much you understand, but I will give the basics here.

A private member cannot be accessed outside of the class. If in your main you wanted to write something like
myClass.first = 5
this would generate an error.
However, if you were to write a function inside your class in the public section to do this, something like:
1
2
3
4
void setFirst(double a)
{
  this.first = a;
}


This would allow you to access that with the following:
myClass.setFirst(x);

Generally functions that allow you to modify private members are known as mutators, and functions that allow you to retrieve private members are known as accessors.

Now another little trick is inline definitions in classes. You have both an inline and the other type (which I can't remember off of the top of my head).

1
2
3
4
5
6
7
8
9
10
11
12
/*Inline definition in the header file*/
class myClass
{
  private:
    double first;
    double second; 
  public:
    double Sum(double x, double y) 
    { 
      return (x + y);
    } //This is inline because it is defined when you see the declaration
}


Or if you want to define the function in another file:
1
2
3
4
5
6
7
8
9
/*Header file here*/
class myClass
{
  private:
    double first;
    double second; 
  public:
    double Sum(double x, double y);
}

1
2
3
4
5
/*Code from  the .cpp file associated with the .h file*/
double Sum(double x, double y) 
{ 
  return (x + y);
}


Now one last little hint to give you is that you also need to call these methods from within your main function.

1
2
3
4
5
6
7
int main()
{
   double x;
   cout << "Please enter the first value." << endl;
   cin >> x;
   myClass.setFirst(x);
 //etc. etc. 


For more information on classes, please see this site's tutorial on classes. To find that just go to the search up top and search for class tutorial.
Last edited on
Topic archived. No new replies allowed.