Question

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) is equal to a real scalar, which is calculated a

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

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

2)

Screenshot of the sample outptut :

Add a comment
Know the answer?
Add Answer to:
implement a C++ class name Vector. The Vector class contains a private double array of length...
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
  • Python REALLY NEED HELP !!!!!!!!! [10pts] Write the class Vector that supports the basic vector operations....

    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...

  • Design a class named Month. The class should have the following private members:

    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...

    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...

    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...

  • Implement a Java class that is called RainFall that uses a double array to store the...

    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...

  • QUESTION 18 According to class materials, the program is allowed to overload operators only if at...

    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...

  • 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...

  • c++ program 1a) Implement a class named ???????. Start by adding the following ?????? members: -...

    c++ program 1a) Implement a class named ???????. Start by adding the following ?????? members: - distance, a variable member of type double. - Non-default constructor which receives a value in order to initialize the variable distance. - Input stream operator to read a value from the command line into the variable distance. 1b) Write a test ???? as follows: - Create a vector to hold instances of type ??????? - Use the following ??? loop to read values from...

  • Q1. Write a C++ class called Time24h that describes a time of the day in hours,...

    Q1. Write a C++ class called Time24h that describes a time of the day in hours, minutes, and seconds. It should contain the following methods: • Three overloaded constructors (one of which must include a seconds value). Each constructor should test that the values passed to the constructor are valid; otherwise, it should set the time to be midnight. A display() method to display the time in the format hh:mm:ss. (Note: the time can be displayed as 17:4:34, i.e. leading...

  • OUTCOMES After you finish this assignment, you will be able to do the following: Define an...

    OUTCOMES After you finish this assignment, you will be able to do the following: Define an abstract class Create concrete classes from an abstract class Overload an operator Split classes into .h and .cpp files Open files for reading Write to files Use output manipulators such as setw, fixed, and setprecision DESCRIPTION A binary arithmetic operation takes two double operands (left and right) to perform addition, subtraction, multiplication, or division on. For example, 10 + 11 is an addition (+)...

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
ADVERTISEMENT