Question

Using Visual Studio, design, implement, and test an object class for a Cartesian 3-dimensional vector. Your class MUST be dec

OCF (Orthodox Canonical Form)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Header File:

#pragma once
#include<math.h>
class ThreeDVector
{
public:
   ThreeDVector(void);
   ~ThreeDVector(void);
   double magnitude();
   ThreeDVector operator+ (ThreeDVector);
   ThreeDVector operator- (ThreeDVector);

   int x, y, z;
};

CPP file:

#include "ThreeDVector.h"

/* Standard constructor
* Initializes values to zeros
*/
ThreeDVector::ThreeDVector(void) {
   x = 0;
   y = 0;
   z = 0;
}

ThreeDVector::~ThreeDVector(void) {
}

/* mganitude for 3 dimensional vector
* function calculates scallar magnitude of vector
*/
double ThreeDVector::magnitude() {
   return sqrt(double(x*x+y*y+z*z));
}

/* + operator overloaded for 3 dimensional vectors
* it adds one vector to another
*/
ThreeDVector ThreeDVector::operator+ (ThreeDVector v) {
   ThreeDVector sum;
   sum.x = x + v.x;
   sum.y = y + v.y;
   sum.z = z + v.z;
   return sum;
}

/* - operator overloaded for 3 dimensional vectors
* it subtracts one vector to another
*/
ThreeDVector ThreeDVector::operator- (ThreeDVector v) {
   ThreeDVector sum;
   sum.x = x - v.x;
   sum.y = y - v.y;
   sum.z = z - v.z;
   return sum;
}


Tester code:

#include <iostream>
#include <string>
#include "ThreeDVector.h"
using namespace std;

int main() {
   ThreeDVector a, b, c;
   cout << "Enter first vector ";
   cin >> a.x >> a.y >> a.z;
   cout << "Enter second vector ";
   cin >> b.x >> b.y >> b.z;

   cout << "Magnitude of first vector is " << a.magnitude() << endl;
   cout << "Magnitude of second vector is " << b.magnitude() << endl;

   c = a + b;
   cout <<"Sum is "<< c.x << " " << c.y << " " << c.z << endl;
   c = a - b;
   cout <<"Difference is "<< c.x << " " << c.y << " " << c.z << endl;

   system("pause");
}

Output:
Enter first vector 1 -2 e Enter second vector 4 3 -6 IMagnitude of first vector is 2.23607 Magnitude of second vector is 7.81


Comment down for any queries

Please give a thumb up

Add a comment
Know the answer?
Add Answer to:
OCF (Orthodox Canonical Form) Using Visual Studio, design, implement, and test an object class for a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • implement a C++ class name Vector. The Vector class contains a private double array of length...

    implement a C++ class name Vector. The Vector class contains a private double array of length 2 named elements that represents a two-dimensional vector. We will also implement an overloaded multiplication operator (*) that accepts a single Vector variable by reference and returns the Dot Product between the current Vector object and the one pointed to by the function argument in form of a double. Please recall that the dot product of two vectors a =(21,92) and 5 = (b1,b2)...

  • Design a class named NumDays, to store a value that represents a number of hours and...

    Design a class named NumDays, to store a value that represents a number of hours and convert it to a number of days. For example, 24 hours would be converted to 1 day, 36 hours would be converted to 1.5 days, and 54 hours would be converted to 2.25 days. This class has two private member variables with data type double: one is named as hours; another is named as days. This class has a constructor that accepts a value...

  • Objectives You will implement and test a class called MyString. Each MyString object keeps track ...

    Objectives You will implement and test a class called MyString. Each MyString object keeps track of a sequence of characters, similar to the standard C++ string class but with fewer operations. The objectives of this programming assignment are as follows. Ensure that you can write a class that uses dynamic memory to store a sequence whose length is unspecified. (Keep in mind that if you were actually writing a program that needs a string, you would use the C++ standard...

  • Please write below code in C++ using Visual Studio. Write program that uses a class template...

    Please write below code in C++ using Visual Studio. Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 • Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the...

  • C++ I am using visual studio for it. Make a copy of the Rational class you...

    C++ I am using visual studio for it. Make a copy of the Rational class you created in the previous Lab. Modify the class. Replace all your mathematical, input, and output functions with overloaded operators. Overload the following 12 operators: + - * / < > = = ! = <= >= >> << In order to test your class in your main function, prompt the user for a numerator and a denominator for your first object. Repeat the prompt...

  • For C++ This is the information about the meal class 2-1. (24 marks) Define and implement...

    For C++ This is the information about the meal class 2-1. (24 marks) Define and implement a class named Dessert, which represents a special kind of Meal. It is to be defined by inheriting from the Meal class. The Dessert class has the following constructor: Dessert (string n, int co) // creates a meal with name n, whose type // is "dessert" and cost is co The class must have a private static attribute static int nextID ; which is...

  • write a C++ program using classes, friend functions and overloaded operators. Computer class: Design a computer...

    write a C++ program using classes, friend functions and overloaded operators. Computer class: Design a computer class (Computer) that describes a computer object. A computer has the following properties: Amount of Ram in GB (examples: 8, 16), defaults to 8. Hard drive size in GB (examples: 500, 1000), defaults to 500. Speed in GHz (examples: 1.6, 2.4), defaults to 1.6. Type (laptop, desktop), defaults to "desktop" Provide the following functions for the class: A default constructor (constructor with no parameters)...

  • Goals . Understand how to implement operator overloading in C++ Warning: Programs must compile us...

    c++ format Goals . Understand how to implement operator overloading in C++ Warning: Programs must compile using gt+ on the Computer Science Linux systems. If your code does not compile on CS machines you will get 0 for the assignment. Organize your files into folders by lab assignment. For this assignment, create a folder called Lab9. A. Read Chapter 18 B. Create a new class called RationalNumber to represent fractions. The class should have the following capabilities: 1) It should...

  • PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and...

    PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and use the multiplication method to generate hash keys. You may use your own linked list code from a previous assignment or you can use a standard sequence container. Partial definitions for the hash table class and a generic data class are provided (C++ and Java versions). You may use them as a starting point if you wish. If you choose to design your own...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT