C++ இல் உள்ள எடுத்துக்காட்டு நிரல்கள் - C++ ஆக்கி பணிமிகுப்பு | 11th Computer Science : Chapter 15 : Polymorphism
ஆக்கி பணிமிகுப்பு Constructor overloading
இனக்குழுவின் சிறப்பு செயற்கூறுகளான ஆக்கிகளையும், செயற்கூறு பணிமிகுப்பு செய்ய முடியும். ஓர் இனக்குழுவில் வெவ்வேறு வரையறுப்புகளைக் கொண்ட ஒன்றுக்கு மேற்பட்ட ஆக்கிகள் இடம் பெறலாம். ஓர் இனக்குழுவிற்கு, பல்வேறு வகையான பொருள்களை உருவாக்க ஆக்கி பணிமிகுப்பு வழி வகை செய்கிறது.
நிரல் 15.3 ஆக்கி பணிமிகுப்பை விளக்கும் நிரல்
Compiler identifies a given member function is a constructor by its name and the return type.
#include<iostream>
using namespace std;
class add
{
int num1, num2, sum;
public:
add()
{
cout<<"\n Constructor without parameters.. ";
num1= 0;
num2= 0;
sum = 0;
}
add ( int s1, int s2 )
{
cout<<"\n Parameterized constructor... ";
num1= s1;
num2=s2;
sum=0;
}
add (add &a)
{
cout<<"\n Copy Constructor ... ";
num1= a.num1;
num2=a.num2;
sum = 0;
}
void getdata()
{
cout<<"\nEnter data ... ";
cin>>num1>>num2;
}
void addition()
{
sum=num1+num2;
}
void putdata()
{
cout<<"\n The numbers are..";
cout<<num1<<'\t'<<num2;
cout<<"\n The sum of the numbers are.. "<< sum;
}
};
int main()
{
add a, b (10, 20) , c(b);
a.getdata();
a.addition();
b.addition();
c.addition();
cout<<"\n Object a : ";
a.putdata();
cout<<"\n Object b : ";
b.putdata();
cout<<"\n Object c.. ";
c.putdata();
return 0;
}
Constructor without parameters..
Parameterized constructor...
Copy Constructor ...
Enter data ... 20 30
Object a :
The numbers are..20 30
The sum of the numbers are.. 50
Object b :
The numbers are..10 20
The sum of the numbers are.. 30
Object c..
The numbers are..10 20
The sum of the numbers are.. 30
//constructor declared as outline member function
#include<iostream>
using namespace std;
class Perimeter
{
int l, b, p;
public:
Perimeter ();
Perimeter (int);
Perimeter (int,int);
Perimeter (Perimeter&);
void Calculate();
};
Perimeter :: Perimeter ()
{
cout<<"\n Enter the value of length and breath";
cin>>l>>b;
cout<<"\n\nNonParameterizd constructor ";
}
Perimeter ::Perimeter (int a)
{
l=b=a;
cout<<"\n\n Parameterizd constructor with one argument ";
}
Perimeter ::Perimeter (int l1, int b1)
{
cout<<"\n\n Parameterizd constructor with 2 argument ";
l=l1;
b=b1;
}
Perimeter ::Perimeter (Perimeter &p)
{
l= p.l;
b= p.b;
cout<<"\n\n copy constructor ";
}
void Perimeter ::Calculate(){
p = 2*(l+b);
cout<<p;
}
int main ()
{
Perimeter Obj;
cout<<"\n perimeter of rectangle is ";
Obj. Calculate ();
Perimeter Obj1(2);
cout<<"\n perimeter of rectangle ";
Obj1.Calculate ();
Perimeter Obj2 (2, 3);
cout<<"\n perimeter of rectangle ";
Obj2.Calculate ();
Perimeter obj3 (Obj2);
cout<<"\n perimeter of rectangle ";
obj3.Calculate ();
return 0;
}
Enter the value of length and breath 10 20
Non Parameterizd constructor
perimeter of rectangle is 60
Parameterizd constructor with one argument
perimeter of rectangle 8
Parameterizd constructor with 2 argument
perimeter of rectangle 10
copy constructor
perimeter of rectangle 10
குறிப்பு
ஆக்கிகள் பல இடம் பெற்றிருப்பதால், ஒரு பொருள் உருவாக்கப்படும் போதே செயலுருபுகள் அவற்றிற்கு அனுப்பி வைக்கப்பட வேண்டும்.