Sign in to follow this  

C++ Overloaded Function?

This topic is 2482 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am now in Advanced C++ and my teacher is just part of the office staff, therefor does not teach nor know anything about c++. So I am really struggling.
This is just a basic constructor, however it's got some extra line after it preceded by a colon.

The below code is an overloaded function?

What does " : CommissionEmployee( first, last, ssn, sales, rate )" do?

Theres also a line in the header like this, what does it do?

[code]// constructor
BasePlusCommissionEmployee::BasePlusCommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate, double salary )
// explicitly call base-class constructor
: CommissionEmployee( first, last, ssn, sales, rate )
{
setBaseSalary( salary ); // validate and store base salary

cout << "BasePlusCommissionEmployee constructor: " << endl;
print();
cout << "\n\n";
} // end BasePlusCommissionEmployee constructor[/code]

Header:
[code]#include <string> // C++ standard string class
using std::string;

#include "CommissionEmployee.h" // CommissionEmployee class declaration

//LINE BELOW THIS
class BasePlusCommissionEmployee : public CommissionEmployee
{
public:
BasePlusCommissionEmployee( const string &, const string &,
const string &, double = 0.0, double = 0.0, double = 0.0 );
~BasePlusCommissionEmployee(); // destructor

void setBaseSalary( double ); // set base salary
double getBaseSalary() const; // return base salary

double earnings() const; // calculate earnings
void print() const; // print BasePlusCommissionEmployee object
private:
double baseSalary; // base salary
}; // end class BasePlusCommissionEmployee[/code]

Share this post


Link to post
Share on other sites
The colon indicates the start of the member initialization list. The member initialization list contains constructor calls for any base classes as well as member variables. In this case CommisionEmployee(stuff) indicates that the argument list after the type name is being used as the arguments for the base class constructor.

Share this post


Link to post
Share on other sites
The code you post is quite complex so possibly harder to understand this in isolation. Simpler example:

[code]
class A
{
private:
int x;

public:
A(int v) : x(v) { } // sets the member 'x' to the argument
};

class B : public A
{
public:
B(int v) : A(v) { } // passes the argument to A's constructor
};
[/code]

Before a derived class is constructed, its parent class(es) are constructed. If they require parameters, you [i]have[/i] to use the initializer syntax. Using it for members of the actual class is good where possible as it avoids default initializing then assignment. For example:

[code]
// comments do not consider compiler optimisations

class A
{
private:
std::string s;

public:
A(const std::string &v){ s=v; } // s is initialized to "", then assigned the value of v
A(const std::string &v) : s(v) { } // s is initialized with v
};
[/code]

For classes with expensive initializing or assignment, this is more efficient so it is generally good practice to initialize here when you can. It also makes it harder to accidentally use non-initialized values in the body of the constructor.

Something else I like is the way you can avoid nameclash between members and parameters.

[code]
class Vector
{
public:
Vector(int x,int y){ this->x=x; this->y=y; } // yuck
Vector(int ix,int iy{ x=ix; y=iy; } // yuck
Vector(int x,int y) : x(x),y(y) { } // woo hoo!

int x,y;
};
[/code]

Trivial detail but worth being aware of.

Share this post


Link to post
Share on other sites
This isn't overloading, it's just inheritence.

[code]
class BasePlusCommissionEmployee : public CommissionEmployee
{
[/code]

The above line in the header essentially means that the class BasePlusCommissionEmployee inherits from the class CommissionEmployee. This is an "is a" relationaship, or BasePlusCommissionEmployee is a CommissionEmployee

[code]
BasePlusCommissionEmployee::BasePlusCommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate, double salary )
// explicitly call base-class constructor
: CommissionEmployee( first, last, ssn, sales, rate )
{
[/code]

The above code in the constructor basically tells the compiler to pass the constructor arguements first, last, ssn, sales, and rate into the constructor of the base class. This lets the base class logic take care of creating it's own members, and the derived class then only has to deal with the salary parameter. This is just a nice handy way of creating a derived class without having to copy and paste the code from the base class constructor.

Share this post


Link to post
Share on other sites

This topic is 2482 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this