9 Replies - 851 Views - Last Post: 01 July 2012 - 02:13 PM Rate Topic: -----

#1 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Base class constructor causing error.

Posted 30 June 2012 - 07:05 PM

Employee and ProductionWorker Classes.

Instructions:
... Write one or more constructors and the appropriate accessor and mutator functions for the class. Write one or more constructors and the appropriate accessor and mutator functions for the class. Demonstrate the classes by writing a program that uses a ProductionWorker object.

Unfortunately the base constructor with the derived class in line 56, is causing an error.

Line 85 refuses to pass the name John Doe.

I also tried the following combination for the int main, and it said it couldn't find payment in the Employee class. It looks like it can distinguish between emp1 for Employee and ProdWorker
	Employee emp1("John Doe", 2);
	ProductionWorker emp1(2, 37);		//shift type, work hours

	// Print out name and pay (based on 40 hours work).
	cout << "Name: " << emp1.getName() << endl;
	cout << "Pay: " << emp1.payment(40.0) << endl;



The full code
#include <iostream>
#include <string>
using namespace std;

class Employee
{
protected:
	string name;
	int number;
	int date;

public:
	Employee()
	{	name = " ";
		number = 0;	
		date = 0;	}

	//Employee(string na, int nu, int d)
	//{	//name = na;
		//number = nu;
		//date = d;	
		//set(name, number, date);	}

	//mutators
	void setName(string na)
	{	name = na;		}

	void setNum(int nu)
	{	number = nu;	}

	void setDate(int d)
	{	date = d;		}

	string getName() const
	{	return name;	}

	int geNumber() const
	{	return number;	}

	int getDate() const
	{	return date;	}
};


class ProductionWorker : public Employee
{
private:
	int shift;
	double rate;

public:
	ProductionWorker() : Employee()
	{	shift = 1;
		rate = 0.0;	}

	ProductionWorker(int s, double r)	:	Employee(na, nu, d)
	{	shift = s;
		rate = r;	}

	void setShift(int s)
	{	shift = s;	}

	void setRate(int r)
	{	rate = r;	}

	string getShift(int s) const
	{	string shiftType;

		if (shift == 1)
			shiftType = "Day";
		if (shift == 2)
			shiftType = "Night";

		return shiftType;	}

	double getRate() const
	{	return rate;	}

	double payment(double h) const	//h = hoursworked
	{	return h * rate;	}
};

int main()
{
	Employee emp1("John Doe", 2);
	ProductionWorker pw1(2, 37);		//shift type, work hours

	// Print out name and pay (based on 40 hours work).
	cout << "Name: " << emp1.getName() << endl;
	cout << "Pay: " << pw1.payment(40.0) << endl;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Base class constructor causing error.

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: Base class constructor causing error.

Posted 30 June 2012 - 10:29 PM

Line 56 is failing because you have lines 18-22 commented out.

Line 85 is failing because you don't have an Employee constructor that takes just a string and a integer.
Was This Post Helpful? 1
  • +
  • -

#3 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Base class constructor causing error.

Posted 30 June 2012 - 11:28 PM

Thanks for pointing out my careless mistakes. At one point I decided to use just the following statement for the constructor function: set(name, number, date)

But I decided I'd like to keep thing simpler/more amateurish manner, and must of have forgotten to remove the comment marks.

The error of line 56 (in this case 55), still persists with the same error, stating the arguments of Employee are undefined.

#include <iostream>
#include <string>
using namespace std;

class Employee
{
protected:
	string name;
	int number;
	int date;

public:
	Employee()
	{	name = " ";
		number = 0;	
		date = 0;	}

	Employee(string na, int nu, int d)
	{	name = na;
		number = nu;
		date = d;		}

	//mutators
	void setName(string na)
	{	name = na;		}

	void setNum(int nu)
	{	number = nu;	}

	void setDate(int d)
	{	date = d;		}

	string getName() const
	{	return name;	}

	int geNumber() const
	{	return number;	}

	int getDate() const
	{	return date;	}
};


class ProductionWorker : public Employee
{
private:
	int shift;
	double rate;

public:
	ProductionWorker() : Employee()
	{	shift = 1;
		rate = 0.0;	}

	ProductionWorker(int s, double r)	:	Employee(na, nu, d)
	{	shift = s;
		rate = r;	}

	void setShift(int s)
	{	shift = s;	}

	void setRate(int r)
	{	rate = r;	}

	string getShift(int s) const
	{	string shiftType;

		if (shift == 1)
			shiftType = "Day";
		if (shift == 2)
			shiftType = "Night";

		return shiftType;	}

	double getRate() const
	{	return rate;	}

	double payment(double h) const	//h = hoursworked
	{	return h * rate;	}
};

int main()
{
	Employee emp1("John Doe", 2, 2011);
	ProductionWorker pw1(2, 37);		//shift type, work hours

	// Print out name and pay (based on 40 hours work).
	cout << "Name: " << emp1.getName() << endl;
	cout << "Pay: " << pw1.payment(40.0) << endl;
        return 0;
}

This post has been edited by mgrex: 30 June 2012 - 11:50 PM

Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,204
  • Joined: 21-June 11

Re: Base class constructor causing error.

Posted 01 July 2012 - 02:44 AM

What are the values of na, nu and d supposed to be on line 55?
Was This Post Helpful? 0
  • +
  • -

#5 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: Base class constructor causing error.

Posted 01 July 2012 - 06:46 AM

EDITED: Are you getting the error:

test.cpp: In constructor 'ProductionWorker::ProductionWorker(int, double)':
test.cpp:55:47: error: 'na' was not declared in this scope
test.cpp:55:51: error: 'nu' was not declared in this scope
test.cpp:55:55: error: 'd' was not declared in this scope

This post has been edited by axnjxn: 01 July 2012 - 07:00 AM

Was This Post Helpful? 0
  • +
  • -

#6 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: Base class constructor causing error.

Posted 01 July 2012 - 07:21 AM

I'm fairly certain that you need to include parameters in your inherited class ProductionWorker to account for the Employee members.

ProductionWorker(string na, int nu, int d, int s, double r) :
                Employee(na, nu, d), shift(s), rate(r) {}



And in your main function, you need to set these values in the constructor call

ProductionWorker pw1("Frank", 5, 2012, 2, 37);



If you wanted to use the name, employee #, and date from the Employee instance emp1, you need to make a constructor that takes an Employee object as an argument and pass the emp1 as a parameter. You will also need a copy constructor (I'm not sure if the compiler does this implicitly someone jump in on this, please).

//copy constructor in Employee class
Employee(const Employee &em)
{   name = em.name;
    number = em.number;
    date = em.date;
}

//constructor in ProductionWorker
ProductionWorker (const Employee &em, int s, double r) : Employee (em)
{
    shift = s;
    rate = r;
}

//call to constructor in main()
Employee emp1("John Doe", 2, 2011);
ProductionWorker pw2(emp1, 2, 100);


This post has been edited by axnjxn: 01 July 2012 - 07:22 AM

Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,204
  • Joined: 21-June 11

Re: Base class constructor causing error.

Posted 01 July 2012 - 07:42 AM

View Postaxnjxn, on 01 July 2012 - 04:21 PM, said:

I'm fairly certain that you need to include parameters in your inherited class ProductionWorker to account for the Employee members.


Either that or he needs to pass constants as arguments to the Employee constructor or get the arguments from somewhere else. Either way he can't just use na, nu and d if he doesn't define them anywhere where they are in scope for the ProductionWorker constructor.


Quote

If you wanted to use the name, employee #, and date from the Employee instance emp1, you need to make a constructor that takes an Employee object as an argument and pass the emp1 as a parameter.


It could also pass the individual members as parameters, like this:

ProductionWorker (const Employee &em, int s, double r) : Employee (em.name, em.number, em.date)


There's not really a good reason to do it this way, I just wanted to point it out as an example where he keeps using the 3-argument Employee constructor.


Quote

You will also need a copy constructor (I'm not sure if the compiler does this implicitly someone jump in on this, please).


Yes, the compiler will automatically define a copy constructor if you don't. The auto-generated constructor will initialize each member of the constructed object using the corresponding member of the argument, so it will act the same way as the copy constructor that you defined (except that your constructor performs some unnecessary default construction).

You only need to define your own copy constructor if the constructor needs to do something different than just invoking the copy constructor of each member.

This post has been edited by sepp2k: 01 July 2012 - 07:44 AM

Was This Post Helpful? 1
  • +
  • -

#8 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: Base class constructor causing error.

Posted 01 July 2012 - 08:44 AM

Thanks. I thought that was the case. I took out the copy constructor and added in an overloaded << operator to provide full output for each class. Has the OP seen these posts? It seems that I've learned from his topic, but I hope he has as well. ;)
Was This Post Helpful? 0
  • +
  • -

#9 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Base class constructor causing error.

Posted 01 July 2012 - 01:17 PM

Thanks for the response, axnjxn, the program compiled.

I noticed though that the base class didn't need the copy constructor, whereas, the derived, ProductionWorker, class needed, especially due to this unique statement:
ProductionWorker pw1(emp1, 2, 37); //employee, shift type, work hours

Thanks again.

This post has been edited by mgrex: 01 July 2012 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#10 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: Base class constructor causing error.

Posted 01 July 2012 - 02:13 PM

No problem, man. Hope I can help again.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1