Thread: Vector Addition Using OOP?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Vector Addition Using OOP?

    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):

    Code:
    #include<iostream>
    #include<cmath>
    using namespace std;
    
    // A class declaration for class Complex
    // This class creates complex number objects  z = x + i*y, 
    // where x is the real and y the imaginary part
    class Vector
    {
          
          Public:
                 Vector();
                 void assign (double x, double y);
                 void convert (double x, double y);
                 Vector modulus();
                 Vector operator+(const Vector &z);
                 Vector operator-(double xsize, double ysize);
                 
    void Vector::assign(double x, double y){
        double size = x;
        double degree = y;
    }
    void Vector::convert(double x, double y){
         double xsize = size*cos(degree/180.0);
         double ysize = size*sin(degree/180.0);
    }
    
    
    
    Vector Vector::operator(const Vector &z){
           Vector temp;
           Vector xsize=(*this).xsize+z.xsize;
           Vector ysize=(*this).ysize+z.ysize;
           return Temp;
    )
    
    Vector Vector::modulus(){
        resultantvec= sqrt(xsize*xsize + ysize*ysize);
        return resultantvec;
    }
    
    Vector Vector::operator-(double xsize, double ysize){
           resultantrad=atan2(ysize/xsize);
           resultantdeg=resultantrad*180;
           return resultantdeg;
    }
    
    
    //Below, the main()
    int main(){                     
                           
             Vector num1;
             Vector num2;
    
             num1.assign(1,60);
             num2.assign(2,50);
                           
             cout<<"the resultant vector is"<<resultantvec<<"at an angle of" resultantdeg<<endl;
    return 0;
    }
    Thanks a lot for the help!

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    I am not sure what you are trying to accomplish with some of those functions.

    You create local variables that "vanish" after the member function completes.

    In the main function and member functions you are trying to use resultantvec which does not exist which is probably the cause of your compilation errors.
    Woop?

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    Ok, Sorry about the mistakes before; My problem is that I'm not sure what to do to make sure it returns 2 values (resultant size and degree). Do I use a friend function? an operator? A void function that passes by reference?
    Last edited by everydaybh; 04-08-2009 at 09:32 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by everydaybh
    My problem is that I'm not sure what to do to make sure it returns 2 values (resultant size and degree). Do I use a friend function? an operator? A void function that passes by reference?
    I think that you need to think a little more about the design of your Vector class. You need to decide upon a minimal but complete interface for Vector. It looks like you could do with some member variables instead of creating local variables named size and degree (and that may well solve your "make sure it returns 2 values" problem).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP flu
    By Hussain Hani in forum General Discussions
    Replies: 15
    Last Post: 06-27-2009, 02:02 AM
  2. Should OOP be any new language priority??
    By Hussain Hani in forum General Discussions
    Replies: 80
    Last Post: 06-13-2009, 10:56 AM
  3. Data Mapping and Moving Relationships
    By Mario F. in forum Tech Board
    Replies: 7
    Last Post: 12-14-2006, 10:32 AM
  4. recommendation for a good OOP book
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2006, 04:28 PM
  5. OOP Theory Question
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2005, 08:37 AM