Passing Default Values to Constructor Parameters





This video tutorial explains how to pass default values to constructor parameters.
You will learn how to provide default values to constructor parameters, what happens when we pass default values to constructor parameters in detail with example.

source code for this tutorial

#include <iostream>
#include <string>

using namespace std;

class Human{
private:
    string name;
    int age;

public:
   Human(){
        age = 0;
        cout << "default constructor"<<endl;
        name = "noname";
        age = 0;
    }
    Human(string iname,int iage = 0){
        cout << "overloaded constructor"<<endl;
        name = iname;
        age = iage;
    }

    void speakUp(){
    cout <<name<<endl<<age<<endl;
    }
};


int main()
{
 Human anil("anil");
 anil.speakUp();
    return 0;
}