How can I put the class definisions in a separate header file. I\'ve been stuck
ID: 3698319 • Letter: H
Question
How can I put the class definisions in a separate header file. I've been stuck on this for a while. Complete code below:
// Header
#pragma once
#include <iostream>
using namespace std;
class matrixType{
// variable declaration
private:
public:
int row, col, array[10][10];
matrixType() { }
matrixType(int, int);
friend ostream& operator << (ostream& os, const matrixType& mtx); // can access the private and public members of a class
void GetElements();
matrixType operator+(const matrixType& mtx);
matrixType operator-(const matrixType& mtx);
matrixType operator*(const matrixType& mtx);
};
// CONSTRUCTOR
matrixType::matrixType(int a, int b){
row = a;
col = b;
}
ostream& operator<<(ostream& output, const matrixType& mtx) // overload the operator to output a matrix
{
for (int r = 0; r < mtx.row; ++r) {
for (int c = 0; c < mtx.col; ++c) {
output << mtx.array[r][c] << " ";
}
output << endl;
}
return output;
}
// GETTERS /////////////////
void matrixType::GetElements()
{
cout << "Please enter all the elements:" << endl;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
cin >> array[i][j];
}
}
}
// SETTERS /////////////////
matrixType matrixType::operator+(const matrixType& mtx) // add 2 matrices
{
matrixType a(row, col);
if ((row == mtx.row) && (col == mtx.col))
{
for (int i = 0; i < row; ++i) { // fill rows
for (int j = 0; j < col; ++j) { // fill columns
a.array[i][j] = array[i][j] + mtx.array[i][j]; // add values
}
}
}else{
exit(134);
}
return a;
}
// subtract 2 matrices
matrixType matrixType::operator-(const matrixType& mtx)
{
matrixType b(row, col);
if ((row == mtx.row) && (col == mtx.col))
{
for (int i = 0; i < row; ++i) { // fill in values in row
for (int j = 0; j < col; ++j) { // fill in values in column
b.array[i][j] = array[i][j] - mtx.array[i][j]; // subtract values
}
}
}else{
exit(187);
}
return b;
}
// multiply 2 matrices
matrixType matrixType::operator*(const matrixType& mtx)
{
matrixType c(row, col);
if (col == mtx.row)
{
for (int i = 0; i < row; ++i) { // fill rows
for (int j = 0; j < mtx.col; ++j) { // fill columns
int product = 0;
for (int i2 = 0; i2 < col; ++i2) {
product += array[i][i2] * mtx.array[i2][j]; // multiply values
}
c.array[i][j] = product;
}
}
}else{
exit(147);
}
return c;
}
// Driver
#include <iostream>
#include "matrixType.h"
#include <windows.h>
using namespace std;
int main(){
int row1;
int col1;
int row2;
int col2;
int num;
while (true) {
// menu display
cout << "1.) Addition" << endl;
cout << "2.) Subtraction" << endl;
cout << "3.) Multiplication" << endl;
cout << "4.) Exit" << endl << endl;
cout << "Select an operation from the menu: ";
cin >> num;
cout << "OPERATION" << " " << num << " SELECTED." << endl << endl;
Beep(700, 100);
if (num == 4) {
cout << "EXITING PROGRAM" << endl << endl;
// exit tune
Beep(700, 500);
Beep(525, 175);
Beep(500, 175);
Beep(525, 175);
Beep(550, 500);
Beep(525, 175);
Sleep(850);
cout << "Have a nice day!" << endl;
Beep(675, 300);
Sleep(200);
Beep(700, 300);
exit(0);
}
// matrix 1 /////////////////////////////////////////
cout << "Enter the number of rows in matrix1: ";
cin >> row1;
//cout << endl;
Beep(700, 100);
cout << "Enter the number of columns in matrix1: ";
cin >> col1;
//cout << endl;
Beep(700, 100);
// matrix 2 /////////////////////////////////////////
cout << " Enter the number of rows in matrix2: ";
cin >> row2;
//cout << endl;
Beep(700, 100);
cout << "Enter the number of columns in matrix2: ";
cin >> col2;
//cout << endl;
Beep(700, 100);
// matrix instantiations
matrixType a(row1, col1);
matrixType b(row2, col2);
matrixType ab(row1, col1); // sum of a and b
matrixType absubtraction(row1, col1); // difference of a and b
matrixType abmultiply(row1, col2); // product of a and b
cout << endl;
switch (num){
case 1:
// input two matrix
a.GetElements();
b.GetElements();
ab = a + b; // addition
cout << "Results for Matrix after values being added: " << ab << endl;
break;
case 2: // inputting two elements into
a.GetElements();
b.GetElements();
absubtraction = a - b; // subtraction
cout << "Results for Matrix after values being subtracted: " << absubtraction << endl;
break;
case 3: // inputting two matrix
a.GetElements();
b.GetElements();
abmultiply = a * b; // multiplication
cout << "Results for Matrix after values being multiplied: " << abmultiply << endl;
break;
default:
system("cls");
cout << "Invalid Input. Try again... ";
break;
}
}
system("pause");
return 0;
}
Explanation / Answer
Putting the class definitions in the header file:-
Generally we use the header files, so that we can use the code snippet in other areas of the project which we want.
Just to reach the functionality of the code reusability, this approach is basically used.
Traditionally, all the class declrations are put in the header file of the same name as that of the class.
For eg. we have class "matrixType" , then we will have the headerfile in this name.
Below we have included the header file for the "matrixType" class.
#ifndef matrixType_H
#define matrixType_H
class matrixType{
// variable declaration
private:
public:
int row, col, array[10][10];
matrixType() { }
matrixType(int, int);
friend ostream& operator << (ostream& os, const matrixType& mtx); // can access the private and public members of a class
void GetElements();
matrixType operator+(const matrixType& mtx);
matrixType operator-(const matrixType& mtx);
matrixType operator*(const matrixType& mtx);
};
Similarly, acording to the traditional approach the methods of the class can be declared in the .cpp files.
Below is the process to include the .cpp files.
#include "matrixType.h"
// CONSTRUCTOR
matrixType::matrixType(int a, int b){
row = a;
col = b;
}
ostream& operator<<(ostream& output, const matrixType& mtx) // overload the operator to output a matrix
{
for (int r = 0; r < mtx.row; ++r) {
for (int c = 0; c < mtx.col; ++c) {
output << mtx.array[r][c] << " ";
}
output << endl;
}
return output;
}
// GETTERS /////////////////
void matrixType::GetElements()
{
cout << "Please enter all the elements:" << endl;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
cin >> array[i][j];
}
}
}
// SETTERS /////////////////
matrixType matrixType::operator+(const matrixType& mtx) // add 2 matrices
{
matrixType a(row, col);
if ((row == mtx.row) && (col == mtx.col))
{
for (int i = 0; i < row; ++i) { // fill rows
for (int j = 0; j < col; ++j) { // fill columns
a.array[i][j] = array[i][j] + mtx.array[i][j]; // add values
}
}
}else{
exit(134);
}
return a;
}
// subtract 2 matrices
matrixType matrixType::operator-(const matrixType& mtx)
{
matrixType b(row, col);
if ((row == mtx.row) && (col == mtx.col))
{
for (int i = 0; i < row; ++i) { // fill in values in row
for (int j = 0; j < col; ++j) { // fill in values in column
b.array[i][j] = array[i][j] - mtx.array[i][j]; // subtract values
}
}
}else{
exit(187);
}
return b;
}
// multiply 2 matrices
matrixType matrixType::operator*(const matrixType& mtx)
{
matrixType c(row, col);
if (col == mtx.row)
{
for (int i = 0; i < row; ++i) { // fill rows
for (int j = 0; j < mtx.col; ++j) { // fill columns
int product = 0;
for (int i2 = 0; i2 < col; ++i2) {
product += array[i][i2] * mtx.array[i2][j]; // multiply values
}
c.array[i][j] = product;
}
}
}else{
exit(147);
}
return c;
}
As we have defined all the methods used in the class in .cpp file, we can use the function whereever we want to while
implementing any projects.For eg we can use these in the driver program accordingly, and we can reuse them.
Just as we have included the other header files, we can include the above header file which have been created.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.