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:

Comment down for any queries
Please give a thumb up
OCF (Orthodox Canonical Form) Using Visual Studio, design, implement, and test an object class for a...
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 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 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 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 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 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 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)...
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 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...