0

Good evening S.O!

Whats the problem?

So I'm trying out a few test examples in of class creation and constructors, and I'm trying to overload a constructor by passing it different parameters. In the following code, I overload Sale by using parameters (double rate) and ().

From my limited knowledge about overloading, this should work. Only problem.. it doesn't seem to.

Here's some code

#include "stdafx.h"  // Defines IDE required "pre-compiled" definition files

#include <iostream>  // Defines objects and classes used for stream I/O

#include <iomanip>
//Namespaces utilized in this program
using namespace std; 
// Announces to the compiler that members of the namespace



// sale class declaration
class Sale
{
private:
double taxRate;

public:
Sale(double rate) //Constructor with 1 parameter, handles taxable sales
{
    taxRate = rate;
}

Sale()
{
    taxRate = 0.0; // handles tax-exempt sales
}

double calcSaleTotal(double cost)
{
    double total = cost + cost*taxRate;
    return total;

}

};


int main()          
{
// Constant "const" Value Declarations
const int NO_ERRORS = 0;  // Program Execution Status: No errors

// Initialized Variable Declarations
int programStatus = NO_ERRORS;  // Assume no program execution errors

Sale cashier1(.06); //defines a Sale object with a 6% sales tax
Sale cashier2();    // define a tax-exempt Sale object

//format the output

cout << fixed << showpoint << setprecision(2); 


//Get and display the total sale pricefor two 24.95 sales.
cout << "With a 0.06 sales tax rate, the total\n";
cout << "of the $24.95 sale is $";
cout << cashier1.calcSaleTotal(24.95) << endl;

cout << "\nOn a tax-exempt purchase, the total of the $24.95 sale is, of course, $";
cout << cashier2.calcSaleTotal(24.95) << endl; 

// This prevents the Console Window from closing during debug mode using
// the Visual Studio IDE.
// Note: Generally, you should not remove this code
cin.ignore(cin.rdbuf()->in_avail());
cout << "\nPress only the 'Enter' key to exit program: ";
cin.get();

return programStatus;

As you can see

I've defined two class objects, both Sale cashier1(.06) for my taxable purchases and

Sale cashier2() for my tax-exempt purchase.

My cashier1 is defined as a Sale class no problem and runs fine. cashier2 however gets an error saying that expression must have a class type.

Where in my code did I go wrong?

Thanks in advance :D Happy coding!

-Jef

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Podo
  • 709
  • 9
  • 30

1 Answers1

0

The line

Sale cashier2();

should be

Sale cashier2;

As previously it is a declaration of a function not what you want

Ed Heal
  • 59,252
  • 17
  • 87
  • 127