Ayush
Ayush

Reputation: 49

Initializing data members of base class using the constructor of privately derived class

I am new to c++ programming,I am trying to execute the following code but it shows the error

no matching function for call to ‘Flower::Flower()’ Rose(string n = "No flower", string c = "Red") : color(c) {}

even though i have given parametric constructor in my class Flower still it says no matching function call.

#include <iostream>
#include <string>
using namespace std;


class Flower
{

public:
string name;


Flower (string n):name (n)
  {
  }


void getFlowerName ()
  {
    cout << name << " " << "is" << " ";
} 

};


class Rose:private Flower
{               // Inherit Flower as private
public:
string color;


    /* Initialize name and color data members */ 
Rose (string n = "No flower", string c = "Red"):color (c)
  {
  } 

void getFlowerName (Rose & r)
  {
    cout << r.color << endl;
  } 

    //  using Flower::getFlowerName ;// Allow call to  getFlowerName() method in the base class
};


class Rose:private Flower
{               // Inherit Flower as private
public:
string color;


    /* Initialize name and color data members */ 
Rose (string n = "No flower", string c = "Red"):color (c)
  {
  } 

void getFlowerName (Rose & r)
  {
    cout << r.color << endl;
  } 

using Flower::getFlowerName;    // Allow call to  getFlowerName() method in 
                                   the base class
};

Upvotes: 0

Views: 175

Answers (2)

Zem
Zem

Reputation: 474

Create Flower::Flower() default constructor.

Flower(){ name = NULL; }

Upvotes: 0

Jarod42
Jarod42

Reputation: 218268

Derived classes should call base classes:

class Rose : private Flower
{
public:
    std::string color;

    Rose (const std::string& n = "No flower",
          const std::string& c = "Red") : Flower(n), color(c)
    {
    }
// ...
};

As currently you call implicitly the default Flower's constructor, but it is not existent.

Upvotes: 2

Related Questions