0
votes

I am new to c++. I have been trying to get past this error. I know when a class in derived, it inherits everything from the base class, but what if the derived class has other data members? How is the constructor suppose to be?

When I try putting only the new I try to pass parameters to the newly made class members in the constructor I get an error to say it doesn't match that of the base class. When I try using that of the base class and adding the new data members it tells me I am redefining. So I wonder whats left to do to get past this error below is my code.

This is the base class:

class movielibrarybase
{


public:
        movielibrarybase(string name, string dirname, string gen, int price);

        void setname(string name);
        string getname();

        void setdirector_name(string dirname);
        string getdirector_name();

        void setgenre(string gen);
        string getgenre();


        void setprice(int price);
        int getprice();

        void display();

        ~movielibrarybase();

    protected:
    string name;
    string director_name;
    string genre;
    int price;
};

And this is the derived class:

class songlibrary: public movielibrarybase
{
    public:
        songlibrary();


        void setartist_name(string name);
        string getartist_name();



        void setsong_position(string position);
        string getsong_position;

        ~songlibrary();


    protected:

    string artist_name;
    string song_postion;
};

I am getting the following errors:

songlibrary.cpp||In constructor 'songlibrary::songlibrary(std::string, std::string, std::string, int, std::string, std::string)':| songlibrary.cpp|6|error: no matching function for call to 'movielibrarybase::movielibrarybase()'| movielibrarybase.cpp|3|note: candidates are: movielibrarybase::movielibrarybase(std::string, std::string, std::string, int)| movielibrarybase.h|8|note: movielibrarybase::movielibrarybase(const movielibrarybase&)| songlibrary.cpp|34|error: no 'std::string songlibrary::getsong_position()' member function declared in class 'songlibrary'|

2
If your computer has a mouse, use it to copy the actual compiler messages and paste them to the question edit box.n. 1.8e9-where's-my-share m.

2 Answers

1
votes

In your case it would be something like:-

songlibrary::songlibrary( string n, string director, string gen, 
                          int pri, string artist, string song )

                     : movielibrarybase( n, director, gen, pri ), 
                       artist_name( artist ), song_postion( song )

One thing to note here is sequence in member initializer list matters. First calls should be to base classes and then derived classes members should be initialized in the order in which they are declared in a class.

Also, class members are initialized in the order of their declaration in the class, the order in which they are listed in a member initialization list makes not a whit of difference

3
votes

For that case you use the member initializer list in the constructor to invoke the right constructor in the base class:

 songlibrary::songlibrary(string name, string dirname, string gen, int price,
         string artist_name, string song_position)
 : movielibrarybase(name, dirname, gen, price), // initialize base class
   artist_name(artist_name), // initialize member in this class
   song_position(song_position) {
   // other ctor stuff
 }

If you do not state the parent class on that list, it will default to the default constructor. Since there is no default constructor in your movielibrarybase class, a compiler error occurred.