OOP help

Hello,
I made this program simply for practice using classes.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*this program:
-finds the distance between 2 points
*/




#include <iostream>
#include <cmath>
#include "Declarations.cpp"




using namespace std;




int main()
{
    Distance coordinates;   //holds coordinates
    double x1, x2;  //x coordinates
    double y1, y2;  //y coordinates

    //get coordinates from the user
    cout<<"Enter the first X coordinate.  ";
    cin>>x1;

    cout<<"Enter the first y coordinate.  ";
    cin>>y1;

    cout<<"Enter the second x coordinate.  ";
    cin>>x2;

    cout<<"Enter the second y coordinate.  ";
    cin>>y2;

    //set the coordinates
    set(x1, x2, y1, y2);

    //print the distance
    cout<<"\n\nThe distance is "<<get_dist()<<endl;

    //pause the program
    cin.get();

    //terminate the program
    return 0;
}




//----------------------------------------------------------------------------------------------------------------------
//Distance class functions

void Distance::set(double a1, double a2, double b1, double b2)    //sets the x and y coordinates
{
    a1=x1;
    a2=x2;
    b1=y1;
    b2=y2;

    //calculate the distance
    dist=calc();
}




double Distance::calc()   //calculates the distance between 2 points
{
    return (sqrt( (x2-x1)*(x2-x1) - (y2-y1)*(y2-y1) ));
}

//-------------------------------------------------------------------------------------------------------------------------- 


Declarations.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//finds the distance between 2 points
class Distance
{
    private:
        double x1, x2;  //x coordinates
        double y1, y2;  //y coordinates
        double dist;    //holds the distance between 2 points
    public:
        void set(double a1, double a2, double b1, double b2);   //sets x and y values
        double get_dist()
            {return dist;}
    private:
        double calc();  //calculates the distance between 2 points
}


There is an error in main.cpp on line 15. Error: expected unqualified-id before 'using'. I have been trying to solve this problem for some time now, and I can't get it. Thanks for your help.
Line 14 in Declarations.cpp needs ";"
closed account (zb0S216C)
You should not include source files; only header files. Also, you're missing a semi-colon after the closing brace of the Distance class.

Wazzak
Last edited on
To solve your problem, put a semi-colon at the end of line 14 in Declarations.cpp

However, you shouldn't be #include-ing .cpp files.

Make a new file "Declarations.h" and cut/paste the lines from Declarations.cpp to it. Then cut/paste the Distance class functions from main.cpp into Declarations.cpp. Put #include Declarations.h" at the top of Declarations.cpp and main.cpp.

Example:

main.cpp
1
2
3
4
5
6
7
8
#include "example.h"

int main(){
  Example example;
  example.write();
  cin.get();
  return 0;
}

example.h
1
2
3
4
5
6
7
class Example{
public: 
  Example(){};
  ~Example(){};
  
  void write();
};

example.cpp
1
2
3
4
5
6
7
#include <iostream>
#include "example.h"

void Example::write()
{
  std::cout << "Hello World\n";
}
Last edited on
Thanks everyone for all your help. LowestOne, if I replace Declarations.cpp with Declarations .h, could I get rid of Declarations.cpp altogether?
Last edited on
Something like this?

Example:

main.cpp
1
2
3
4
5
6
7
8
#include "example.h"

int main(){
  Example example;
  example.write();
  cin.get();
  return 0;
}

example.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

class Example{
public: 
  Example(){};
  ~Example(){};
  
  void write();
};

void Example::write()
{
  std::cout << "Hello World\n";
}


Yes, this works. I'm not sure of the reasoning behind having that extra .cpp, but I imagine it is a good one :)
Last edited on
Topic archived. No new replies allowed.