/*
 * Global Tech's employee system
 */

#include<iostream>
using std::cout;
using std::cin;
using std::endl;

#include "Person.h"

int main(){

  Person employee("Sue" , "Jones");

  cout << "Employee Info " << employee.getFirstName() << " " << employee.getLastName() << endl;

  return 0;


}

/*
 * Person Class Definition
 */

#include <string>
using std::string;

class Person{

 public:
  Person(const string &, const string &);

  void setFirstName(const string &);

  void setLastName(const string &);

  string getFirstName() const;

  string getLastName() const;

  void print() const;

 private:
  string firstName;
  string lastName;

};

 /*
 * Class for person
 */

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

#include "Person.h"//person class definition

/**
 * Constructor
 */

Person::Person(const string &first, const string &last) : firstName( first ), lastName( last )
{
  
  setFirstName(first);

  setLastName(last);

}

//set First Name
void Person::setFirstName(const string &first){

  firstName = first;

}

//set last name
void Person::setLastName(const string &last){

  lastName = last;

}

//return first name
string Person::getFirstName() const{

  return firstName;

}

//return last name
string Person::getLastName() const{

  return lastName;

}

//print Person object
void Person::print() const{

  cout << "Person: " << firstName << ' ' << lastName;

}

 /*
 * header file for Employee
 */

#include <string>
using std::string;

#include "Person.h"

class Employee : public Person{

 public:
  Employee(int = 000000000);

  void setEmployeeID( int );
  int getEmployeeID();

  void print();

 private:
  int employeeID;

};

*
 * Class Employee member function definition
 */

#include <iostream>
using namespace std;

#include "Employee.h"

/**
 * Constructor
 */

Employee::Employee( int eID) : Person(first , last){

  setEmployeeID( eID );

}

void Employee::setEmployeeID( int eID){

  employeeID = eID;

}

int Employee::getEmployeeID(){

  return employeeID;

}

void Employee::print(){

  cout << "Employee" << endl;

  Person::print();

  cout << "\nEmployee ID: " << getEmployeeID();

}

When I compile this I get

[TEX]
Employee.cpp: In constructor 'Employee::Employee(int)':
Employee.cpp:20: error: 'first' was not declared in this scope
Employee.cpp:20: error: 'last' was not declared in this scope
[/TEX]

I cant figure out why it doesnt recognize first and last. Any ideas?

Recommended Answers

All 4 Replies

I cant figure out why it doesnt recognize first and last. Any ideas?

Uh... first and last are local parameters to the constructor in the Person class. They are not at all visible to the derived class Employee.

If you want this to work you'll have to change Employee's constructor in this way--

class Employee : public Person{

 public:
  Employee(int = 000000000, string first = "N/A", string last = "N/A");

  void setEmployeeID( int );
  int getEmployeeID();

  void print();

 private:
  int employeeID;

};

--if you dont want default values for the first and last name I believe you will either have to provide another constructor that declares the strings first then a default-int or replace the current constructor with the same constructor suggested.

Tried changing the employee constructor like was shown above but I still get the same error.

Did you change the definition of the constructor as well?

class Employee : public Person{
public:
  Employee(int eID = 000000000, string first = "N/A", string last = "N/A");

  ...
};

Employee::Employee(int eID, string first, string last)
  : Person(first, last)
{
  setEmployeeID( eID );
}

maybe this can help...vote me...

// constructor
Employee::Employee( int eID) : setEmployeeID( eID )
{
   // :-) 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.