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.
Below is the code for above problem. I have added the main function to test the Vector class. I have also added comments to explain what each statement does.
I have included the screenshots of the code and the sample output.
If you have any queries, feel free to ask in comments.
If you find this answer helpful, please leave an Upvote.
C++ code :
#include <iostream>
using namespace std;
// Vector class
class Vector {
private :
// a double array representing a 2D
vector
double elements[2];
public :
// empty constructor
Vector() {
}
// parameterized constructor to
initialize a Vector
Vector(double elem1, double elem2)
{
this->elements[0] = elem1;
this->elements[1] = elem2;
}
// overriding the multiplication(*)
operator
double operator*(const Vector&
vect) {
// variable to
store the dot product
double
dotProduct;
// calculate the
dot product
dotProduct =
(this->elements[0] * vect.elements[0]) + (this->elements[1] *
vect.elements[1]);
// return the
calculated dot product
return
dotProduct;
}
};
// main function to test the Vector class
int main() {
// initialize the first vector
Vector first(1.5, 2.8);
// initialize the second vector
Vector second(2.4, 5.9);
/* calculate the dot product of the two vectors
using the overloaded
multiplication(*) operator
*/
double product = first * second;
// print the calculated dot product
cout << "The dot product is : " <<
product;
}
Screenshots of the code :
1)
![[*] Vector.cpp 1 #include <iostream> 2 3 using namespace std; 4. 5 // Vector class 6 class Vector { 8 private : // a double a](http://img.homeworklib.com/questions/a8968cf0-be9d-11eb-8389-bf8cf8b082d7.png?x-oss-process=image/resize,w_560)
2)
Screenshot of the sample outptut :
implement a C++ class name Vector. The Vector class contains a private double array of length...
Python REALLY NEED HELP !!!!!!!!! [10pts] Write the class Vector that supports the basic vector operations. Such operations are addition (+) and subtraction (-) of vectors of the same length, dot product (*) and multiplication (*) of a vector by a scalar. All methods must return (not print) the result. Your class should also support the rich comparison for equality (==) - You must use the special methods for those 4 operators in order to override their behavior - You...
In C++: Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the term and creates the polynomial c x^n. (This constructor can be given default arguments easily to have a...
Design a class named Month. The class should have the following private members: • name - A string object that holds the name of a month, such as "January", "February", etc. • monthNumber - An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12. In addition, provide the following member functions:• A default constructor that sets monthNumber to 1 and name...
Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the Address class in Address.cpp. a. This class should have two private data members, m_city and m_state, that are strings for storing the city name and state abbreviation for some Address b. Define and implement public getter and setter member functions for each of the two data members above. Important: since these member functions deal with objects in this case strings), ensure that your setters...
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...
C++ PRPGRAM- double-linked lists Create the .h and .cpp files for an Element class that contains the data and pointers for a double-link list for this application. Your linked list must consist of objects of this class. The class should contain at least the following elements (you may have more if you wish): The standard four constructors (default, parameterized, copy, move) The standard assignment operators (copy, move) A destructor Stream operators (<< and >>) The standard relational operators (<, <=,...
Implement a Java class that is called RainFall that uses a double array to store the amount of rainfall for each month of the year. The class should have only one instance variable of type double[]. Implement the following methods in the RainFall class: 1. A constructor that creates and initializes all entries in the array to be -1. 2. toString: a method that returns a one-line String representation of the object that includes 12 double numbers that represent the...
PLEASE DO BOTH OF THESE IN PYTHON 1) Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Python...
QUESTION 18 According to class materials, the program is allowed to overload operators only if at least one incoming parameter received by the operator has a class type. 1. (a) True 2. (b) False QUESTION 19 According to the course materials, a function can take value parameters and reference parameters. A value parameter is a separate variable from the one in main() or another calling function only if the function value parameter has a different name than the one in...
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...