derived class constructor syntax

This is a discussion on derived class constructor syntax within the C++ Programming forums, part of the General Programming Boards category; I have written a clock class, with an international clock derived class. I am having problems with the syntax for ...

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    derived class constructor syntax

    I have written a clock class, with an international clock derived class. I am having problems with the syntax for the constructors in the derived class.

    Here is what I've got in the .h:

    Code:
    class clock
    {
    	public:
    
    	clock();
    	clock(string name);
    	clock(int hours, int minutes, bool morning, string name);
    
    	void set_clock (int hour, int minute, bool morning, string name);
    	void set_clock (int hour, int minute, bool morning);
    	void advance (int minutes);
    
    	int conv_time_mins(int hour, int minute);
    
    	int get_hour() const;
    	int get_minutes() const;
    	bool is_morning() const;
    	void display_time();
    
    	private:
    
    	int _hours;
    	int _mins;
    	bool _morn;
    	string _name;
    };
    
    
    
    class iclock : public clock
    {
    	public:
    
    	iclock();
    	iclock(int offset);
    
    
    	void set_clock (int hour, int minute, bool morning);
    	void set_clock (int hour, int minute, bool morning, string name, int offset);
    
    	private:
    
    	int _offset;
    
    	};
    and here is the .cpp

    Code:
    clock::clock()
    {
    	 _hours = 0;
    	 _mins = 0;
    	 _morn = false;
    	 _name.assign("");
    }
    
    clock::clock(string name)
    {
    	_hours = 0;
    	_mins = 0;
    	_morn = true;
    	_name = name;
    }
    
    clock::clock(int hours, int minutes, bool morning, string name)
    {
    	_hours = hours;
    	_mins = minutes;
    	_morn = morning;
    	_name = name;
    }
    
    iclock::iclock()
    	: clock()
    {
    	_offset = 0;
    }
    
    iclock::iclock(int offset) : clock(string name)
    {
    	_offset = offset;
    }
    
    iclock::iclock(int offset)
    	: clock(int hours, int minutes, bool morning, string name)
    {
    	_offset = offset;
    }
    I am getting errors saying that there is no matching function call when I try to create an object of type iclock in my main, like this:

    Code:
    iclock tokyo("Tokyo", 9);
    iclock cairo("Cairo", 2);
    iclock london("London", 0);
    iclock new_york("New York", -5);
    iclock GMT("GMT", 0);
    Any help with the syntax?

    Brian
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    None of your constructors have an adequate parameter list. For the constructor calls you are trying, you need a constructor that takes a string pointer and a signed int:

    Code:
    class iclock : public clock
    {
    	public:
    
    	iclock();
    	iclock(int offset);
                    iclock(LPCHAR,int); //You need this at least
    
    
    	void set_clock (int hour, int minute, bool morning);
    	void set_clock (int hour, int minute, bool morning, string name, int offset);
    
    	private:
    
    	int _offset;
    
    };
    
    iclock::iclock(LPCHAR lpChar,int offset)
    {
    _hours = 0;
    _mins = 0;
    _morn = true;
    _name = lpChar;
    _offset=offset;
    
    }
    I don't know what you're trying to do here:
    Code:
    iclock::iclock(int offset) : clock(string name)
    {
    	_offset = offset;
    }
    
    iclock::iclock(int offset)
    	: clock(int hours, int minutes, bool morning, string name)
    {
    	_offset = offset;
    }
    You're placing a constructor call in an initializer list, which, AFAIK, is illegal. But whatever it is, to make the code work, you don't need it.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    There are several things you can do:
    1. Provide default values for the base class data. To do this, you would leave your declarations the same, but in your implementation you would do this:
      Code:
      iclock::iclock(int offset) : clock("Default Name")
      {
          _offset = offset;
      }
      This is not appropriate for your example usage.
    2. You could also allow the user to pass in values for both the base class data and the derived class data. To do this for your example usages (e.g. iclock tokyo("Tokyo", 9);) look at this code:
      Code:
      class iclock : public clock
      {
          public:
      
          iclock();
          iclock(string name, int offset);
          iclock(int hours, int minutes, bool morning, string name, int offset);
          // ...
      };
      
      // Implementation
      iclock::iclock(string name, int offset) : clock(name)
      {
          _offset = offset;
      }
      
      iclock::iclock(int hours, int minutes, bool morning, string name, int offset) : clock(hours, minutes, morning, name)
      {
          _offset = offset;
      }
      Notice how you are adding the required information to the parameter list, and then you are calling the appropriate base class constructor in the initialization list (yes, this is legal if you do it this way) in order to allow the base class to do whatever it wants with the data that is specific to it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. call base class function or derived class function
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 05:23 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  4. Replies: 4
    Last Post: 12-28-2002, 11:29 PM
  5. Constructors + Derived class
    By MethodMan in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2002, 04:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21