msvr100.dll free (void * pblock) - c++

Hello there stackOverflow.
I am a noob when it comes to c++ and today i tried to compile my first program including classes. This ofcourse gave me an error and i don't really know what i did wrong since its my first time compling a class program :P
i tried to google it and it said something about either the DLL.'s are building wrong? (i got it all in debug mode, so it can't be that). So the other option is: i might be freeing the memory wrongly or something like that, can anyone explain where i did wrong?
i tried to compare the code to the tutorial i found. But that didn't help. (the tutorial im referring to is: http://www.youtube.com/watch?v=vz1O9nRyZaY ). The error i'm getting is
void __cdecl _free_base (void * pBlock)
{
int retval = 0;
if (pBlock == NULL)
return;
RTCCALLBACK(_RTC_Free_hook, (pBlock, 0));
retval = HeapFree(_crtheap, 0, pBlock);
if (retval == 0)
{
errno = _get_errno_from_oserr(GetLastError());
}
}
and my source code / header files are:
main.cpp
#include <iostream>
#include <string>
#include "conio.h"
#include <crtdbg.h>
#include "Tax.h"
using namespace std;
int main()
{
string name;
double tax;
double income;
cout << "Enter your name: ";
cin >> name;
cout << "Enter your annually income: ";
cin >> income;
cout << "Enter your tax rate in pure numbers: ";
cin >> tax;
Tax Customer_1(name, income, tax);
cout << endl << "Customer name: " << Customer_1.getName() << endl <<
"Income annually: " << Customer_1.getIncome() << endl <<
"Tax percent(in real numbers): " << Customer_1.getTax() << endl <<
"Your income after tax, per month is: " << Customer_1.calculateTax() << endl;
cout << endl;
_getch();
return 0;
}
Tax.h:
#include <iostream>
#include <string>
using namespace std;
#ifndef TAX_H
#define TAX_H
class Tax
{
public:
//Default constructor
Tax();
//Overload constructor
Tax(string, double, double);
//destructor
~Tax();
//Accessor functions
string getName() const;
double getIncome() const;
double getTax() const;
//Mutator functions
void setName(string);
void setIncome(double);
void setTax(double);
double calculateTax() const;
private:
//Member variables
string newName;
double newIncome;
double newTax;
};
#endif
Tax.cpp
#include "Tax.h"
Tax::Tax()
{
newIncome = 0.0;
newTax = 0.0;
}
Tax::Tax(string name, double income, double tax)
{
newName = name;
newIncome = income;
newTax = tax;
}
Tax::~Tax()
{
}
string Tax::getName() const
{
return newName;
}
double Tax::getIncome() const
{
return newIncome;
}
double Tax::getTax() const
{
return newTax;
}
void Tax::setName(string name)
{
newName = name;
}
void Tax::setIncome(double income)
{
newIncome = income;
}
void Tax::setTax(double tax)
{
newTax = tax;
}
double Tax::calculateTax() const
{
return (( newIncome - ( newIncome * (newTax / 100))) / 12); // ((68400 - ( 68400 * (38/100 = 0.38))) / 12)
}

I've (slightly) modified your code to work out, try this:
tax.h:
#include <iostream>
#include <string>
#ifndef TAX_H
#define TAX_H
class Tax
{
public:
//Default constructor
Tax();
//Overload constructor
Tax(std::string, double, double);
//destructor
~Tax();
//Accessor functions
std::string getName() const;
double getIncome() const;
double getTax() const;
//Mutator functions
void setName(std::string);
void setIncome(double);
void setTax(double);
double calculateTax() const;
private:
//Member variables
std::string newName;
double newIncome;
double newTax;
};
#endif
tax.cpp:
#include "tax.h"
Tax::Tax()
{
newIncome = 0.0;
newTax = 0.0;
}
Tax::Tax(std::string name, double income, double tax)
{
newName = name;
newIncome = income;
newTax = tax;
}
Tax::~Tax() {}
std::string Tax::getName() const
{
return newName;
}
double Tax::getIncome() const
{
return newIncome;
}
double Tax::getTax() const
{
return newTax;
}
void Tax::setName(std::string name)
{
newName = name;
}
void Tax::setIncome(double income)
{
newIncome = income;
}
void Tax::setTax(double tax)
{
newTax = tax;
}
double Tax::calculateTax() const
{
return (( newIncome - ( newIncome * (newTax / 100))) / 12);
}
main.cpp
#include "tax.h" // <iostream> and <string> are here
// never put 'using namespace std' in a header file
using namespace std;
int main()
{
string name;
double tax;
double income;
cout << "Enter your name: ";
cin >> name;
cout << "Enter your annually income: ";
cin >> income;
cout << "Enter your tax rate in pure numbers: ";
cin >> tax;
Tax Customer_1(name, income, tax);
cout << endl << "Customer name: " << Customer_1.getName() << endl <<
"Income annually: " << Customer_1.getIncome() << endl <<
"Tax percent(in real numbers): " << Customer_1.getTax() << endl <<
"Your income after tax, per month is: " << Customer_1.calculateTax() << endl;
cout << endl;
return 0;
}
Take note of what has been replaced and the headers you were using; judging by the _getch() at the end of the code, I'm assuming you were using that so that your console window would not go away while debugging; if that was the case, you could instead do something with cin and the strings you already have like so:
...
cin.clear(); // clear buffer for erroneous 'enter' presses
cin >> name; // console will pause until you press enter
return 0;
Though if your intention was to actually have the console pause (vs. the program just ending), you might want to consider other (more elegant) solutions but that is beyond the scope of this question.
A couple of notes: the reason your program worked when not in debug mode (i.e. you went to 'release') is your inclusion of the <crtdbg.h> header; this file is only valid in a debug configuration and you really shouldn't include it if your not using anything in it (which you're not in your code). I also took out the using namespace std; in your tax.h file as there are numerous reasons not to include using namespace std in a header.
I hope that can help

Related

Creating shopping cart

for my class were creating a mini store, and I have the three main functions that work, main.cpp, items.cpp, items.h. but the shoppingcart.cpp and the shoppinfcart.h keeps giving me issues, because I'm trying to pass the parameters from main.cpp, for example (sc1.add(&a) to the functions in shoppingcart.h and shopping cart.cpp.
I have attached copy of my code. any help will be appreciated.
Item.h
#ifndef Items_hpp
#define Items_hpp
#include <string>
#include <iostream>
using namespace std;
class Items {
private:
string ItemName;
float ItemPrice;
int ItemQuant;
public:
Items();
Items(string in, float ip, int iq);
void Name(string in);
string entername();
void CalcPrice( float ip);
float enterprice();
void CalcQuant(int iq);
int enterquant();
};
#endif
Items.cpp
#include "Items.h"
Items::Items()
:ItemName("")
,ItemPrice(0)
,ItemQuant(0)
{
}
Items::Items(string in, float ip, int iq)
:ItemName(in)
,ItemPrice(ip)
,ItemQuant(iq)
{
}
void Items:: CalcPrice(float ip)
{
ItemPrice = ip;
}
void Items::CalcQuant(int iq)
{
ItemQuant = iq;
}
void Items:: Name(std::string in)
{
ItemName = in;
}
float Items:: enterprice()
{
return ItemPrice;
}
int Items:: enterquant()
{
return ItemQuant;
}
string Items:: entername()
{
return ItemName;
}
a snippet of the code for adding items to vector
ShoppingCart.cpp
#include "ShoppingCart.h"
#include <vector>
void ShoppingCart ::add(&its)
{
shopitems.push_back(&its);
}
ShoppingCart.h
#ifndef ShoppingCart_hpp
#define ShoppingCart_hpp
#include <iostream>
#include <string>
#include <vector>
#include "Items.h"
using namespace std;
class ShoppingCart
{
private:
vector <const char its> shopitems;
string cname; //customers name
string cdate; // todays date
public:
ShoppingCart();
ShoppingCart(string cname);
void add( char *its);
};
#endif
an example of main
main.cpp
#include <iostream>
#include <string>
#include <vector>
#include "Items.h"
#include "ShoppingCart.h"
using namespace std;
int main() {
char choice;
int itemsbuy;
float org = .75;
float appl = .25;
float pear = 1.00;
float bana = .85;
float grape = 6.00;
float oatmlk = 5.00;
float almmlk = 6.00;
float cashmlk= 5.00;
float soymlk = 4.00;
float cocomlk = 3.00;
float twopermlk = 3.00;
float vitdmlk = 4.00;
float fatmlk = 4.00;
float goatmlk = 6.00;
float rasinbran = 4.00;
float granola = 5.00;
float honeybunches = 6.00;
float twix = 4.00;
float honey = 5.00;
float yogurt = 1.50;
float cashyog = 2.00;
float eggs = 1.80;
float vegeggs = 3.00;
float cheese = 2.00;
float alcheese = 3.00;
int itemop;
int itemno;
int productcounter = 0;
int productsum= 0;//might need.
cout << "Welcome to J&S Breakfast Grocery store!"<<endl;
cout << "--------------------------------------"<<endl;
cout << "How many items will you be purchasing today?"<<endl;
cout << endl;
cin >> itemsbuy;
ShoppingCart sc1; //**intializing "shoppingcart.h"**//
for (int i = 0; i < itemsbuy; i++) {
cout << "Menu:"<<endl;
cout << "---------------------------"<<endl;
cout << "A. Fruits"<<endl;
cout << "B. Non-Diary"<<endl;
cout << "C. Diary" <<endl;
cout << "D. Cereal" <<endl;
cout << "E. Other"<<endl;
cout << "Please select your type of items to add to cart"<<endl;
cin >> choice;
cout << endl;
switch (choice) {
case 'A':
case 'a':
cout << "Menu:" <<endl;
cout << "---------------------------"<<endl;
cout << "1.Oranges -$ "<< org<<endl;
cout << "2.Apples -$ "<< appl << endl;
cout << "3.Pears - $"<<pear<<endl;
cout << "4.Bananas - $" << bana << endl;
cout << "5.Grapes - $" << grape << endl;
cout << endl;
cout << "Chooose option"<<endl;
cin >> itemop;
cout << "How Many?"<<endl;
cin >> itemno;
if (itemno > itemsbuy) {
cout << "Error, cannot add more than youre planning to buy. Please start over."<<endl;
break;
}
else {
if (itemop ==1) {
Items a("Oranges" , org, itemno);
productcounter++;
sc1.add(&a); //**trying to pass parameter here**//
if (productcounter == itemsbuy) {
cout<< "Error. You are attempting to buy more than you planned. Please Start over. Thank you."<<endl;
}
}
There are a few mistakes in your code.
ShoppingCart.h:
vector <const char its> shopitems;.
std::vector is declared in the following syntax:
std::vector<data type> variable name.
It looks like that you're trying to create a container of Items and store them. Therefore instead of vector<const char> shopitems, you should use:
vector<Items> shopitems instead.
also:
void add(char* its); is incorrect, since this method will be adding Items to your shopitems vector. Change it to: void add(const Items& its);
This will pass a const reference of object Items, meaning the method will not copy the entire class Items when it is called, and the object will not be modifiable.
ShoppingCart.cpp:
Let's also fix the syntax and semantic in your .cpp file, the correct now is:
void ShoppingCart::add(const Item& its)
{
shopitems.push_back(its); //& is not necessary here.
}
Main.cpp
Now, in your main file let's call the method correctly. sc1.add(&a);, the: & here is telling it to pass the memory address of where the object Items a is located. Let's correct this, and pass the object Items a normally, simply write: sc1.add(a);
Logic error
There is one final logic error I caught in your code, that is: if (productcounter == itemsbuy). If itemsbuy (The amount of items you want to buy today) is: 1. Then, productcounter is also 1 and you can't buy anything, this should be: if (productcounter > itemsbuy)

C++ Polymorphism Employee Project

For class I have to adapt a program I wrote last week for polymorphism. Last week it used a specific set of information for the employees but now I have to make it work with polymorphism as well as read/write data from a file, I am completely lost with what I am supposed to be doing, If someone could even point me in the right direction it would be so much help. I can post my current .h and .cpp file for a look at what I have as well as the instructions of what I am supposed to be doing.
.h
#pragma once
#include <string>
using namespace std;
class Employee {
private:
int employeeNumber; // Employee's employee number
string employeeName; //Employee's name
string streetAddress; //Employee's street address
string phoneNumber; //Employee's phone number
double hourlyWage; //Employee's hourly wage
double hoursWorked; //Employee's hours worked
double netPay; //Net pay
double grossPay; //Gross pay
public:
Employee();
Employee(int, string, string, string, double, double);
int getEmployeeNumber();
void setEmployeeNumber(int);
string getEmployeeName();
void setEmployeeName(string);
string getStreetAddress();
void setStreetAddress(string);
string getPhoneNumber();
void setPhoneNumber(string);
double getHourlyWage();
void setHourlyWage(double);
double getHoursWorked();
void setHoursWorked(double);
double calcPay()
{
const int OVER = 40;
double federal = 0.20;
double state = 0.075;
double timeHalf = 1.5;
double grossPay;
double netPay;
if (getHoursWorked() < OVER)
{
grossPay = getHoursWorked() * getHourlyWage();
netPay = grossPay - (grossPay * federal) - (grossPay * state);
}
if (getHoursWorked() >= OVER)
{
grossPay = getHoursWorked() * ((getHourlyWage() * timeHalf));
netPay = grossPay - (grossPay * federal) - (grossPay * state);
}
return netPay;
}
};
.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "Employee.h"
#include <iomanip>
using namespace std;
Employee::Employee()
{
employeeNumber = 0; // Employee's employee number
employeeName = ""; //Employee's name
streetAddress = ""; //Employee's street address
phoneNumber = ""; //Employee's phone number
hourlyWage = 0; //Employee's hourly wage
hoursWorked = 0;
grossPay = 0;
netPay = 0;
}
Employee::Employee(int empNum, string empName, string streetAddress,
string phoneNumber, double hourlyWage, double hoursWorked)
{
employeeNumber = empNum;
employeeName = empName;
this->streetAddress = streetAddress;
this->phoneNumber = phoneNumber;
this->hourlyWage = hourlyWage;
this->hoursWorked = hoursWorked;
grossPay = 0;
netPay = 0;
}
int Employee::getEmployeeNumber()
{
return employeeNumber;
}
void Employee::setEmployeeNumber(int empNum)
{
employeeNumber = empNum;
}
string Employee::getEmployeeName()
{
return employeeName;
}
void Employee::setEmployeeName(string empName)
{
employeeName = empName;
}
string Employee::getStreetAddress()
{
return streetAddress;
}
void Employee::setStreetAddress(string strtAddrs)
{
streetAddress = strtAddrs;
}
string Employee::getPhoneNumber()
{
return phoneNumber;
}
void Employee::setPhoneNumber(string phnNum)
{
phoneNumber = phnNum;
}
double Employee::getHourlyWage()
{
return hourlyWage;
}
void Employee::setHourlyWage(double hrWage)
{
hourlyWage = hrWage;
}
double Employee::getHoursWorked()
{
return hoursWorked;
}
void Employee::setHoursWorked(double hrWorked)
{
hoursWorked = hrWorked;
}
void printCheck(Employee ee)
{
cout << "\n\n--------------------- Fluff Shuffle Electronics -------------------------------- \n";
cout << " Pay to the order of " << ee.getEmployeeName() << "...........................$" << ee.calcPay();
cout << "\n\n United Bank of Eastern Orem \n";
cout << "------------------------------------------------------------------------------- \n";
cout << " Hours Worked: " << ee.getHoursWorked();
cout << "\n Hourly Wage: " << ee.getHourlyWage();
cout << endl << endl;
}//End of function
void read(ifstream &in)
{
Employee employees[10];
int counter = 0;
while (in.read((char *)&employees[counter++], sizeof(Employee)))
for (int i = 0; i<counter; i++)
{
printCheck(employees[i]);
}
in.close();
}
void write(ofstream &out)
{
Instantiate your employees here first, then call their functions.
Employee joe(37, "Joe Brown", "123 Main St.", "123-6788", 10.00,
45.00);
printCheck(joe);
Employee sam(21, "Sam Jones", "45 East State", "661-9000", 12.00,
30.00);
printCheck(sam);
Employee mary(15, "Mary Smith", "12 High Street", "401-8900", 15.00, 40.00);
printCheck(mary);
out.write((char *)(&joe), sizeof(Employee));
out.write((char *)(&sam), sizeof(Employee));
out.write((char *)(&mary), sizeof(Employee));
out.close();
}
//Main function
int main()
{
int choice;
string filename;
while (true)
{
cout << "\nThis program has two options:\n";
cout << "1 - Create a data file, or\n";
cout << "2 - Read data from a file and print paychecks\n";
cout << "\n Press any other key to quit..........\n";
cout << "Please enter <1> to create a file or <2> to print
checks: ";
cin >> choice;
if (choice == 1)
{
cout << "Enter the file name: ";
cin >> filename;
ofstream out(filename);
out.open(filename.c_str(), ios::binary);
write(out);
}
else if (choice == 2)
{
cout << "Enter the file name: ";
cin >> filename;
ifstream in(filename);
in.open(filename.c_str(), ios::binary);
read(in);
}
else break;
//Calls function to displays information
}
}//End of main
These are the instructions for the project.
This is the diagram it refers to
To start: create two classes derived from Employee:
class HourlyEmployee: public Employee
{
};
class SalariedEmployee: public Employee
{
}
and move members related to Hourly working from Employee to HourlyEmployee, then add members related to Salary to SalariedEmployee (WeeklySalary).
This way (removing attributes related to hourly working) you make Employee class more general that can be a base for other kind of employees to (SalariedEmployee).
When you derive HourlyEmployee or SalariedEmployee from Employee, you mean they are kind of Employee, so members that Employee has, they will inherit automatically.

no candidates for error compile error

I am trying to compile my application and it keeps running into "no match for 'operator<<'... it is not clear what the exact error of the program seems to be because he object is configured correctly as far as I can see.
#include <iostream>
#include <conio.h>
#include <cmath>
#include <stdexcept>
using namespace std;
class MortgageCalc
{
protected:
float term;
public:
void setData(float, float, float);
float setTerm ();
float monthly;
float total;
float interest;
float setLoan(void); //mutator
float setIntrest(void); //mutator
float setYears(void);
int years;
float loan;
};
void MortgageCalc::setData(float l, float i, float y)
{
loan = l;
interest = i;
years = y;
setTerm();
}
float MortgageCalc::setTerm()
{ //simple interest calculation with power calc to establish whole number translation
term = pow((1 + ((interest/100) / 12)), (12 * years));
return term;
}
float MortgageCalc::setLoan(void)
{ //returns loan amt to private member
return loan;
}
float MortgageCalc::setIntrest(void)
{ //returns interest amt to private member
return interest;
}
float MortgageCalc::setYears(void)
{ //returns years to private member
return years;
}
class mPayment : public MortgageCalc
{
public:
int monthly()
{
return ((loan * ((interest/100) / 12) * term ) / (term - 1));
}
};
class tPayment : public mPayment
{
public:
int total()
{
return (monthly() * (years * 12));
}
};
class iPayment : public tPayment
{
public:
int plusInterest()
{
return (total() - loan);
}
};
int main()
{
double loan(0), interest(0);
int years = 0;
MortgageCalc mort1;
cout << "Enter the total loan amount on your mortgage loan: $"; //established loan variable
cin >> loan;
cout << "Enter the interest rate (in whole #'s only): "; //establishes interest rate variable
cin >> interest;
cout << "Enter the length of the loan in years: "; //establishes term of payments
cin >> years;
mort1.setData(loan, interest, years);
mPayment m;
cout << "Monthly payment due is " << m.monthly() << "." << endl;
tPayment t;
cout << "Total payment will be " << t.total() << "." << endl;
iPayment i;
cout << "Total payment plus Interest will be " << i.plusInterest() << "." << endl;
return 0;
};
cout << "Total payment plus Interest will be " << i.plusInterest << "." << endl;
plusInterest is a class method pointer. Unsurprisingly, std::ostream has no clue what to do with a class method pointer, and is rightfully voicing its very strong objection, to such a preposterous proposition that it knows what to do with some strange class's method pointer.
You probably meant to write:
cout << "Total payment plus Interest will be " << i.plusInterest() << "." << endl;
Now, that's a proper function call, that returns an int, and std::ostream is now delighted to take this int, and do its magic with it.

I clearly initialized my class, but I am getting errors saying I didn't [duplicate]

This question already has answers here:
Method for solving error: "cannot instantiate abstract class"
(3 answers)
Closed 6 years ago.
error1-error C2259: 'HourlyEmployee' : cannot instantiate abstract class
error2-object of abstract class type "HourlyEmployee" is not allowed:
pure virtual function "Employee::printPay" has no overrider
This error is in this part and I don't understand the problem.
VE.push_back(new HourlyEmployee(empID, hours, payRate));
main.cpp:
#include "hourly.h"
#include "lab7.h"
#include "salarie.h"
#include <iostream>
#include <iomanip>
#include <vector>
void getInput(vector <Employee *> & VE);
void printList(const vector <Employee *> & VE);
using namespace std;
int main()
{
vector <Employee *> VEmp;
getInput(VEmp);
printList(VEmp);
cout << "Lab 7 completed by: " << "youssef zaki " << endl;
system("pause");
return 0;
}
void getInput(vector<Employee*>& VE)
{
std::vector < HourlyEmployee > ;
int empID,
choice;
double salary,
hours,
payRate;
do
{
cout << "Enter 1 for Hourly Employee" << endl;
cout << "Enter 2 for Salaried Employee" << endl;
cout << "Enter 3 to stop: ";
cin >> choice;
cout << endl;
// process option
switch (choice)
{
case 1:
cout << "Enter the ID: ";
cin >> empID;
cout << "Enter the number of hours worked: ";
cin >> hours;
cout << "Enter the pay rate: ";
cin >> payRate;
VE.push_back(new HourlyEmployee(empID, hours, payRate));
break;
case 2:
cout << "Enter the ID: ";
cin >> empID;
cout << "Enter the salary: ";
cin >> salary;
VE.push_back(new SalariedEmployee(empID, salary));
break;
case 3:
break;
default:
cout << "Error: invalid option" << endl;
}
cout << endl;
} while (choice != 3);
}
void printList(const vector<Employee*>& VE )
{
for (std::size_t i = 0; i != VE.size(); ++i)
{
VE[i]->printPay();
}
}
employ.h file:
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
class Employee
{
private:
int EmpID;
public:
Employee(int);
~Employee();
int getEmpID() const;
virtual void printPay()=0;
};
#endif
employ.cpp:
#include "lab7.h"
#include <iostream>
#include <iomanip>
#include <vector>
Employee::Employee(int empID)
{
EmpID = empID;
}
Employee::~Employee()
{
}
int Employee::getEmpID() const
{
return EmpID;
}
employsalarie.h file:
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
class SalariedEmployee: public Employee
{
private:
double salary;
public:
SalariedEmployee(int, double);
~SalariedEmployee();
double getSalary() const;
void printPay();
};
#endif
employsalry.cpp:
#include "salarie.h"
#include <vector>
SalariedEmployee::SalariedEmployee(int empID, double salary) : ` ` Employee(empID)
{
salary = salary;
}
SalariedEmployee::~SalariedEmployee()
{
}
double SalariedEmployee::getSalary() const
{
return salary;
}
void SalariedEmployee::printPay()
{
double weeklyPay = salary / 52;
cout << fixed << showpoint << setprecision(2);
cout << "The pay for the salaried employee with ID number ";
cout << getEmpID() << " is $" << weeklyPay << endl;
}
hourlyemploy.h:
#ifndef HOURLYEMPLOYEE_H
#define HOURLYEMPLOYEE_H
#include "lab7.h"
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
class HourlyEmployee : public Employee
{
private:
double hours;
double payRate;
public:
HourlyEmployee(int, double, double);
~HourlyEmployee();
double getHours() const;
double getPayRate() const;
void printPay(double);
};
#endif
hourlyemploy.cpp:
#include "hourly.h"
#include <iostream>
#include <iomanip>
#include <vector>
HourlyEmployee::HourlyEmployee(int empID, double hours, double payRate)
: Employee(empID)
{
this->hours = hours;
this->payRate = payRate;
}
HourlyEmployee::~HourlyEmployee()
{
}
double HourlyEmployee::getHours() const
{
return hours;
}
double HourlyEmployee::getPayRate() const
{
return payRate;
}
void HourlyEmployee::printPay(double)
{
double weeklyPay = hours * payRate;
cout << fixed << showpoint << setprecision(2);
cout << "The pay for the hourly employee with ID number ";
cout << getEmpID() << " is $" << weeklyPay << endl;
}
Error message gives you all the information:
error1-error C2259: 'HourlyEmployee' : cannot instantiate abstract class error2-object of abstract class type "HourlyEmployee" is not allowed:
pure virtual function "Employee::printPay" has no overrider
In HourlyEmployee class you have not overriden void printPay(), you only added method of the same name but different argument list: void printPay(double);.
I suggest to use override keyword when overriding virtual methods:
void printPay(double) override; // this will generate error (signature mismatch)
while:
void printPay() override; // will work just fine
Another problem from your code:
SalariedEmployee::SalariedEmployee(int empID, double salary) : ` ` Employee(empID)
{
salary = salary;
}
here salary is a local variable in SalariedEmployee, to properly initialize class variable use
this->salary = salary;
More hints to earn extra points (From your HW) :-)
Do not use using namespace std;
Do not use endl, but instead "\n"
Do not use raw pointers, use std::unique_ptr<> and if you need to share it then std::shared_ptr<>. Those two together with std::make_unique and std::make_shared.
In you code you use new but I never found delete - thats asking for memory leaks.

C++ Calculating wage earned and overtime using a structure.

I am trying to figure out what is wrong with my code. I must use a structure to prompt the user for a workers idNumber, hoursWorked, and hourlyWage. I have the idNumber, hoursWorked, and hourlyWage working well, however the problem lies in my calc function. I cannot figure out how to calculate the money earned with overtime being 1.5 times and to be able to print that to the screen. I keep getting a mash up of weird numbers.
#include<iostream>
using namespace std;
struct Worker
{
int idNumber;
int hoursWorked;
double hourlyRate;
double earned;
};
void input(Worker & theData);
//Postcondition: theData.idNumber, theData.hoursWorked, and theData.hourlyRate are given input values
// the user must input into these values.
void print(const Worker &);
void calc(Worker & theWage);
void main()
{
Worker Data;
input(Data);
print(Data);
system("pause");
}
void input(Worker & theData)
{
cout << "Enter the Employee idNumber";
cin >> theData.idNumber;
cout << "Enter the Hours Worked.";
cin >> theData.hoursWorked;
cout << "Enter the HoutlyRate for under 41 hours.";
cin >> theData.hourlyRate;
}
void print(const Worker & w)
{
cout << w.idNumber << "\n"
<< w.hoursWorked << "\n"
<< w.hourlyRate << "\n"
<< w.earned << endl;
}
void calc(Worker & theWage)
{
if (theWage.hoursWorked <= 40)
{
theWage.earned = theWage.hoursWorked * theWage.hourlyRate;
}
else
{
int basePay;
basePay = theWage.hoursWorked * theWage.hourlyRate;
theWage.earned = (theWage.hoursWorked - 40) * 1.5 + basePay;
}
}
Your calc() function must be called in main before your print() function.
void main()
{
Worker Data;
input(Data);
calc(Data); // This line was forgotten.
print(Data);
system("pause");
}
The weird number you saw was the uninitialized earned variable.