views:

85

answers:

4

Hello Every one.

Here I can do conversion from emp class object to emp class object. But I can't do conversion from employee class object to emp class object - I have added comment where I am getting error - 'setEmpID' is not a member of 'employee'. what should I do to resolve this error ? ( I am just preparing for C++ exam & this is the only one that I couldn't solve )

Edit - see this is the definition of program - There are two classes Emp and Employee.Emp is defined in the payroll department containing details about employee id and details about his/her payment. Employee isHuman resource department class containing only basic salary details and full personal details like name of spouse, number of children, previous experience of an employee etc. Add code in the Emp class such that, conversion from one type of employee object into another is possible. While converting, items which are not here in the source class (like No. of children when source class is Employee) should take a default value.

#include<iostream.h>
#include<conio.h>
#include<string.h>

class employee;

class emp
{
private:
    unsigned int empID;
public:
    emp(){
        empID=0;
    }

    emp(unsigned int x){
        empID=x;
    }

    emp(employee tmp) {
        // i am getting error here.
        tmp.setEmpID(10);
    }

    void setEmpID(unsigned int x){
        empID=x;
    }
    int getEmpID(){
        return empID;
    }
};


class employee : public emp {
private:
    char name[30];
public:
    employee();
    employee(unsigned int x);
    employee(unsigned int x,char y[]);
    employee(emp tmp);
    void display();
};


employee :: employee()
{
    emp();
    name[0]='\0';
}

employee :: employee(unsigned int x)
{
    emp(x);
    name[0]='\0';
}
employee :: employee(unsigned int x,char y[]) : emp(x)
{
    strcpy(name,y);
}
employee :: employee(emp tmp) : emp( tmp.getEmpID() )
{
    name[0]='\0';
}

void employee ::  display(){
    cout<<"No is -> "<<getEmpID()<<endl<<"Name -> "<<name;
}

void main() {
    clrscr();
    emp e1(10);
    employee e2(10u,"nimita");
    cout<<e1.getEmpID()<<endl;
    e2.display();
    getch();
}

Thanks in advance for sharing your great knowledge.

Sugar

+3  A: 

At the point of call to tmp.setEmpID(10), the definition of class employee is not yet seen by the compiler, since it was just forward declared. So the compiler has no knowledge of the class' methods yet.

In other words this is a cyclical dependency. Luckily it is easy to resolve by moving the implementation of emp(employee tmp) to e.g. the cpp file, where both class definitions are visible.

Péter Török
Yes ! I know that - but what is the way of implement it. The thing that you said is exact I already knew it - I mean what should I do to resolve it.
sugar
@sugar: Move the `emp` constructor definition outside the class, either in a separate file or so that it appears after the declaration of `employee`.
casablanca
sugar
@sugar, yes :-)
Péter Török
@sugar In C++, you generally always want class definitions and implementations to be separate, except when templating is involved.
Tyler McHenry
Yep ! Worked perfectly ! Thanks a lot.
sugar
+1  A: 

It looks like you might need a copy constructor if you are going to be passing in objects by value. Basically a constructor that takes an object by reference to tell the compiler what to do in a situation when a object is passed by value.

Jordan
+1  A: 

Since your base class (emp) has a constructor from a derived class, I'd say that your design is flawed. A base class should never need to know what is in a derived class, which is true of the code you have posted here, so there is no need to pass a derived class to construct a base class.

What you really need to do is create a true copy constructor (I'd recommend using initializers) for your base class, and pass your derived class instance to it in the derived copy constructor, i.e.:

class emp
{
private:
    unsigned int empID;
public:
    emp(): empID(0) { }
    emp(unsigned int x): empID(x) { }
    emp(emp const& tmp): empID(tmp.empID) { }
    virtual ~emp() { }
}

class employee: public emp
{
public:
    employee(): emp() { }
    employee(unsigned int x): emp(x) { }
    employee(employee const& tmp): emp(tmp) { }
}

(note this code does not have the extra assignments for employee that your class has- just an example to see a proper copy constructor for a derived and base class)

MarkD
+1  A: 

you don't must use a derivate class into a base class, instead that use the base class and just use the base functions in that function.

I suggest that read some C++ book, look here

Gustavo V