Thread: Inherited constructors with parameters

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    Inherited constructors with parameters

    I am trying to use inheritance and would like the derived class (employee) to be created using the base class (person) constructor which has parameters. This doesn't complie and I get error C2661: 'employee::employee' : no overloaded function takes 2 parameters Error executing cl.exe. If I use the default constructor then other member functions from the base class behave as I would expect (print). Are constructors a special case, or have I missed something ?

    Code:
    //person.h
    #include <string>
    using namespace std;
    
    class person  
    {
    public:
    	person();	//default constructor
    	person(string n, int a);
    	~person();
    
    	void set_name(string n);
    	void have_birthday();
    	void print();
    
    private:
    	int age;
    	string name;
    };
    
    class employee : public person
    {
    public:		
    	~employee();
    	void set_employee_number(int e);
    	int get_employee_number();
    
    private:
    	int employee_number;
    
    };
    
    //person.cpp
    #include <iostream>
    #include "person.h"
    
    //person
    
    
    person::person()
    :name("No Name"),age(0)
    {
    	cout << "person default constructor" << endl;
    }
    
    person::person(string n, int a)
    :name(n),age(a)
    {
    	cout << "person constructor: " << name << endl;
    }
    
    person::~person()
    {
    
    	cout << "person destructor:" << name << endl;
    }
    
    
    void person::set_name(string n)
    {
    	cout << name << " changed to " << n << endl;
    	name = n;
    
    }
    
    void person::have_birthday()
    {
    	cout << "Happy Birthday " << name << endl;
    	age++;
    }
    
    void person::print()
    {
    	cout << name << " is " << age << endl;
    }
    
    //employee
    
    
    employee::~employee()
    {
    	cout << "Employee destructor" << endl;
    
    }
    
    
    void employee::set_employee_number(int e)
    {
    	employee_number = e;
    }
    	
    int employee::get_employee_number()
    {
    	return(employee_number);	
    }
    
    	
    //test.cpp
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    #include "person.h"
    
    int main()
    {
    
    
    	person me;
    	//me.print();
    	me.set_name("Dave");
    	me.have_birthday();
    	me.print();
    	
    	person you("Karen",28);	
    	you.have_birthday();
    	you.print();
    
    	//employee worker;
    	//worker.set_name("Worker");
    	employee worker("Worker",30);  //no overloaded function takes 2 parameters
    	worker.print();
    	worker.set_employee_number(123);
    	cout << "Employee Number" << worker.get_employee_number() << endl;
    
    	return 0;
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    class A
    {
        int m_n;
    public:
        A(int n) : m_n(n) {}
    };//A
    
    class B : public A
    {
        int m_b;
    public:
        // B constructor must initialize A
        B(int b) : A(b) {}
    };//B
    
    int main()
    {
        B b(3);
    
        return 0;
    }//main
    gg

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    Many thanks

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    12

    Re: Inherited constructors with parameters

    Originally posted by dave74

    Code:
    //person.h
    #include <string>
    using namespace std;
    
    class person  
    {
    public:
    
                person(string n = "No Name", int a=0);
    	//This should also take care of it, and you won't have to create a subclass...
    	~person();
    
    	void set_name(string n);
    	void have_birthday();
    	void print();
    
    private:
    	int age;
    	string name;
    };
    
    
    //person.cpp
    #include <iostream>
    #include "person.h"
    
    //person
    
    
    person::person(string n, int a)
    :name(n),age(a)
    
    {
    	cout << "person constructor: " << name << endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 03:35 PM
  2. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 03:13 AM
  3. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM
  4. Do constructors get inherited?
    By Shadow12345 in forum C++ Programming
    Replies: 28
    Last Post: 08-21-2002, 11:41 AM
  5. Default Constructors w/Classes as Parameters
    By GrNxxDaY in forum C++ Programming
    Replies: 22
    Last Post: 07-31-2002, 07:50 AM