C++ inheritance and polymorphism 1- Implement an abstract class Arrays that has
ID: 3909399 • Letter: C
Question
C++ inheritance and polymorphism
1- Implement an abstract class Arrays that has the following pure virtual functions typel), that returns the type of the array (1-dimensional or 2-dimensional). i) sie0, that returns the number of elements in the array (e.g. (4,6) if we have a 2 dimensional array of size 4 by 6). Part A Implement the subclass onebimension for one-dimensional arrays. It must have one data member: a pointer to an array of integers Besides implementing the methods typel) and size() defined in the base class, you have to write code for the following functions a- A constructor that takes zero or one parameter (i.e. the length of the array The default size may be set to 5) b- A destructor that simply deletes the array C-number0Elements(): returns the number of elements stored so far in the array d- Insert): method to add an element into the array in case it is not full e- Search0): method to search for an element and returns the position of that element in case it was found f- Max): returns the maximum value in the arrayExplanation / Answer
//============================================================================
// Name : Arrays.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
// C++ code to implement inheritance and polymorphism
#include <iostream>
#include <string>
using namespace std;
// abstract class Arrays
class Arrays{
public:
// pure virtual functions
virtual string type()=0;
virtual string size()=0;
};
// A
class OneDimension : public Arrays
{
int *arr; // pointer to array of integers
int length; // length of array
int numElements; // number of elements occupied
public:
OneDimension(int length=5)
{
this->length = length;
arr = new int[length];
numElements=0;
}
~OneDimension()
{
delete[] arr;
}
int numberOfElements()
{
return numElements;
}
void Insert(int num)
{
if(numberOfElements() < length)
{
arr[numElements] = num;
numElements++;
}
}
int Search(int num)
{
for(int i=0;i<numberOfElements();i++)
if(arr[i] == num)
return i;
return -1;
}
int Max()
{
if(numberOfElements() > 0)
{
int max = arr[0];
for(int i=1;i<numberOfElements();i++)
{
if(arr[i] > max)
max = arr[i];
}
return max;
}
return -1;
}
string type()
{
return "1-dimension";
}
string size()
{
return to_string(length); // convert int to string
}
};
// B
class Matrices : public Arrays{
int **arr; // pointer to two-dimensional array of integers
int rows,cols; // dimensions of the matrix
public:
Matrices(int numRows=3,int numCols=3)
{
rows=numRows;
cols = numCols;
arr = new int*[rows];
for(int i=0;i<rows;i++)
arr[i] = new int[cols];
}
~Matrices()
{
for(int i=0;i<rows;i++)
delete[] arr[i];
delete [] arr;
}
void setMatrix()
{
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
cout<<" Enter element ("<<i<<","<<j<<") : ";
cin>>arr[i][j];
}
}
}
void print()
{
for(int i=0;i<rows;i++)
{
cout<<" ";
for(int j=0;j<cols;j++)
{
cout<<arr[i][j]<<" ";
}
}
}
Matrices operator+(const Matrices &m1)
{
if(rows == m1.rows && cols == m1.cols)
{
Matrices m(rows,cols);
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
m.arr[i][j] = arr[i][j] + m1.arr[i][j];
return m;
}
Matrices m(0,0);
return m;
}
Matrices operator-(const Matrices &m1)
{
if(rows == m1.rows && cols == m1.cols)
{
Matrices m(rows,cols);
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
m.arr[i][j] = arr[i][j] - m1.arr[i][j];
return m;
}
Matrices m(0,0);
return m;
}
Matrices operator*(const Matrices &m1)
{
if(cols == m1.rows)
{
Matrices m(rows,m1.cols);
for(int i=0;i<rows;i++)
for(int j=0;j<m1.cols;j++){
m.arr[i][j] = 0;
for(int k=0;k<cols;k++)
m.arr[i][j] += arr[i][k] + m1.arr[k][j];
}
return m;
}
Matrices m(0,0);
return m;
}
string type()
{
return "2-dimensional";
}
string size()
{
return("(" + to_string(rows)+","+to_string(cols)+")");
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.