C program Create a class Vector with floatingpoint fields x

C++ program

Create a class Vector with floating-point fields x, y, and z, with accessors, mutators, and constructors for these. Also create methods Vector Vector::add(Vector u) float Vector::dot(Vector u) Vector Vector::cross(Vector u) Create a header vector.h and source vector.cpp. Demonstrate the functions in the file vector-ops.cpp. Translations to and from polar coordinates. Additional points if the following methods are implemented: float Vector::radius() float Vector::theta() float Vector::phi() Vector Vector::Vector(float radius, float phi, float rho) That is, the methods radius, theta, and phi should give the magnitude radius the polar angle theta, and the azimuthal angle phi. In addition, a constructor should be added such that when these values are supplied, a Cartesian vector is created. Demonstrate these also in vector-ops.cpp. Make it generic and overload operators. Additional points if you make the class generic. Also, instead of defining add, overload the + operator; instead of dot, overload *; and instead of cross, overload &. Demonstrate these as well in vector-ops.cpp.

Solution

# include<iostream>

# include<cmath>

using namespace std;

//class vector specified in Question

class Vector

{

    public:

            float x,y,z;

            Vector():x(0),y(0),z(0){

            }

            Vector add(Vector u);

            float dot(Vector u);

            Vector cross(Vector u);

            float radius();

            float theta();

            float phi();

            void display()

            {

                cout<<x<<\"i + \"<<y<<\"j + \"<<z<<\"k\"<<endl;

            }

            Vector(float rad, float p,float rho);

            Vector operator+(Vector u); //operator overloading + for add

            float operator*(Vector u); //operator overloading * for dot product

            Vector operator&(Vector u); //operator overloading & for cross product

};

# include \"vector.h\"

Vector Vector::add(Vector u)

{

    Vector temp;

    temp.x = x+u.x;

    temp.y = y+u.y;

    temp.z = z+u.z;

    return temp;

}

float Vector::dot(Vector u)

{

               return (x*u.x) + (y*u.y) + (z*u.z);

}

Vector Vector::cross(Vector u)

{

               Vector temp;

               temp.x = y*u.z-z*u.y;

               temp.y = z*u.x-x*u.z;

               temp.z = x*u.y-y*u.x;

               return temp;

}

float Vector::radius()

{

               float r = std::sqrt((x*x)+(y*y)+(z*z));

               cout<<\"Radius is: \"<<r<<endl;

               return r;

}

float Vector::theta()

{

               float t = atan(y/x);

               cout<<\"Theta is: \"<<t<<endl;

               return t;

}

float Vector::phi()

{

               float p = atan((std::sqrt((x*x) + (y*y)))/z);

               cout<<\"phi is: \"<<p<<endl;

               return p;

}

Vector::Vector(float radius, float phi,float rho)

{

    float X = radius * sin(phi) * cos(rho);

    float Y = radius * sin(phi) * sin(rho);

    float Z = radius * cos(phi);

    cout<<\"Cartesian Vector is\"<<endl;

    cout<<X<<\"i + \"<<Y<<\"j + \"<<Z<<\"k\"<<endl;

}

//defination of operator overloaded function +

Vector Vector::operator+(Vector u)

{

    Vector temp;

    temp.x = x+u.x;

    temp.y = y+u.y;

    temp.z = z+u.z;

    return temp;

}

//defination of operator overloaded function *

float Vector::operator*(Vector u)

{

    return (x*u.x) + (y*u.y) + (z*u.z);

}

//defination of operator overloaded function &

Vector Vector::operator&(Vector u)

{

    Vector temp;

    temp.x = y*u.z-z*u.y;

    temp.y = z*u.x-x*u.z;

    temp.z = x*u.y-y*u.x;

    return temp;

}

# include<iostream>

# include \"vector.h\"

using namespace std;

int main()

{

               Vector V,u,temp,temp1,temp2,temp3;

               V.x = 1;

               V.y = 2;

               V.z = 3;

               u.x = 4;

               u.y = 5;

               u.z = 6;

               float p = V.dot(u);

               cout<<\"Dot product of vector is: \"<<p<<endl;

               temp=V.add(u);

               cout<<\"Addition of vector is :\"<<endl;

               temp.display();

               temp1=V.cross(u);

               cout<<\"cross product of vector is :\"<<endl;

               temp1.display();

               float rad,phi,rho;

               rad = 12.3;

               phi = 0.7;

               rho = 0.9;

               Vector t(rad,phi,rho);

               p = V*u;

               cout<<\"Dot product of vector is: \"<<p<<endl;

               temp2=V+u;

               cout<<\"Addition of vector is :\"<<endl;

               temp2.display();

    temp3=V&u;

               cout<<\"cross product of vector is :\"<<endl;

               temp3.display();

               return 0;

}

C++ program Create a class Vector with floating-point fields x, y, and z, with accessors, mutators, and constructors for these. Also create methods Vector Vecto
C++ program Create a class Vector with floating-point fields x, y, and z, with accessors, mutators, and constructors for these. Also create methods Vector Vecto
C++ program Create a class Vector with floating-point fields x, y, and z, with accessors, mutators, and constructors for these. Also create methods Vector Vecto
C++ program Create a class Vector with floating-point fields x, y, and z, with accessors, mutators, and constructors for these. Also create methods Vector Vecto
C++ program Create a class Vector with floating-point fields x, y, and z, with accessors, mutators, and constructors for these. Also create methods Vector Vecto

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site