Task 1: Design Class (.h file) - Create a new project - Design a class to allow
ID: 3745590 • Letter: T
Question
Task 1: Design Class (.h file)
- Create a new project
- Design a class to allow calculations on vector number (numbers with an x and y component)
- Declare a default constructor and an overload of a constructor that supports passing each of the attributes and passing a magnitude and angle (total of 3 constructor versions).
- Make all attributes (variables) private.
- Declare getters and setters for all attributes as appropriate.
- Declare a print member function to print out the value using cout.
THEN
- Write the implementation code for the methods declared in Task 1. The definitions should be in a separate .cpp file.
NOTE: You will likely need to change the .h file
Explanation / Answer
MyVector.h
---------
#ifndef MyVector_h
#define MyVector_h
#include <iostream>
#include <string>
using namespace std;
class MyVector{
private:
int x, y;
public:
MyVector();
MyVector(int x1, int y1);
MyVector(double magnitude, double angleDegrees);
int getX();
int getY();
void setX(int x1);
void setY(int y1);
void print();
};
#endif /* MyVector_h */
MyVector.cpp
--------
#include "MyVector.h"
#include <cmath>
MyVector::MyVector(){
x = 0;
y = 0;
}
MyVector::MyVector(int x1, int y1){
x = x1;
y = y1;
}
MyVector::MyVector(double magnitude, double angleDegrees){
double radians = angleDegrees * M_PI / 180;
x = magnitude * cos(radians);
y = magnitude * sin(radians);
}
int MyVector::getX(){
return x;
}
int MyVector::getY(){
return y;
}
void MyVector::setX(int x1){
x = x1;
}
void MyVector::setY(int y1){
y = y1;
}
void MyVector::print(){
cout << "x = " << x << ", y = " << y << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.