Hello i've overloaded the insertion and Extraction operators. when i run my program the Insertion does not seem to output the values although the Extraction is putting the values into the class.
Its seems like the insertions view of the instance has no values in it.
Main
/ Input Poly
cout << "Input p1: " << endl;
Polynomial P1;
cin >> P1;
// Output Poly
cout << "p1(x) = " << P1 << '\n' << endl;
Class function
//Insertion
ostream& operator<<(ostream& os, Polynomial Poly){
for (int i=0; i < Poly.polyNum; i++) {
os << Poly.poly[i] << " x^" << i;
if(i != Poly.polyNum - 1){
os << " + ";
}
}
return os;
}
//Extraction
istream& operator>>(istream& is, Polynomial Poly){
int numP = 0;
int * tempP;
is >> numP;
tempP = new int [numP+1];
for (int i=0; i < numP; i++) {
is >> tempP[i];
}
Poly.polyNum = numP;
Poly.poly = new int[Poly.polyNum +1];
for (int i=0; i < Poly.polyNum; i++) {
Poly.poly[i] = tempP[i];
}
return is;
}
istream& operator>>(istream& is, Polynomial Poly)
should be
istream& operator>>(istream& is, Polynomial& Poly)
What you're doing now is simply changing the members of a copy of your object:
Polynomial P1;
cin >> P1;
P1 is not modified after this.
Related
I am learning c++ and made my code of Matrix generating Class with operations.
However, When I try to delete the two matrices which input by user after the whole works, it pops up the message:
"Exception thrown at 0x0F4DBF9B (ucrtbased.dll) in Matrix 1.3.exe: 0xC0000005: Access violation reading location 0xDDDDDDCD"
at "(45)the delete[] arr[i];" line, which is in the destructor.
I tried to remove the brackets, but as I need those brackets to remove arrays, it also did not work.
Do you have any idea whats going wrong here?
#include <iostream>
#include <iomanip>
using namespace std;
class Matrix
{
private:
int row, col;
int** arr;
public:
Matrix(int, int);
~Matrix();
int getRow() const;
int getCol() const;
int** getMatrixArr() const;
friend Matrix* operator+(const Matrix&, const Matrix&);
friend Matrix* operator-(const Matrix&, const Matrix&);
friend Matrix* operator*(const Matrix&, const Matrix&);
friend Matrix* operator*(const Matrix&, int);
friend ostream& operator<<(ostream&, const Matrix*);
friend istream& operator>>(istream&, const Matrix&);
};
//construct and destruct--------------------------------------------------------------------------------------------------
Matrix::Matrix(int row, int col)
{
this->row = row;
this->col = col;
arr = new int*[row];
for (int i = 0; i < row; i++)
{
arr[i] = new int[col];
}
}
Matrix::~Matrix()
{
for (int i = 0; i < row; i++)
{
delete[] arr[i];
}
delete[] arr;
}
//getters------------------------------------------------------------------------------------------------------------------
int Matrix::getRow() const
{
return row;
}
int Matrix::getCol() const
{
return col;
}
int** Matrix::getMatrixArr() const
{
return arr;
}
//operation methods(OpOv)----------------------------------------------------------------------------------------------------------
Matrix* operator+(const Matrix& m, const Matrix& n)
{
Matrix* sum = new Matrix(m.row, m.col);
cout << "calculating..." << endl;
for (int i = 0; i <m.row; i++)
{
for (int j = 0; j <m.col; j++)
{
cout << setw(3) << m.arr[i][j] << "+" << n.arr[i][j];
sum->arr[i][j] = m.arr[i][j] + n.arr[i][j];
}
cout << endl;
}
return sum;
}
Matrix* operator-(const Matrix& m, const Matrix& n)
{
Matrix* sum = new Matrix(m.row, m.col);
cout << "caluclating..." << endl;
for (int i = 0; i < m.row; i++)
{
for (int j = 0; j < m.col; j++)
{
cout << setw(3) << m.arr[i][j] << "-" << n.arr[i][j];
sum->arr[i][j] = m.arr[i][j] - n.arr[i][j];
}
cout << endl;
}
return sum;
}
Matrix* operator*(const Matrix& m, const Matrix& n)
{
Matrix* sum = new Matrix(m.row, n.col);
cout << "calculating..." << endl;
for (int i = 0; i < m.row; i++)
{
for (int j = 0; j < n.col; j++)
{
sum->arr[i][j] = 0;
}
}
for (int i = 0; i < m.row; i++)
{
for (int j = 0; j < n.col; j++)
{
for (int t = 0; t < m.col; t++)
{
cout << setw(3) << "+" << m.arr[i][t] << "x" << n.arr[t][j];
sum->arr[i][j] += m.arr[i][t] * n.arr[t][j];
}
}
cout << endl;
}
return sum;
}
Matrix* operator*(const Matrix& m, int num)
{
Matrix* sum = new Matrix(m.row, m.col);
cout << "calculating..." << endl;
for (int i = 0; i < m.row; i++)
{
for (int j = 0; j < m.col; j++)
{
cout << setw(3) << m.arr[i][j] << "x" << num;
sum->arr[i][j] = m.arr[i][j] * num;
}
cout << endl;
}
return sum;
}
// input & output ---------------------------------------------------------------------------------------------------------------------
istream& operator>>(istream& is, const Matrix& m)
{
cout << "Enter the values for the Matrix (expecting: " << m.row * m.col << "): ";
for (int i = 0; i < m.row; i++)
{
for (int j = 0; j < m.col; j++)
{
is >> m.arr[i][j];
}
}
return is;
}
ostream& operator<<(ostream& os, const Matrix* m)
{
cout << "result: " << endl;
for (int i = 0; i < m->row; i++)
{
for (int j = 0; j < m->col; j++)
{
os << setw(3) << m->arr[i][j];
}
cout << endl;
}
return os;
}
//main-------------------------------------------------------------------------------------------------------------------------------------
int main()
{
int rowNum1, colNum1;
cout << "what is the row of the Matrix 1?: " << endl;
cin >> rowNum1;
cout << "What is the column for the Matrix 1?: " << endl;
cin >> colNum1;
Matrix m1(rowNum1, colNum1);
cin >> m1;
int rowNum2, colNum2;
cout << "what is the row of the Matrix 2?: " << endl;
cin >> rowNum2;
cout << "What is the column for the Matrix 2?: " << endl;
cin >> colNum2;
Matrix m2(rowNum2, colNum2);
cin >> m2;
int choice;
do
{
cout << "Now, what operation do you want to use?" << endl;
cout << "1) addition, 2) Sub action, 3)Multiplication, 4) scalar multiplication 5) quit" << endl << ":";
cin >> choice;
if (choice == 1)
{
if (m1.getRow() != m2.getRow() || m1.getCol() != m2.getCol())
{
cout << "The number of rows or columns of both Matrices are not same, you cannot add them together." << endl;
return 0;
}
else
{
Matrix * result = (m1 + m2);
cout << result << endl;
}
}
else if (choice == 2)
{
if (m1.getRow() != m2.getRow() || m1.getCol() != m2.getCol())
{
cout << "The number of rows or columns of both Matrices are not same, you cannot add them together." << endl;
return 0;
}
else
{
Matrix * result = (m1 - m2);
cout << result << endl;
}
}
else if (choice == 3)
{
if (m1.getCol() != m2.getRow())
{
cout << "Your first Matrix's number of columns and the second Matrice's number of rows are not accorded." << endl;
return 0;
}
else
{
Matrix* result = (m1 * m2);
cout << result << endl;
}
}
else if (choice == 4)
{
int value;
cout << "What is the integer value for the multiplication?: ";
cin >> value;
int MatCho;
cout << "First Matrix or Second Matrix(1 or 2)?: ";
cin >> MatCho;
if (MatCho == 1)
{
Matrix* result = (m1 * value);
cout << result << endl;
}
else if (MatCho == 2)
{
Matrix* result = (m2 * value);
cout << result << endl;
}
else
{
cout << "invalid input" << endl;
}
}
else if (choice == 5)
{
break;
}
else
{
cout << "Invalid input" << endl;
}
} while (choice != 5);
m1.~Matrix();
m2.~Matrix();
return 0;
}
Two issues here:
Never call destructor explicitly like you do with m1 and m2 here. Their destructors will be called automatically at the end of the main function in any case. So for you the destructors will run twice. On the second run, the array of pointers has already been deleted. But row, col and the pointer still has their old values and will try to access and delete already freed memory.
All your operations you do on the matrices will leak memory. You are using new inside the operator functions but never deleting the result. It is a bad habit expecting the user of a function to delete memory you have allocated. Return by value instead. This will actually not infer any performance loss, if you implement a move constructor (http://www.learncpp.com/cpp-tutorial/15-3-move-constructors-and-move-assignment/)
Remove the manual call to the destructors. As they're stack-allocated, the C++ compiler you use will handle them when you exit the main function, even if you already called them (which is what is happening).
I have seen several articles regarding something similar but none of them seem to help me. I would appreciate if someone could look at my code and tell me what I am doing wrong here. My teacher is not helping much. I commented out lines 65 - 75 in the driver because i couldn't get it to compile with these, will need help with this as well
ArrayDriver.cpp
#include <iostream>
#include "myArray.h"
using namespace std;
int main()
{
myArray<int> list1(5);
myArray<int> list2(5);
int i;
cout << "list1 : ";
for (i = 0 ; i < 5; i++)
cout << list1[i] <<" ";
cout << endl;
cout << "Enter 5 integers: ";
for (i = 0 ; i < 5; i++)
cin >> list1[i];
cout << endl;
cout << "After filling list1: ";
for (i = 0 ; i < 5; i++)
cout << list1[i] <<" ";
cout<< endl;
list2 = list1;
cout << "list2 : ";
for (i = 0 ; i < 5; i++)
cout << list2[i] <<" ";
cout<< endl;
cout << "Enter 3 elements: ";
for (i = 0; i < 3; i++)
cin >> list1[i];
cout << endl;
cout << "First three elements of list1: ";
for (i = 0; i < 3; i++)
cout << list1[i] << " ";
cout << endl;
myArray<int> list3(-2, 6);
cout << "list3: ";
for (i = -2 ; i < 6; i++)
cout << list3[i] <<" ";
cout<< endl;
list3[-2] = 7;
list3[4] = 8;
list3[0] = 54;
list3[2] = list3[4] + list3[-2];
cout << "list3: ";
for (i = -2 ; i < 6; i++)
cout << list3[i] <<" ";
cout<< endl;
/*
if (list1 == list2)
cout << " list 1 is equal to list2 " << endl;
else
cout << " list 1 is not equal to list2" << endl;
if (list1 != list2)
cout << " list 1 is not equal to list2 " << endl;
else
cout << " list 1 is equal to list2" << endl;
*/
//10% EXTRA CREDIT: UNCOMMENT CODE IF YOU'VE SUCCESSFULLY IMPLEMENTED THE FOLLOWING:
//cout << list1<< (list1 == list2 ? " is equal to" : " not equal to ") << list2 << endl;
//cout << list1<< (list1 != list2 ? " not equal to" : " is equal to ") << list2 << endl;
return 0;
}
myArray.h
#ifndef MYARRAY_H
#define MYARRAY_H
//#pragma once
#include <iostream>
//#include <assert.h>
//#include <iomanip>
//#include <string>
using namespace std;
template <class DataType>
class myArray
{
//overload for <<
//friend ostream& operator<<(ostream& out, myArray<DataType>& arr);
//overload for >>
friend istream& operator>>(istream& in, myArray<DataType>& arr);
public:
//myArray();
myArray(int size);
myArray(int start, int end);
//~myArray();
//overload []
DataType& operator[](int i);
//overload == operator
friend bool operator==(myArray<DataType> &arr1, myArray<DataType> &arr2);
//overload != operator
friend bool operator!=(myArray<DataType> &arr1, myArray<DataType> &arr2);
//overload = opertator
myArray<DataType> &operator=(const myArray<DataType> &rhs)
{
if (myDataType != NULL)
delete[]myDataType;
myDataType = new DataType[rhs.arraySize];
arraySize = rhs.arraySize;
for (int i = 0; i < arraySize; i++)
myDataType[i] = rhs.myDataType[i];
return *this;
}
//function
void SetNULL();
protected:
int startIndex;
int endIndex;
int arraySize;
//template
DataType *myDataType;
};
#endif
template <class DataType>
DataType& myArray<DataType>::operator[](int i)
{
if (i > arraySize)
{
cout << "Array out of bounds: " << endl;
}
else if (startIndex == 0)
{
return myDataType[i];
}
else
{
return myDataType[(startIndex + (i - 1))];
}
}
template <class DataType>
myArray<DataType>::myArray(int size) :
arraySize(size), startIndex(0), endIndex(size)
{
myDataType = new DataType[arraySize];
SetNULL();
}
template <class DataType>
myArray<DataType>::myArray(int start, int end) :
startIndex(start), endIndex(end)
{
if (start > end)
{
cout << "Invalid start position: " << endl;
}
else
{
arraySize = end - start;
}
myDataType = new DataType[arraySize];
SetNULL();
}
template <class DataType>
void myArray<DataType>::SetNULL()
{
for (int i = startIndex; i < endIndex; i++)
{
myDataType[i] = (DataType)0;
}
}
//overload == operator
template <class DataType>
bool operator==(myArray<DataType> &arr1, myArray<DataType> &arr2)
{
bool testBool = true;
for (int i = 0; i < arraySize; i++)
{
if (arr1[i] != arr2[i])
{
testBool = false;
}
}
return testBool;
}
//overload != operator
template <class DataType>
bool operator!=(myArray<DataType> &arr1, myArray<DataType> &arr2)
{
bool testBool = true;
for (int i = 0; i < arraySize; i++)
{
if (arr1[i] == arr2[i])
{
testBool = false;
}
}
return testBool;
}
//overload >> operator
template <class DataType>
istream& operator>> (istream &in, myArray<DataType> &aList)
{
for (int i = 0; i < aList.arraySize; i++)
in >> aList.list[i];
return in;
}
That's why not ignoring warnings (or using debugger) is helpful. My compiler showed the issue (via warnings), which should be the culprit of your problem.
In function:
myArray<DataType> &operator=(const myArray<DataType> &rhs)
{
if (myDataType = NULL)
delete[]myDataType;
myDataType = new DataType[rhs.arraySize];
arraySize = rhs.arraySize;
for (int i = 0; i < arraySize; i++)
myDataType[i] = rhs.arraySize;
return *this;
}
UPDATE: You are also assigning the array size as elements of your newly created copy. Copy actual elements.
UPDATE #2: As pointed out by fellow commenters, you also forgot to check for self-assignment. Check if the array pointer of your current array is equal to the one you are assigning.
you are doing an assignment instead of comparison in the first if statement. Consider doing this:
myArray<DataType> &operator=(const myArray<DataType> &rhs)
{
if (myDataType == rhs.myDataType) //CHECK IF THE ARRAY IS THE SAME (self-assignment case).
return *this;
// Missing '!' character here!!!
if (myDataType != NULL)
delete[]myDataType;
myDataType = new DataType[rhs.arraySize];
arraySize = rhs.arraySize;
for (int i = 0; i < arraySize; i++)
myDataType[i] = rhs.myDataType[i]; // COPY ACTUAL ELEMENTS INSTEAD OF WRITTING ARRAY SIZE.
return *this;
}
And, to avoid such issues in the future (if your compiler doesn't pick up on issues like this, consider doing comparisons, with constant values, like this:
if (NULL != myDataType)
delete[]myDataType;
And then your compiler will, no matter what, pick up on the issue, since it can't assign to a constant value.
UPDATE #3: Also, as I noticed, you are also allowing for arrays to have user defined start/end indexes. But, you would need to rethink your current approach of calculating the index at which to get the elements in such a case (where startIndex != 0). You are getting the position of element with formula: 'startIndex + (i - 1)'. But in your test case, in list3, where the range of indexes is [-2; 6), and you start your for loop, the initial index equates to -2 + (-2 - 1) = -2 - 3 = -5. I am not sure why your code isn't crashing here, but it should.
Also, to implement the comparisons operators, check if the size is the same at first, since if one of the arrays is smaller, and you are iterating through the indexes of the larger one, it should crash, when you will access the non-existing index (and to be honest, if sizes of arrays differ - they are not equal, are they?).
I am having problem to print out the vector that hold my person info.
struct PersonInfo{
string name;
vector<string> phones;
};
int main(){
string line, word;
vector<PersonInfo> people;
while(getline(cin, line)){
PersonInfo info;
istringstream record(line);
record >> info.name;
while(record >> word)
info.phones.push_back(word);
people.push_back(info);
}
for(auto i = people.begin(); i != people.end(); i++)
cout << people << endl;
return 0;
}
error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
cout << people << endl;
^
You need to define << operator for your custom struct. Define it after the struct like this
ostream & operator<<(ostream & out, const PersonInfo & p) {
out << p.name << endl;
copy(p.phones.begin(), p.phones.end(), ostream_iterator<string>(out, " "));
return out;
}
and correct your print statement
for (auto i : people)
cout << i << endl;
See working demo at http://ideone.com/uLSLHY.
There are several options:
Using iterator
for(auto i = people.begin(); i != people.end(); i++)
{
cout << i->name<< endl;
for ( auto l= i->phones.begin(); l != i->phones.end(); ++l)
std::cout<< *l<<"\n";
}
using range loop
for ( auto & val: people)
{
std::cout<<val.name<<"\n";
for ( auto & phone: val.phones)
std::cout<<phone;
}
You can use vector index, left to you as home work
for(int i = 0 ; i < people.size(); i++){
cout << people[i].name<< endl;
for(int j = 0 ;j<people[i].phones.size() ; j++)
cout<< people[i].phones[j]<<" ";
cout<<endl;
}
Here in using the first loop we are iterating until the size of vector people and as each of the content in vector people has another vector named phones so we need to iterate that one too. we did this in the second loop.
So if people[0] has phone size 10 then we will iterate
people[0].phones[10] and so on..
I am currently writing a "Polynomial" class in order to practice Operator Overloading. I have successfully overloaded the stream extraction and insertion operators, but I am having some trouble with the "+" operator.
My class has a private pointer that proceeds to create an array in the constructor. I understand how one would overload the "+" operator with a complex number class, for example, but I'm getting confused with this program.
Guidance in finding a solution would be greatly appreciated.
Thank you.
#include<iostream>
#include<stdexcept>
using namespace std;
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
class Polynomial
{
friend istream& operator>>(istream& in, Polynomial& p);
friend ostream& operator<<(ostream& out, const Polynomial& p);
public:
Polynomial(int = 10);
~Polynomial();
void assignExponent();
Polynomial operator+(const Polynomial& other);
private:
int SIZE;
int *exponents;
int *polyPtr; //***************
};
#endif
//CONSTRUCTOR
Polynomial::Polynomial(int arraySize)
{
if(arraySize > 0)
SIZE = arraySize;
else
{
cout << "Array size must be greater than 0. Program will now "
<< "Terminate..." << endl;
system("pause");
exit(0);
}
polyPtr = new int[SIZE]; //*********************
exponents = new int[SIZE];
for(int i = 0; i<SIZE; i++)
polyPtr[i] = 0;
assignExponent();
};
//DESTRUCTOR
Polynomial::~Polynomial() //******************
{
delete [] polyPtr;
};
//STREAM INSERTION
istream& operator>>(istream& in, Polynomial& p)
{
for(int i = 0; i<p.SIZE; i++)
{
in >> p.polyPtr[i]; //*************
}
return in;
};
//STREAM EXTRACTION
ostream& operator<<(ostream& out, const Polynomial& p)
{
int exponent;
for(int i = 0; i<p.SIZE; i++)
{
exponent = (p.SIZE - 1) - i;
if(p.polyPtr[i] != 1)
{
if(exponent > 0 && exponent != 1)
out << p.polyPtr[i] << "x^" << exponent << " + ";
if(exponent == 1)
out << p.polyPtr[i] << "x" << " + ";
if(exponent == 0)
out << p.polyPtr[i];
}
//In order to not display coefficient if = 1
else
{
if(exponent > 0 && exponent != 1)
out << "x^" << exponent << " + ";
if(exponent == 1)
out << "x" << " + ";
if(exponent == 0)
out << p.polyPtr[i];
}
}
return out;
};
//Assigns a value for exponent
void Polynomial::assignExponent()
{
for(int i = 0; i<SIZE; i++)
{
exponents[i] = (SIZE - 1) - i;
}
};
//OVERLOAD OF + OPERATOR
Polynomial Polynomial::operator+(const Polynomial& other)
{
Polynomial sum(SIZE);
int difference;
//If the first polynomial is larger
if (SIZE > other.SIZE)
{
difference = SIZE - other.SIZE;
for(int i = 0; i<SIZE; i++)
{
if(i - difference < 0)
sum.polyPtr[i] = polyPtr[i];
else
{
sum.polyPtr[i] = polyPtr[i] +
other.polyPtr[i - difference];
}
}
}
//If the second polynomial is larger **PROBLEM**
if(other.SIZE > SIZE)
{
difference = other.SIZE - SIZE;
for(int i = 0; i<other.SIZE; i++)
{
if(i - difference < 0)
sum.polyPtr[i] = other.polyPtr[i];
else
{
sum.polyPtr[i] = other.polyPtr[i] +
polyPtr[i - difference];
}
}
}
//If the polynomials are equal
if(SIZE == other.SIZE)
{
for(int i = SIZE-1; i >= 0; i--)
{
sum.polyPtr[i] = polyPtr[i] + other.polyPtr[i];
}
}
return sum;
};
int main()
{
int polySize;
//User enters a size for the first & second polynomial
cout << "Enter a size for the first polynomial: ";
cin >> polySize;
Polynomial pOne(polySize);
cout << "\nEnter a size for the second polynomial: ";
cin >> polySize;
Polynomial pTwo(polySize);
//User enters in values (Overload of >> operator
cout << "\n\nEnter in values for the first polynomial, "
<< "in the format - (x x x x): " << endl;
cin >> pOne;
cout << "\nEnter in values for the second polynomial, "
<< "in the format - (x x x x): " << endl;
cin >> pTwo;
//Overload << operator for output
cout << "\nPolynomial 1 is: " << pOne << endl
<< "Polynomial 2 is: " << pTwo << endl;
Polynomial pThree = pOne + pTwo;
cout << "\nAfter being added together, the new polynomial is: "
<< pThree << endl;
system("pause");
}
I have updated my current code because I did not think opening another question would be the best way to go. Anyway, in trying to line up my polynomial's to be added, I've partially succeeded. The only problem that arises is when the second polynomial, pTwo, is larger than the first. I've labeled the section of code, PROBLEM. Thanks in advance.
I guess the + should write like below:
class Polynomial
{
// ...
Polynomial operator+(const Polynomial& other);
// ...
};
Polynomial Polynomial::operator+(const Polynomial& other)
{
Polynomial sum(SIZE);
for(int i = 0; i< SIZE; i++)
{
sum.polyPtr[i] = polyPtr[i] + other.polyPtr[i];
}
return sum;
}
Hello I have problems calling the void initialize() function in my code. I am supposed to get an array that looks like this
//cccccccccccccccccccccccccc
cxxxxxxxxxxxxxxxxxxxxxxxxc
cxxxxxxxxxxxxxxxxxxxxxxxxc
cxxxxxxxxxxxxxxxxxxxxxxxxc
cxxxxxxxxxxxxxxxxxxxxxxxxc
cxxxxhhhhhhhxxxxxxxxxxxxxc
cxxxxhhhhhhhxxxxxxxxxxxxxc
cxxxxhhhhhhhxxxxxxxxxxxxxc
cxxxxhhhhhhhxxxxxxxxxxxxxc
cxxxxxxxxxxxxxxxxxxxxxxxxc
cccccccccccccccccccccccccc//
where the array is defined in Initialize() and the variables come from FIN , SteamPipe and GridPT classes. My code is giving me the first set of answers before the array but when it gets to the array I only get the symbol '?' that is what it's been designated in the class GridPT.
I think that the way I am calling the void function is wrong or there is something wrong passing the variables? I already tried taking out the const but still the same.
here is my code .h file
#include<iostream>
#include<istream>
#include<cmath>
#include<cstdlib>
using namespace std;
const int maxFinHeight = 100;
const int maxFinWidth = 100;
class Steampipe
{
private:
int height, width;
double steamTemp;
public:
Steampipe (int H = 0, int W = 0, double sT = 0.0)
{
height = H;
width = W;
steamTemp = sT;
}
// Access function definitions
int getWidth()const{return width;};
int getHeight()const{return height;};
double getSteamTemp()const{return steamTemp;};
void setWidth(int dummysteamwidth) {width = dummysteamwidth;};
void setHeight(int dummysteamheight){height = dummysteamheight;};
void setSteamTemp(double dummysteamtemp){steamTemp = dummysteamtemp;};
// Access function definitions
friend istream& operator>>(istream& , Steampipe& ); // YOU MUST DEFINE
friend ostream& operator<<(ostream& , const Steampipe& ); // YOU MUST DEFINE
};
class GridPt
{
private:
double temperature;
char symbol;
public:
GridPt(double t = 0.0, char s = '?')
{
temperature = t;
symbol = s;
}
// Access function definitions
double getTemperature()const {return temperature;};
char getsymbol() const {return symbol;};
void setTemperature (double T=0) {T=temperature;}
void setsymbol (char S='?'){S=symbol;}
};
class Fin
{
private:
int width, height; //width and height of fin
int pipeLocationX, pipeLocationY; //grid location of lower left corner of steampipe
double boundaryTemp; //temperature of fin surroundings
Steampipe Pipe; //steampipe object - COMPOSITION
GridPt GridArray[maxFinHeight][maxFinWidth]; // array of GridPts - COMPOSITION
public:
int getwidth() {return width;}
int getheight() {return height;}
int getpipeLocationX(){return pipeLocationX;}
int getpipeLocationY(){return pipeLocationX;}
double get_boundarytemp(){return boundaryTemp;}
void setwidth (int w){w=width;}
void setheight (int h){h=height;}
friend istream& operator>>(istream& , Fin& ); // YOU MUST DEFINE
friend ostream& operator<<(ostream& , const Fin& ); // YOU MUST DEFINE
void initialize(); // YOU MUST DEFINE
};
void Fin::initialize()
{
int j(0) ,i(0);
for ( i = 0; i < height; i++)
{
for ( j = 0; j < width; j++)
{
if ( j == pipeLocationX && i == pipeLocationY
&& i < Pipe.getHeight() + pipeLocationY
&& j < Pipe.getWidth() + pipeLocationX)
{
GridArray [j][i].setTemperature(Pipe.getSteamTemp());
GridArray [j][i].setsymbol('H');
}
else if (j == (width -1) || i == (height -1) || j == 0 || i == 0)
{
GridArray [j][i].setTemperature(boundaryTemp);
GridArray [j][i].setsymbol('C');
}
else
{
GridArray [j][i].setTemperature((Pipe.getSteamTemp() + boundaryTemp)/2);
GridArray [j][i].setsymbol('X');
}
}
}
}
istream &operator >> (istream & in, Fin& f)
{
int ws, hs;
double ts;
char comma;
cout << "Enter the height of the fin (integer) >>> ";
in >> f.height;
cout << "Enter the width of the fin (integer) >>> " ;
in >> f.width;
cout << "Enter the height of the streampipe (integer) >>> ";
in >>hs;
f.Pipe.setHeight(hs);
cout << "Enter the width of the steampipe (integer) >>> ";
in >> ws;
f.Pipe.setWidth(ws);
cout << "Enter coordinates of lower left corner of steampipe (X,Y) >>> ";
in >> f.pipeLocationX >> comma >> f.pipeLocationY;
cout << "Enter the steam temperature (floating point) >>> ";
in >> ts;
f.Pipe.setSteamTemp(ts);
cout << "Enter the temperature around the fin (floating point) >>> ";
in >> f.boundaryTemp;
return in;
}
ostream &operator << (ostream &stream, const Fin& t)
{
int i = 0;
int j = 0;
stream << "The width of the fin is " << t.width << endl;
stream << "The height of the fin is " << t.height << endl;
stream << "The outside temperature is " << t.boundaryTemp << endl;
stream << "The lower left corner of the steam pipe is at "
<< t.pipeLocationX << "," << t.pipeLocationY << endl;
stream << "The steampipe width is " << t.Pipe.getWidth() << endl;
stream << "the steampipe height is " << t.Pipe.getHeight() << endl;
stream << "The temperature of the steam is " << t.Pipe.getSteamTemp() << endl;
for ( i = 0; i < t.height; i++)
{
for ( j = 0; j < t.width; j++)
{
if (j == t.pipeLocationX && i == t.pipeLocationY && i < t.Pipe.getHeight() + t.pipeLocationY && j < t.Pipe.getWidth() + t.pipeLocationX)
{
t.GridArray [j][i].getTemperature();
stream<<t.GridArray [j][i].getsymbol();
}
else if (j == (t.width -1) || i == (t.height -1) || j == 0 || i == 0)
{
t.GridArray [j][i].getTemperature();
stream<<t.GridArray [j][i].getsymbol();
}
else
{
t.GridArray [j][i].getTemperature();
stream<<t.GridArray [j][i].getsymbol();
}
}stream<<endl;
}
int coorx(0),coory(0);char comma;
stream << "Enter the coordinates of the gridpoint of interest (X,Y) >>> "
<< endl;
cin>>coorx>>comma>>coory;
stream<<"The temperature at location ("<<coorx<<","<<coory<<") is "
<< t.GridArray << endl;
return stream;
}
and my driver file cpp is:
#include "pipe.h"
int main()
{
Fin one;
cin >> one;
one.initialize();
cout << one;
return 0;
}
Any suggestions will be very appreciated. Thank you
In your code the following is wrong:
void setTemperature (double T=0) {T=temperature;}
void setsymbol (char S='?'){S=symbol;}
It must be:
void setTemperature (double T=0) {temperature = T;}
void setsymbol (char S='?') { symbol = S;}
As an aside: It is not a good habit to place function definitions in your header (unless it's a template), rather put them in a source-file with just the declarations in the header.
In your code you also use t.GridArray in your output-stream directly:
stream<<t.GridArray;
This will not work.
It is not a function, or a type that the ostream will understand.
Use the i and j variables you have declared and step through each element of the array to print them, same as when you initialize the array.