Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can't figure out what is wrong with this program. The first two tables work perfectly, however, the third table which adds the first two tables

Can't figure out what is wrong with this program. The first two tables work perfectly, however, the third table which adds the first two tables is broken. According to an online debugger, the program is breaking in the Table.h deconstructor ~Table. Thank you in advance!

//main.cpp

//User Libraries #include #include #include #include using namespace std;

//User Libraries #include "PlusTab.h" //Global Constants

//Function Prototype void prntTab(const Table &);

//Execution Begins Here! int main(int argc, char** argv) { //Initialize the random seed srand(static_cast(time(0)));

//Declare Variables int rows = 3, cols = 4;

//Test out the Tables PlusTab tab1(rows, cols); PlusTab tab2(tab1); PlusTab tab3 = tab1 + tab2;

//Print the tables cout

//Exit Stage Right system("pause"); return 0; }

void prntTab(const Table &a) { cout

//Table.h

#ifndef TABLE_H #define TABLE_H

#include "AbsTabl.h"

class Table :public AbsTabl { public: Table(unsigned int, unsigned int); Table(const Table &); virtual ~Table(); int getSzRow()const { return szRow; } int getSzCol()const { return szCol; } int getData(int, int)const; void setData(int, int, int); };

#endif /* TABLE_H */

//Table.cpp

#include "Table.h" #include Table::Table(unsigned int r, unsigned int c) {

szRow = r; szCol = c;

columns = new RowAray*[r];

for (int i = 0; i

for (int i = 0; i setData(j, (rand() % 90 + 10)); } } }

Table::~Table() { for (int i = 0; i

Table::Table(const Table &t) { szRow = t.szRow; szCol = t.szCol; columns = t.columns; };

int Table::getData(int r, int c) const { return columns[r]->getData(c); };

void Table::setData(int r, int c, int value) { columns[r]->setData(c, value); }

//PlusTab.h

#ifndef PLUSTAB_H #define PLUSTAB_H

#include "Table.h"

class PlusTab :public Table { public: PlusTab(unsigned int r, unsigned int c) :Table(r, c) {}; PlusTab operator+(const PlusTab &t); };

#endif /* PLUSTAB_H */

//PlusTab.cpp

#include "PlusTab.h"

PlusTab PlusTab::operator+(const PlusTab &t) { PlusTab tab(this->getSzRow(), this->getSzCol());

for (int i = 0; i setData(j, this->getData(i, j) + t.getData(i, j)); } } return tab; }

//RowArray.h

#ifndef ROWARAY_H #define ROWARAY_H

#include "AbsRow.h"

class RowAray :public AbsRow { public: RowAray(unsigned int); virtual ~RowAray(); int getSize()const { return size; } int getData(int i)const { if (i >= 0 && i

#endif /* ROWARAY_H */

//RowAray.cpp

#include #include "RowAray.h"

RowAray::RowAray(unsigned int n) { this->size = n; rowData = new int[n]; }

RowAray::~RowAray() { delete[]rowData; }

void RowAray::setData(int i, int r) { this->rowData[i] = r; }

//AbsTabl.h

#ifndef ABSTABL_H #define ABSTABL_H

#include "RowAray.h"

class AbsTabl { protected: int szRow; int szCol; RowAray **columns; public: virtual int getSzRow()const = 0; virtual int getSzCol()const = 0; virtual int getData(int, int)const = 0; };

#endif /* ABSTABL_H */

//AbsRow.h

#ifndef ABSROW_H #define ABSROW_H

class AbsRow { protected: int size; int *rowData; public: virtual int getSize()const = 0; virtual int getData(int)const = 0; };

#endif /* ABSROW_H */

CORRECT OUTPUTimage text in transcribed

THE CURRENT OUTPUT (I added a system pause in order to stop the program from crashing so I could screen shot the current output of the program):

image text in transcribed

Abstracted and Polymorphic Print Table 1 size is [row, col]- [3,4 87 92 81 44 16 72 54 39 32 62 8669 Copy Constructed Table 2 size is [row, col]-[3,4] 87 92 81 44 16 72 54 39 32 62 86 69 Operator Overloaded Table 3 size is [row, col][3, 4] 174 184 162 88 32 144 108 78 64 124 172 138 RUN SUCCESSFUL (total time: 873ms) Abstracted and Polymorphic Print Table 1 size is [row, col] [3,4] 34 27 96 21 10 50 51 26 60 49 45 76 Copy Constructed Table 2 size is [row,col] - [3,4] 34 27 96 21 10 50 51 26 60 49 45 76 Operator Overloaded Table 3 size is [row, col] [3,4] Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

AutoCAD Database Connectivity

Authors: Scott McFarlane

1st Edition

0766816400, 978-0766816404

More Books

Students also viewed these Databases questions

Question

Describe new developments in the design of pay structures. page 475

Answered: 1 week ago