Hi.
I have a question. How do I allocate dynamic memory in istream &operator>>() (operator overloading)? I need to allocate int & char dynamic memory when the program runs. I am an amateur in C++, and if anyone could help me, with the example, it would be great!
My file is as follows:
//header file
#ifndef Money_h
#define Money_h
#include <iostream>
#include <string>
using namespace std;
class Money{
protected:
string billName;
int billCode;
long int refNum;
int accNum;
char *accName;
char *address;
int *startDate;
int *endDate;
int *dueDate;
public:
Money();
Money(int);
Money(int,int,int,int,int,int);
~Money();
Money::Money(int bCode, int rNum, int aNum, int sDate, int eDate, int dDate)
{billCode=bCode, refNum=rNum, accNum=aNum, startDate=&sDate, endDate=&eDate, dueDate=&dDate;}
istream &operator>> (istream &is, Money& b){
Money tmp;
tmp.accName=new char[100];//trying to allocate it this way. Is it
tmp.address=new char[100];//possible?
tmp.startDate=new int[100];//
tmp.endDate=new int[100];//
tmp.dueDate=new int[100];//
cout << "Biller name: ";
is >> b.billName;
cout << "Biller code: ";
is >> b.billCode;
cout << "Reference: ";
is >> b.refNum;
cout << "Account number: ";
is >> b.accNum;
cout << "Account name: ";
is >> tmp.accName;
cout << "Address: ";
is >> tmp.address;
cout << "Start date: ";
is >> *tmp.startDate;
cout << "End date";
is >> *tmp.endDate;
cout << "Due date";
is >> *tmp.dueDate;
b=tmp;
return is;
};