我试图用我的班级做一个动态数组。你看我想用他们的信息输入2个用户,然后显示它。我设置了整个类,输入和输出运算符已经过载。如何在类中构建动态数组并向其输入数据然后显示它?我可能不得不在友元函数中使用已经重载的[]运算符?
这是班级:
#pragma once
#include<iostream>
#include<string>
using namespace std;
template <class T>
class Payroll {
private:
int empNumber;
string name;
T hours;
T payRate;
T grossPay;
T *aptr;
int arraySize;
public:
Payroll(int size);
Payroll();
Payroll(const Payroll & aPayroll);
~Payroll();
//Mutators
void setEmpNumber(int empNumber);
void setName(string name);
void setHours(T hours);
void setPayRate(T payRate);
void setGrossPay(T grossPay);
//Accessors
int getEmpNumber()const;
string getName()const;
T getHours()const;
T getPayRate()const;
T getGrossPay()const;
friend ostream & operator << <>(ostream & output, const Payroll & aPayroll);
friend istream & operator >> <>(istream & input, Payroll & aPayroll);
T &operator[](int);
};
template <class T>
Payroll<T>::Payroll(int size) {
arraySize = size;
aptr = new T[size];
for (int i = 0; i < arraySize; i++)
aptr[i] = T();
}
template <class T>
Payroll<T>::Payroll() : empNumber(0), name(""), hours(0.00), payRate(0.00), grossPay(0.00) {}
template <class T>
Payroll<T>::Payroll(const Payroll & aPayroll) : empNumber(aPayroll.empNumber), name(aPayroll.name), hours(aPayroll.hours),
payRate(aPayroll.payRate), grossPay(aPayroll.grossPay) {
arraySize = aPayroll.arraySize;
aptr = new T[arraySize];
for (int i = 0; i < arraySize; i++)
aptr[i] = aPayroll[i];
}
template <class T>
Payroll<T>::~Payroll() {
if (arraySize > 0)
delete[] aptr;
}
//Mutators
template <class T>
void Payroll<T>::setEmpNumber(int empNumber) {
this->empNumber = empNumber;
}
template <class T>
void Payroll<T>::setName(string name) {
this->name = name;
}
template <class T>
void Payroll<T>::setHours(T hours) {
this->hours = hours;
}
template <class T>
void Payroll<T>::setPayRate(T payRate) {
this->payRate = payRate;
}
template <class T>
void Payroll<T>::setGrossPay(T grossPay) {
this->grossPay = grossPay;
}
//Accessors
template <class T>
int Payroll<T>::getEmpNumber()const {
return(this->empNumber);
}
template <class T>
string Payroll<T>::getName()const {
return(this->name);
}
template <class T>
T Payroll<T>::getHours()const {
return(this->hours);
}
template <class T>
T Payroll<T>::getPayRate()const {
return(this->payRate);
}
template <class T>
T Payroll<T>::getGrossPay()const {
return(this->grossPay);
}
template <class T>
ostream & operator << (ostream & output, const Payroll<T> & aPayroll) {
for (int i = 0; i < aPayroll.arraySize; i++) {
output << "Employee Number: " << aPayroll.empNumber;
output << "\nEmployee Name: " << aPayroll.name;
output << "\nHours Worked: " << aPayroll.hours;
output << "\nPay Rate: " << aPayroll.payRate;
output << "\nGross Pay: " << aPayroll.grossPay << endl;
}
return output;
}
template <class T>
istream & operator >>(istream & input, Payroll<T> & aPayroll) {
for (int i = 0; i < aPayroll.arraySize; i++) {
cout << "Please enter Employee Number: ";
input >> aPayroll.empNumber;
cout << "Please enter Employee Name: ";
input >> aPayroll.name;
cout << "Please enter Hours Worked: ";
input >> aPayroll.hours;
cout << "Please enter Pay Rate: ";
input >> aPayroll.payRate;
cout << "Please enter Gross Pay: ";
input >> aPayroll.grossPay;
cout << endl;
}
return input;
}
template <class T>
T &Payroll<T>::operator[](int position) {
return aptr[position];
}
这是主要的:
int main(){
Payroll <int> employee(2);
cin >> employee;
cout << employee;
return 0;
}