I am working in c++. My program requires me to use a header file that is provide
ID: 3850408 • Letter: I
Question
I am working in c++. My program requires me to use a header file that is provided. I must use this header file and create 5 methods: 2 constructors, 2 accessors and this operation* on a 4x4 matrix. Matrix has 16 floating point values that must be entered by user. The Two operator functions: i*t; and t+i, multiply identity (i) matrix by user input value matrix and second function add identity (i) plus user input matrix transform. These two operations must be able to be carried out on the same 4x4 matrix entered by the user. Here is header file below. The point of the work is to create an implementation of this class to be able to display in a console.
// Transform.h
//
#pragma once
#ifndef __Lecture_5__Transform__
#define __Lecture_5__Transform__
class Transform
{
public:
Transform(); // Identity Matrix
Transform(float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
float m20, float m21, float m22, float m23,
float m30, float m31, float m32, float m33); // Explicit ctor
// Accessors
float get(unsigned int row, unsigned int col) const;
void set(unsigned int row, unsigned int col, float value);
// Operations
Transform operator*(const Transform& rhs) const;
private:
float data[4][4];
};
#endif /* defined(__Lecture_5__Transform__) */
Explanation / Answer
#include "Transform.h"
#include <iostream>
using namespace std;
// Identity Matrix
Transform::Transform(){
for(int i=0;i<4;i++){
for(int j =0;j<4;j++){
data[i][j]=1;
}
}
};
// Explicit constructor
Transform::Transform(float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
float m20, float m21, float m22, float m23,
float m30, float m31, float m32, float m33){
data[0][0]=m00;
data[0][1]=m01;
data[0][2]=m02;
data[0][3]=m03;
data[1][0]=m10;
data[1][1]=m11;
data[1][2]=m12;
data[1][3]=m13;
data[2][0]=m20;
data[2][1]=m21;
data[2][2]=m22;
data[2][3]=m23;
data[3][0]=m30;
data[3][1]=m31;
data[3][2]=m32;
data[3][3]=m33;
};
// Accessors
float Transform::get(unsigned int row, unsigned int col){
return data[row][col];
}
// Mutator
void Transform::set(unsigned int row, unsigned int col, float value){
data[row][col] = value;
}
// Operator overloading
Transform Transform::operator*(Transform& rhs){
Transform t;
for(int i=0;i<4;i++){
for(int j =0;j<4;j++){
cout<<data[i][j]*rhs.get(i,j);
t.set(i,j,data[i][j]*rhs.get(i,j));
}
}
}
Transform Transform::operator+(Transform& rhs){
Transform t;
for(int i=0;i<4;i++){
for(int j =0;j<4;j++){
t.set(i,j,data[i][j]+rhs.get(i,j));
}
}
}
main.cpp:
#include <iostream>
#include "Transform.h"
#include <iomanip>
using namespace std;
int main()
{
Transform t1,t2;
float value;
cout << "Identity matrix:" << endl;
for(int i=0;i<4;i++){
for(int j =0;j<4;j++){
cout<<t1.get(i,j)<<" ";
}
cout<<endl;
}
cout<<"Enter matrix data: "<<endl;
for(int i=0;i<4;i++){
for(int j =0;j<4;j++){
cout<<"Enter floating number"<<i<<","<<j<<": ";
cin>>value;
t2.set(i,j,value);
}
}
Transform t3 = t2*t1;
cout << "Multipled matrix:" << endl;
for(int i=0;i<4;i++){
for(int j =0;j<4;j++){
cout<<t3.get(i,j)<<" ";
}
cout<<endl;
}
Transform t4 = t2+t1;
cout << "Addition matrix:" << endl;
for(int i=0;i<4;i++){
for(int j =0;j<4;j++){
cout<<t4.get(i,j)<<" ";
}
cout<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.