Thursday 16 March 2017

/ If Copy constructor specified within a class definition by programmer than any explicit call of constructor is not valid 

#include<iostream>
using namespace std;

class Complex
{
int real,img;
public:
Complex()
{
real=0;
img=0;
}
Complex(int,int); //Constructor declaration is also allowed here as like normal member function 
Complex(Complex & x) // Copy constructor specified within class definition 
{
real=x.real;
img=x.img;
}
void putdata()
{
cout<<real<<"+"<<img<<"i"<<endl;
}
};
Complex::Complex(int r,int i) //Syntax to define constructor outside the class instead of inline constructor 
{
real=r;
img=i;
}
int main()
{
Complex c1; //Call default Constructor implicit call
Complex c2= Complex(10,20); //Call constructor with two int parameters (Explicit call). Error as explicit call of constructor 
//will be invalid as programmer also specified copy constructor within class definition. 
Complex c3(c2); 
c1.putdata();
c2.putdata();
c3.putdata();
}
//Destructor destroy object in LIFO fashion (Last in First out) as object go out of the scope. 
//Destructor call by compiler when any object created by user goes out of the scope.
//Syntax of desctructor is same expect;
// There is no any parameter in destructor.
// ~ sign before the definition of function. please refer code for prototype of destructor. 
//Run the code and observe the output write your views on comments. 
#include<iostream>
using namespace std;

class Alpha
{
static int count;
public:
Alpha() // Default constructor
{
count++;
cout<<"Object created:"<<count<<endl;
}
~Alpha()
{
cout<<"Object Destroyed:"<<count<<endl;
count--;
}
};
int Alpha::count;
int main()
{
Alpha a1,a2,a3,a4;
{
Alpha a5;
}
{
Alpha a6;
}
}
#include<iostream>
using namespace std;

class Complex
{
int real;
int img;
public:
Complex(int r, int i) // parameterized Constructor
{
real=r;
img=i;
}
void putdata()
{
cout<<real<<"+ "<<img<<"i"<<endl;
}
};
int main()
{
Complex c1; // Error as no matching function call, 
//Reason: Compiler could not find appropriate constructor to call i.e. a constructor with no parameter 
//Rules: If no constructor is defined within class defination than compiler can only call default constructor automatically //otherwise not 
// Solution: Create Define Default constructor within class defination i.e a constructor with no parameters.
// note: Default constructor is also called parameter less constructor 

Complex c2(10,200); //Correct
c1.putdata();
c2.putdata();
}
// Use of defualt parameters in constructor 
#include<iostream>
using namespace std;

class Complex
{
int real;
int img;
public:
Complex(int r=0, int i=0) // parameterized Constructor with default parameters
{
real=r;
img=i;
}
void putdata()
{
cout<<real<<"+ "<<img<<"i"<<endl;
}
};
int main()
{
Complex c1,c2(10),c3(10,20); //Correct
// Case c1: Compiler will call Parameterized Constructor with default value of r and i as 0 and 0 respectively
// Case c2: Compiler will call the same Constructor with value of r as 10 and i as 0(i is default in this case)
// Case c3: Compiler will call the same Constructor with value of r as 10 and i as 20
c1.putdata();
c2.putdata();
c3.putdata();
}
// Function overloading concepts rules are also applicable to Constructor as it is also a function with the same name to its //class
#include<iostream>
using namespace std;

class Complex
{
int real;
int img;
public:
Complex(int r=0, int i=0) // parameterized Constructor with default parameters
{
real=r;
img=i;
}
Complex() // Compiler generates ambiguity error as both parameters have default value in parameterized constructor
{
real=0;
img=0;
}
void putdata()
{
cout<<real<<"+ "<<img<<"i"<<endl;
}
};

int main()
{
Complex c1,c2(10),c3(10,20); //Ambiguity error
c1.putdata();
c2.putdata();
c3.putdata();
}
Note: Compiler only generates ambiguity error if and only if we create an object with no parameters i.e. if the statements in main function is as follows;
Complex c2(10),c3(10,20); //OK, no error as there is no any object with default arguments that leads to ambiguity;
c2.putdata();
c3.putdata();
then there will be no any errors in the code as function overloading is compiled time binding and always tries to bind the function call with definition during the compilatoin process.
Class to Class Type Conversion -through Casting Operator Function (Overload Destination Class in Source Class)
#include<iostream>
#include<iomanip>
using namespace std;
class KG;
class POUND
{
private:
float lb;
public:
POUND(){}
void get();
void put();
float & get_lb(){ return lb; } // reference function for lb member
};
class KG
{
private:
float kg;
public:
KG(){}
void get();
void put();
operator POUND()
{
POUND P1;
P1.get_lb() =kg/2.20462;
return P1;
}
};
void KG::get()
{
cout<<"enter value of kg:";
cin>>kg;
}
void KG::put()
{
cout<<"kg = "<<kg<<endl;
}
void POUND::get()
{
cout<<"enter value of lb:";
cin>>lb;
}
void POUND::put()
{
cout<<"lb = "<<lb<<endl;
}
int main()
{
KG K;
K.get();
POUND P;
//P.get();
P=K;
P.put();
return 0;
}