943,844 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 745
  • C++ RSS
Apr 8th, 2009
0

Vector Addition Using OOP?

Expand Post »
I want to create a program that asks the user to enter two vector (size and direction), and calculates its addition through turning it into components (using OOP).
here's what I've got so far (I don't know how to fix the errors), I'm not sure what's wrong/ to do next (vector 1 has a magnitude of 1 and 60 degrees while vector 2 has 2 and 50 degrees):
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4.  
  5. // A class declaration for class Complex
  6. // This class creates complex number objects z = x + i*y,
  7. // where x is the real and y the imaginary part
  8. class Vector
  9. {
  10.  
  11. Public:
  12. Vector();
  13. void assign (double x, double y);
  14. void convert (double x, double y);
  15. Vector modulus();
  16. Vector operator+(const Vector &z);
  17. Vector operator-(double xsize, double ysize);
  18.  
  19. void Vector::assign(double x, double y){
  20. double size = x;
  21. double degree = y;
  22. }
  23. void Vector::convert(double x, double y){
  24. double xsize = size*cos(degree/180.0);
  25. double ysize = size*sin(degree/180.0);
  26. }
  27.  
  28.  
  29.  
  30. Vector Vector::operator(const Vector &z){
  31. Vector temp;
  32. Vector xsize=(*this).xsize+z.xsize;
  33. Vector ysize=(*this).ysize+z.ysize;
  34. return Temp;
  35. )
  36.  
  37. Vector Vector::modulus(){
  38. resultantvec= sqrt(xsize*xsize + ysize*ysize);
  39. return resultantvec;
  40. }
  41.  
  42. Vector Vector::operator-(double xsize, double ysize){
  43. resultantrad=atan2(ysize/xsize);
  44. resultantdeg=resultantrad*180;
  45. return resultantdeg;
  46. }
  47.  
  48.  
  49. //Below, the main()
  50. int main(){
  51.  
  52. Vector num1;
  53. Vector num2;
  54.  
  55. num1.assign(1,60);
  56. num2.assign(2,50);
  57.  
  58. cout<<"the resultant vector is"<<resultantvec<<"at an angle of" resultantdeg<<endl;
  59. return 0;
  60. }

Thanks a lot for the help!
Last edited by everydaybh; Apr 8th, 2009 at 11:44 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
everydaybh is offline Offline
2 posts
since Apr 2009
Apr 9th, 2009
0

Re: Vector Addition Using OOP?

Your program has too many errors to enumerate. You need to learn the basics (like you can't say Public instead of public; capitalization counts). Get a decent book and work through the examples.
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Apr 9th, 2009
0

Re: Vector Addition Using OOP?

Other than that, in function assign , you are using local variables, which make it meaningless.
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009
Apr 9th, 2009
0

Re: Vector Addition Using OOP?

Where are the Data members?
I appreciate that you have modified complex number class to work here. This is a good example of code re-usability. But a Private inheritance in this matter would have been more eye-catching.
Well, The program has several typographical and syntactical errors.
Correct it!!.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Apr 9th, 2009
0

Re: Vector Addition Using OOP?

Agreed with all above ...

To the OP: Please remember that every C++ keyword is written in lowercase, this was introduced to keep everything simple, so actually you don't have to hesitate whether you've to enter an uppercase letter or a lowercase one, it's just always in lowercase ...
Last edited by tux4life; Apr 9th, 2009 at 5:34 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: in over my head...pointers/variables
Next Thread in C++ Forum Timeline: Why Doesnt this simple Cout statement work??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC