Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6663349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:29:22+00:00 2026-05-26T02:29:22+00:00

#include<iostream> using namespace std; class base { protected: int a; public: base(int i) {

  • 0
#include<iostream>

using namespace std;

class base 
{
protected:
    int a;

public:
    base(int i)
    {
        a=i;
    }
};

class derived :protected base
{
public:
    derived(){}

    void show()
    {
        cout<<a;
    }
};

int main()
{
    base obj(2);
    derived obj1;
    obj1.show();
    return 0;
}

Why is this program giving error as In constructor derived::derived():

error: no matching function for call to base::base()

Please explain. As i have read in stackoverflow that in case of inheriting as protected, protected members of base class became protected member of derived class. If I am wrong then please share a good link to clear my misconception

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:29:22+00:00Added an answer on May 26, 2026 at 2:29 am

    Once you define a constructor for any class, the compiler does not generate the default constructor for that class.

    You define the parameterized constructor(base(int i)) for base and hence the compiler does not generate the no argument constructor for base.

    Resolution:
    You will need to define a constructor taking no arguments for base yourself.
    Add this to your base class:

    base():a(0)
    {
    
    }
    

    EDIT:

    Is it needed to define default constructor to every class? Or is it necessary to have the constructor that matches the derived type also present in base type?

    The answer to both is NO.

    The purpose of constructors in C++ is to initialize the member variables of the class inside the constructor. As you understand the compiler generates the default constructor(constructor which takes no arguments) for every class.

    But there is a catch, If you yourself define (any)constructor(one with parameters or without) for your class, the compiler does not generate the default constructor for that anymore. The compiler reasoning here is “Huh, this user writes a constrcutor for his class himself, so probably he needs to do something special in the constructor which I cannot do or understand”, armed with this reasoning the compiler just does not generate the default no argument constructor anymore.

    Your code above and you assume the presence of the default no argument constructor(When derived class object is created). But since you already defined one constructor for your base class the compiler has applied its reasoning and now it refuses to generate any default argument constructor for your base class. Thus the absence of the no argument constructor in the base class results in the compiler error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<iostream> using namespace std; class base { public: virtual void add() { cout <<
#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
#include<iostream> using namespace std; class A { int a; int b; public: void eat()
Here is my code - #include<iostream> using namespace std; class base { public: void
#include<iostream> using namespace std; class Something { public: int j; Something():j(20) {cout<<Something initialized. j=<<j<<endl;}
#include <iostream> #include <conio.h> using namespace std; class Base { int a; public: Base(const
#include<iostream> using namespace std; class A { public: int i; A() {cout<<A()<<endl;} ~A() {cout<<~A()<<endl;}
#include <iostream> #include <vector> using namespace std; class Base { public: void Display( void
#include <iostream> #include <vector> using namespace std; class base { int x; public: base(int
Consider the following program. #include<iostream> using namespace std; class base { public: int _bval;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.