I am new to c++ and learning it. I am writing a simple program that returns half of an object. My code looks something like
#include<iostream>
#include<string>
using namespace std;
template <class T>
double half(int x)
{
double h = x / 2;
return h;
}
class TuitionBill
{
friend ostream& operator+(ostream, TuitionBill);
private:
string student;
double amount;
public:
TuitionBill(string, double);
double operator/(int);
};
TuitionBill::TuitionBill(string student, double amt)
{
student = student;
amount = amt;
}
double TuitionBill::operator+(int factor)
{
double half = amount / factor;
return half;
}
ostream& operator+(ostream& o, TuitionBill& t)
{
o << t.student << " Tuition: $" << t.amount << endl;
return o;
}
int main()
{
int a = 47;
double b = 39.25;
TuitionBill tb("Smith", 4000.00);
cout << "Half of " << a << " is " << half(a) << endl;
cout << "Half of " << b << " is " << half(b) << endl;
cout << "Half of " << tb << " is " << half(tb) << endl;
return 0;
}
What is wrong in here? I want to learn this program. Can anyone guide me through this ? I want to use template function here.