-8

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.

Green Eagle
  • 227
  • 3
  • 12
  • 2
    What do you mean by "what is wrong"? What is not working for you? What result did you expect and what did you get? – pptaszni Aug 14 '18 at 06:29
  • 2
    Your function template (not "template function") never uses the type `T`. Why is it a template? (If you're new to C++, implementing templates is not a good place to start. Start with chapter one of [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).) – molbdnilo Aug 14 '18 at 06:30
  • 2
    Typos: you wrote `operator+` instead of `operator/`, and also `operator+` instead of `operator<<`. – molbdnilo Aug 14 '18 at 06:32
  • 2
    And in your constructor, you're assigning the arguments to themselves. Read about member initialization in a good C++ book. – molbdnilo Aug 14 '18 at 06:35

1 Answers1

1

There are lots of mistakes in your code. Many of them look like typos, but the serious errors are

Not using the template parameter T in your function signature. This is the reason half(tb) did not compile since your version of half always expected an int.

Not understanding that in your constructor student = student; is just assigning a variable to itself. You can ensure that the class variable gets assignied by prefixing this-> to the name of the class variable (or you can just ensure that the class variable and parameter names are different as with ammount and amt.

You might also check out the difference between assignment (what you are doing in your constructor) and initialisation (what you should be doing in your constructor).

Below is a working version of your code

#include<iostream>
#include<string>
using namespace std;

template <class T>
double half(T x)
{
    double h = x / 2;
    return h;
}

class TuitionBill
{
    friend ostream& operator<<(ostream&, const TuitionBill&);
private:
    string student;
    double amount;
public:
    TuitionBill(string, double);
    double operator/(int);
};

TuitionBill::TuitionBill(string student, double amt)
{
    this->student = student;
    amount = amt;
}

double TuitionBill::operator/(int factor)
{
    double half = amount / factor;
    return half;
}

ostream& operator<<(ostream& o, const TuitionBill& t)
{
    o << t.student << "  Tuition: $" << t.amount;
    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;
}
john
  • 85,011
  • 4
  • 57
  • 81