Hi there,
I have an assignment from my tutor and I am not sure I understand it correctly :(
I need some help with clarification (maybe more experienced people will be able to help)
Make a class to hold 2D array. You must start by defining a class:
1 2 3 4
|
class Matrix
{
private:
double m[2][2];
|
* use a 4 argument constructor to initialize the arrays m1 and m2. Use a no arguments constructor to initialize the array elements of m3 to 0.
*Include a printOut method in the class so that we can print out any desired matrix like so: m1.printOut();
*Later: overload the + sign to add together the elements individually inside
the o/operator function like so:
|
temp.m[0][0] = m[0][0] + mat.m[0][0];
|
I done I think most of the assignment... however I am not clear on
how to create 4 argument constructor. I do appreciate I need 4 variables
I have added this to private: inside class, but that does not make to much sense
at least for me. Array is type double so I can't use create custom type to hold this inside array.
If you could advise what do you think about the code it would be Fab :)
My tutor got very laid back approach usually reply to my email 1h before my class :(
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
matrix.h
#pragma once
#include <string>
#include <iostream>
class matrix
{
private:
double m[2][2];
int employee;
std::string name;
std::string surname;
double rate;
public:
matrix()
{
m[2][2] = { 0 };
}
matrix(int employee_, std::string name_, std::string surname_, double rate_) : employee(employee_), name(name_), surname(surname), rate(rate_)
{}
void getData(matrix m_);
void printOut();
matrix operator + (matrix am) const; // add 2 matrixes
};
matrix.cpp
#include "matrix.h"
#include <iostream>
void matrix::printOut()
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
std::cout << m[i][j] << std::endl;
}
}
}
matrix matrix::operator+(matrix am) const
{
matrix temp;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
temp.m[i][j] = m[i][j] + am.m[i][j];
}
}
return temp;
}
void matrix::getData(matrix m_)
{
std::cout << "Please enter value for arrya" << std::endl;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
std::cout << "Plese provide " << j+1 << " value: ";
std::cin >> m[i][j];
}
}
}
main.cpp
#include <iostream>
#include "matrix.h"
#include <string>
int main()
{
matrix m1, m2;
matrix m3;
//get data from user
std::cout << "M1 Object: " << std::endl;
m1.getData(m1);
std::cout << "M2 Object: " << std::endl;
m2.getData(m2);
// funcion to print out
std::cout << "Prinint object M1: " << std::endl;
m1.printOut();
std::cout << "Prinint object M2: " << std::endl;
m2.printOut();
//Overloading + operator
std::cout << "adding M3 = M1 + M2 " << std::endl;
m3 = m1 + m2;
std::cout << "Prinint object M3: " << std::endl;
m3.printOut();
system("pause");
return 0;
}
|
thanks very much :)