Jump to content
  • Advertisement
Sign in to follow this  
coderWalker

C++ Overloaded Function?

This topic is 2675 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?

// 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


Header:
#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

Share this post


Link to post
Share on other sites
Advertisement
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:


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
};


Before a derived class is constructed, its parent class(es) are constructed. If they require parameters, you have 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:


// 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
};


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.


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;
};


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.


class BasePlusCommissionEmployee : public CommissionEmployee
{


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


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 )
{


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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!