Constructor Overloading C++

in #programming9 years ago

Very Simple Program for constructor overloading in c++

firstly i created a class with default constructor.
then i write code for overload constructor and in this constructor i have to formal arguments
then in next step i created an object “obj” to invoke class with default arguments.
and in last step i created an object “obj1” to invoke class again with to value.

?

include< iostream >;

using namespace std;
class cOverload{

private:
    int res, val1, val2;
public:
     
    //Default Constructor
    cOverload(){
         
         
        val1 = 10;
        val2 = 20;
         
        cout<<"Default Constructor: "<<val1<<" and "<<val2<<"\n\n";
         
    }   
     
    // Constructor OverLoading
     
    cOverload( int a , int b){
         
        val1 = a;
        val2 = b;
         
        cout<<"Overload Constructor: "<<val1<<" and "<<val2<<"\n\n";
         
    }

};

int main(){

cOverload obj; //object created with default constructor;
 
cOverload obj2(40,50); // object created with overLoaded constructor;
 
 
return 0;

}

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.092
BTC 63551.01
ETH 1783.00
USDT 1.00
SBD 0.39