virtual function syntax

Can not compile. Please help to amend

Part 1/2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24


#define EMPLOYEE_H
#include <string> // C++ standard string class
class Employee
{
public:
Employee( const std::string &, const std::string &,
const std::string & );
virtual ~Employee() { }; // virtual destructor
void setFirstName( const std::string & ); // set first name
std::string getFirstName() const; // return first name
void setLastName( const std::string & ); // set last name
std::string getLastName() const; // return last name
void setSocialSecurityNumber( const std::string & ); // set SSN
std::string getSocialSecurityNumber() const; // return SSN
// pure virtual function makes Employee an abstract base class
virtual double earnings() const = 0; // pure virtual
virtual void print() const; // virtual
private:
std::string firstName;
std::string lastName;
std::string socialSecurityNumber;
}; // end class Employee 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Fig. 12.11: SalariedEmployee.h
// SalariedEmployee class derived from Employee.

#define SALARIED_H
#include <string> // C++ standard string class
#include "Employee.h" // Employee class definition
class SalariedEmployee : public Employee
{
public:
SalariedEmployee( const std::string &, const std::string &,
const std::string &, double = 0.0 );
virtual ~SalariedEmployee() { }; // virtual destructor
void setWeeklySalary( double ); // set weekly salary
double getWeeklySalary() const; // return weekly salary
// keyword virtual signals intent to override
virtual double earnings() const override; // calculate earnings
virtual void print() const override; // print object
private:
double weeklySalary; // salary per week
}; // end class SalariedEmployee 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Fig. 12.13: CommissionEmployee.h
// CommissionEmployee class derived from Employee.
#define COMMISSION_H
#include <string> // C++ standard string class
#include "Employee.h" // Employee class definition
class CommissionEmployee : public Employee
{
public:
CommissionEmployee( const std::string &, const std::string &,
const std::string &, double = 0.0, double = 0.0 );
virtual ~CommissionEmployee() { }; // virtual destructor
void setCommissionRate( double ); // set commission rate
double getCommissionRate() const; // return commission rate
void setGrossSales( double ); // set gross sales amount
double getGrossSales() const; // return gross sales amount
// keyword virtual signals intent to override
virtual double earnings() const override; // calculate earnings
virtual void print() const override; // print object
private:
double grossSales; // gross weekly sales
double commissionRate; // commission percentage
}; // end class CommissionEmployee 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Fig. 12.15: BasePlusCommissionEmployee.h
// BasePlusCommissionEmployee class derived from CommissionEmployee.
#ifndef BASEPLUS_H
#define BASEPLUS_H
#include <string> // C++ standard string class
#include "CommissionEmployee.h" // CommissionEmployee class definition
class BasePlusCommissionEmployee : public CommissionEmployee
{
public:
BasePlusCommissionEmployee( const std::string &, const std::string &,
const std::string &, double = 0.0, double = 0.0, double = 0.0 );
virtual ~BasePlusCommissionEmployee() { }; // virtual destructor
void setBaseSalary( double ); // set base salary
double getBaseSalary() const; // return base salary
// keyword virtual signals intent to override
virtual double earnings() const override; // calculate earnings
virtual void print() const override; // print object
private:
double baseSalary; // base salary per week
}; // end class BasePlusCommissionEmployee
#endif // BASEPLUS_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

// SalariedEmployee class member-function definitions.
#include <iostream>
#include <stdexcept>
#include "SalariedEmployee.h" // SalariedEmployee class definition
using namespace std;
// constructor
SalariedEmployee::SalariedEmployee( const string &first,
const string &last, const string &ssn, double salary )
: Employee( first, last, ssn )
{
setWeeklySalary( salary );
} // end SalariedEmployee constructor
// set salary
void SalariedEmployee::setWeeklySalary( double salary )
{
if ( salary >= 0.0 )
weeklySalary = salary;
else
throw invalid_argument( "Weekly salary must be >= 0.0" );
} // end function setWeeklySalary
// return salary
double SalariedEmployee::getWeeklySalary() const
{
return weeklySalary;
} // end function getWeeklySalary
// calculate earnings;
// override pure virtual function earnings in Employee
double SalariedEmployee::earnings() const
{
return getWeeklySalary();
} // end function earnings
// print SalariedEmployee's information
void SalariedEmployee::print() const
{

cout << "salaried employee: ";
Employee::print(); // reuse abstract base-class print function
cout << "\nweekly salary: " << getWeeklySalary();
} // end function print 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Fig. 12.14: CommissionEmployee.cpp
// CommissionEmployee class member-function definitions.
#include <iostream>
#include <stdexcept>
#include "CommissionEmployee.h" // CommissionEmployee class definition
using namespace std;
// constructor
CommissionEmployee::CommissionEmployee( const string &first,
const string &last, const string &ssn, double sales, double rate )
: Employee( first, last, ssn )
{
setGrossSales( sales );
setCommissionRate( rate );
} // end CommissionEmployee constructor
// set gross sales amount
void CommissionEmployee::setGrossSales( double sales )
{
if ( sales >= 0.0 )
grossSales = sales;
else
throw invalid_argument( "Gross sales must be >= 0.0" );
} // end function setGrossSales
// return gross sales amount
double CommissionEmployee::getGrossSales() const
{
return grossSales;
} // end function getGrossSales

// set commission rate
void CommissionEmployee::setCommissionRate( double rate )
{
if ( rate > 0.0 && rate < 1.0 )
commissionRate = rate;
else
throw invalid_argument( "Commission rate must be > 0.0 and < 1.0" );
} // end function setCommissionRate
// return commission rate
double CommissionEmployee::getCommissionRate() const
{
return commissionRate;
} // end function getCommissionRate
// calculate earnings; override pure virtual function earnings in Employee
double CommissionEmployee::earnings() const
{
return getCommissionRate() * getGrossSales();
} // end function earnings
// print CommissionEmployee's information
void CommissionEmployee::print() const
{
cout << "commission employee: ";
Employee::print(); // code reuse
cout << "\ngross sales: " << getGrossSales()
<< "; commission rate: " << getCommissionRate();
} // end function print 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Fig. 12.16: BasePlusCommissionEmployee.cpp
// BasePlusCommissionEmployee member-function definitions.
#include <iostream>
#include <stdexcept>
#include "BasePlusCommissionEmployee.h"
using namespace std;
// constructor
BasePlusCommissionEmployee::BasePlusCommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate, double salary )
: CommissionEmployee( first, last, ssn, sales, rate )
{
setBaseSalary( salary ); // validate and store base salary
} // end BasePlusCommissionEmployee constructor

// set base salary
void BasePlusCommissionEmployee::setBaseSalary( double salary )
{
if ( salary >= 0.0 )
baseSalary = salary;
else
throw invalid_argument( "Salary must be >= 0.0" );
} // end function setBaseSalary
// return base salary
double BasePlusCommissionEmployee::getBaseSalary() const
{
return baseSalary;
} // end function getBaseSalary
// calculate earnings;
// override virtual function earnings in CommissionEmployee
double BasePlusCommissionEmployee::earnings() const
{
return getBaseSalary() + CommissionEmployee::earnings() ;
} // end function earnings
// print BasePlusCommissionEmployee's information
void BasePlusCommissionEmployee::print() const
{
cout << "base-salaried ";
CommissionEmployee::print(); // code reuse
cout << "; base salary: " << getBaseSalary();
} // end function print 



Last edited on
Part 2/2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/ Fig. 12.17: fig12_17.cpp
// Processing Employee derived-class objects individually
// and polymorphically using dynamic binding.
#include <iostream>
#include <iomanip>
#include <vector>
#include "Employee.h"
#include "SalariedEmployee.h"
#include "CommissionEmployee.h"
#include "BasePlusCommissionEmployee.h"
using namespace std;
void virtualViaPointer( const Employee * const ); // prototype
void virtualViaReference( const Employee & ); // prototype
int main()
{
// set floating-point output formatting
cout << fixed << setprecision( 2 );
// create derived-class objects
SalariedEmployee salariedEmployee("John", "Smith", "111-11-1111", 800 );
CommissionEmployee commissionEmployee("Sue", "Jones", "333-33-3333", 10000, .06 );
BasePlusCommissionEmployee basePlusCommissionEmployee("Bob", "Lewis", "444-44-4444", 5000, .04, 300 );
cout << "Employees processed individually using static binding:\n\n";
// output each Employee’s information and earnings using static binding
salariedEmployee.print();
cout << "\nearned $" << salariedEmployee.earnings() << "\n\n";
commissionEmployee.print();
cout << "\nearned $" << commissionEmployee.earnings() << "\n\n";
basePlusCommissionEmployee.print();
cout << "\nearned $" << basePlusCommissionEmployee.earnings()
<< "\n\n";
// create vector of three base-class pointers
vector< Employee * > employees( 3 );
// initialize vector with pointers to Employees
employees[ 0 ] = &salariedEmployee;
employees[ 1 ] = &commissionEmployee;
employees[ 2 ] = &basePlusCommissionEmployee;
cout << "Employees processed polymorphically via dynamic binding:\n\n";
// call virtualViaPointer to print each Employee's information
// and earnings using dynamic binding
cout << "Virtual function calls made off base-class pointers:\n\n";
for ( const Employee *employeePtr : employees )
virtualViaPointer( employeePtr );
// call virtualViaReference to print each Employee's information
// and earnings using dynamic binding
cout << "Virtual function calls made off base-class references:\n\n";

for ( const Employee *employeePtr : employees )
virtualViaReference( *employeePtr ); // note dereferencing
} // end main
// call Employee virtual functions print and earnings off a
// base-class pointer using dynamic binding
void virtualViaPointer( const Employee * const baseClassPtr )
{
baseClassPtr->print();
cout << "\nearned $" << baseClassPtr->earnings() << "\n\n";
} // end function virtualViaPointer
// call Employee virtual functions print and earnings off a
// base-class reference using dynamic binding
void virtualViaReference( const Employee &baseClassRef )
{
baseClassRef.print();

cout << "\nearned $" << baseClassRef.earnings() << "\n\n";
} // end function virtualViaReference 
Proper formatting that includes some white space & indentation would go a long way to make that wall of text more readable.
Can not compile.

And your compile-time errors are? The EXACT errors would be helpful, "can't compile" isn't one bit useful.
You may want to start by fixing most of your #include guards. The fist several files don't have proper guards, look at your baseplus header for the proper use of guards.



Actually there are no compiler errors but linker errors.

This Employee::print(); // reuse abstract base-class print function is not allowed. Since Employee::print has no body.

You probably also need to implement the Employee constructor.
Still don't understand meaning ifndef, yet, i always deleted book ifndef.
Fixed. Works.
Thank.
If someone could easily explain it's usage i will cast big plus to carma.
Namaste
Still don't understand meaning ifndef, yet, i always deleted book ifndef.
I guess you mean include guards. See:

https://www.geeksforgeeks.org/include-guards-in-c/

If someone could easily explain it's usage
What? The include guards?
Still don't understand meaning ifndef
If Not Defined.

https://stackoverflow.com/questions/1653958/why-are-ifndef-and-define-used-in-c-header-files

If someone could easily explain it's usage
http://en.wikipedia.org/wiki/Include_guard

i always deleted book ifndef.
Don't. Learn what something means before you modify what an experienced programmer has used.
Topic archived. No new replies allowed.