Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Initializing with constructors

Status
Not open for further replies.

bradmanda

Programmer
Feb 24, 2007
2
0
0
US
Hello, I need to figure out how to add a constructor and destructor to my class, labeled "Invoice." The constructor will initialize the invoice number (innumber) to 1000, days (days) to 1, category (categ) to 2, and the insurance (insur) to 0.0. I don't believe I can just simply initialize the values in my external member functions below like so, because I keep getting the following error prompts. Any help is appreciated!


d:\projects\invoice5.cpp(24) : error C2614: 'Invoice' : illegal member initialization: 'categ' is not a base or member
d:\projects\invoice5.cpp(24) : error C2614: 'Invoice' : illegal member initialization: 'insur' is not a base or member
d:\projects\invoice5.cpp(24) : error C2614: 'Invoice' : illegal member initialization: 'days' is not a base or member
d:\projects\invoice5.cpp(24) : error C2614: 'Invoice' : illegal member initialization: 'innumber' is not a base or member

My code:

// ==================================================================
// > Amanda Bradshaw
// > Program: lab4.cpp
// > Description: functions, overloading, static, etc.
// ==================================================================
#include <iostream>
#include <iomanip>
#include <ctype.h>
using namespace std;

const char TITLE[] = "Sierra Rental Agency";
const double MININS = 0.0;
const double MAXINS = 50.0;
const int MINI = 1;
const int MAXI = 20000;
const int MIND = 1;
const int MAXD = 30;
const int MINC = 1;
const int MAXC = 7;

int get(char msg[], int min, int max);
double get(char msg[], double min, double max);
char get(char msg[]="Are you sure");
void get(char item[], int size, char msg[]);
void line(int length=70, char shape='=');

float Rates[] = { 0.0F, 18.00F, 21.50F, 25.00F, 39.00F, 41.50F, 45.00F, 300.00F};

char categs[][20] = { "",
"Subcompact",
"Compact",
"Midsize",
"SUV",
"Luxury",
"Truck",
"Motorcycle"
};

struct invoicedata
{
int innumber;
int days;
double insur;
double dailyfee;
double totalfee;
double totalcharge;
char categname[40];
};

class Invoice
{
private:
struct invoicedata data;
public:
Invoice(); //constructor
~Invoice();
static void Opening();
void ShowData();
void GetData();
void GetData(int innumber, int days, double insur, int categ);
void CalcFee();
double GetFee();
};

/* ================================================================ */
int main()
{
Invoice invoice1;
int count = 0;
char verify;
float total = 0;

Invoice::Opening();
do
{

Invoice invoice1; //constructor called

cout << "Default Package (y/n): ";
cin >> verify;
if (verify == 'N' || verify == 'n')
{
invoice1.GetData();
}
else
{
invoice1.GetData(1000, 1, 0.0, 2);
}
invoice1.CalcFee();
Invoice::Opening();
invoice1.ShowData();
count++;
total += invoice1.GetFee();
}
while (get("\n\nEnter another course") == 'Y');

cout << "Number of Invoices ------ " << count << endl;
cout << "Grand Total ------------- " << total << endl;
cout << "Average ----------------- " << total / count << endl;
cout << "Press ENTER to end ";
cin.get();
return 0;
}
/* ================================================================ */

Invoice::Invoice(): innumber(1000), days(1), insur(0.0), categ(2) //contstructor values initialized
{
GetData();
}

Invoice::~Invoice()
{
cout << "Course " << data.categname << " cleared." << endl;
}

void Invoice::Opening()
{
line();
cout << " Sierra College Rental Agency " << endl;
line();
}
void Invoice::ShowData()
{
cout << "Invoice Number ......" << data.innumber << endl;
cout << "Days Rented ........." << data.days << endl;
cout << "Dailly Amount ......." << data.dailyfee << endl;
cout << "Rental Category ....." << data.categname << endl;
line();
cout << "Rental Amount ......." << data.totalfee << endl;
cout << "Insurance Amount ...." << data.insur << endl;
cout << "Total Charge ........" << data.totalcharge << endl;
}
void Invoice::GetData()
{
int categ;

cout << "1 = Subcompact\n";
cout << "2 = Compact\n";
cout << "3 = Midsize\n";
cout << "4 = SUV\n";
cout << "5 = Luxury\n";
cout << "6 = Truck\n";
cout << "7 = Motorcycle\n";
categ = get("Select the rental category: ", MINC, MAXC);
strcpy(data.categname, categs[categ]);
data.dailyfee = Rates[categ];

data.innumber = get("Enter the invoice number: ", MINI, MAXI);
data.days = get("Enter the number of days rented: ", MIND, MAXD);
data.insur = get("Enter the insurance amount: ", MININS, MAXINS);
cin.ignore(20, '\n');
}
void Invoice::GetData(int innumber, int days, double insur, int categ)
{
strcpy(data.categname, categs[categ]);
data.dailyfee = Rates[categ];

data.innumber = innumber;
data.days = days;
data.insur = insur;
}
void Invoice::CalcFee()
{
data.totalfee = data.dailyfee * data.days;
data.totalcharge = data.totalfee + data.insur;
}
inline double Invoice::GetFee()
{
return data.totalcharge;
}

/* ================================================================ */
int get(char msg[], int min, int max)
{
int item;
cout << msg << ": ";
cin >> item;
while (cin.fail() || item < min || item > max)
{
if (cin.fail())
{
cin.clear();
cin.ignore(10, '\n');
cout << "Error, non-numeric input " << endl;
}
else
{
cout << "Error, value must be " << min << " to " << max << endl;
}
cout << msg << ": ";
cin >> item;
}
cin.ignore(10, '\n');
return item;
}

/* ================================================================ */
double get(char msg[], double min, double max)
{
double item;
cout << msg << ": ";
cin >> item;
while (cin.fail() || item < min || item > max)
{
if (cin.fail())
{
cin.clear();
cin.ignore(10, '\n');
cout << "Error, non-numeric input " << endl;
}
else
{
cout << "Error, value must be " << min << " to " << max << endl;
}
cout << msg << ": ";
cin >> item;
}
cin.ignore(10, '\n');
return item;
}
/* ================================================================ */
char get(char msg[])
{
char choice;
cout << msg << " (Y/N): ";
cin >> choice;
choice = (char)toupper(choice);
while (choice != 'N' && choice != 'Y')
{
cout << "\aError, you must enter a Y or N, try again" << endl;
cout << msg << " (Y/N): ";
cin >> choice;
choice = (char)toupper(choice);
}
cin.ignore(10, '\n');
return choice;
}
/* ================================================================ */
void get(char item[], int size, char msg[])
{
cout << msg << ": ";
cin.get(item, size);
cin.ignore(10, '\n');
}
/* ================================================================ */
void line(int length, char shape)
{
for(int k=0; k < length; k++)
{
cout << shape;
}
cout << endl;
}
/* ================================================================ */



 
Use [ignore]
Code:
[/ignore] tags when posting code.

The only member variable you have in your class is data, so the compiler is telling you it doesn't know what you're talking about.
You need to initialize each of the members of your data member.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top