Lucas Jones
Lucas Jones

Reputation: 17

Code won't inherit and I have no idea why

this is my first question here so do be kind :)

I am writing a program to take in names, surnames and numbers/email addresses and have to use inheritance to create a class person_with_telephone from the base class Person.

I have tried and even come close but there are always those few errors that pop up and since I'm new to C++ I don't know what they mean.

Here is the code in question:

class Person
{

private:
  string m_FirstName, m_LastName,  m_email, m_telephone;

public:
  Person(const string& firstName, const string& lastName, const string telephone) :
    m_FirstName(firstName), m_LastName(lastName), m_telephone(telephone)
        {}

        string get_name() const
        {
                return m_FirstName;
        }
        string get_surname() const
        {
                return m_LastName;
        }

        bool has_telephone_p()
        {
                if (m_telephone == "")
                {
                  return false;
          cout << "You have no phone number registered" << endl;
                }

                else
                {
                  return true;
          cout << "Your number is: " << m_telephone << endl;
                }
        }

       string get_telephone() const
        {
                 return m_telephone;
        }

        bool has_email_p()
        {

        }
};

class Person_with_telephone: public Person
{

private:
  string m_telephone;

public:
  Person(const string& telephone) : m_telephone(telephone)
  {};

  string set_telephone()
  {

  }

  string get_telephone()
  {

  }


};

Ignore the empty member functions, those come later. Any ideas as to why I get the errors:

main.cc: In constructor ‘Person_with_telephone::Person_with_telephone(const string&)’:
main.cc:59:73: error: no matching function for call to ‘Person::Person()’
   Person_with_telephone(const string& telephone) : m_telephone(telephone)
                                                                         ^
main.cc:59:73: note: candidates are:
main.cc:13:3: note: Person::Person(const string&, const string&, std::string)
   Person(const string& firstName, const string& lastName, const string telephone) :
   ^
main.cc:13:3: note:   candidate expects 3 arguments, 0 provided
main.cc:6:7: note: Person::Person(const Person&)
 class Person
       ^
main.cc:6:7: note:   candidate expects 1 argument, 0 provided
<builtin>: recipe for target 'main' failed
make: *** [main] Error 1

Compilation exited abnormally with code 2 at Tue Dec  1 10:17:39

Thanks for any help! :)

Upvotes: 1

Views: 56

Answers (3)

Temple
Temple

Reputation: 1631

In derived class You have to call Base contructor explicitly, if You won't it is called implicit, in this case it will be called inplicit and when that happens default one from base will be called. In Youre base You have declared constructor taking 3 params and default has been not created by that, so that what error is saying. You have to create default one in base if You don't wanna call it expilitly, or call the one You have defined in derived initialization list.

Upvotes: 0

Unick
Unick

Reputation: 674

Looks like you have wrong constructor of Person_with_telephone. It should have name Person_with_telephone. Also it also should call constructor of Person, because it has no default constructor. Also it is strange because your Person class has m_telephone field. Try this constructor:

Person_with_telephone(const string& firstName, const string& lastName, const string telephone) : Person(firstName,lastName, telephone), m_telephone(telephone)
  {};

Maybe you need remove m_telephone from Person.

Upvotes: 0

David Schwartz
David Schwartz

Reputation: 182819

A Person_with_telephone is a Person. Thus a constructor for a Person_with_telephone is also constructing a Person. You have no default constructor it can call, so you have to supply the parameters to the Person constructor.

This is the syntax:

class Int
{
  public:
    int j;
    Int (int q) : j(q) { ; }
};
class IntAndString : public Int
{
  public:
    std::string t;
    IntAndString(int q, std::string s) : Int(q), t(s) { ; }
};

Also, for some reason, both Person_with_telephone and Person have an m_telephone member. That will cause you no end of pain and confusion. If they're both supposed to have a member like that, give them different names.

Upvotes: 2

Related Questions