Showing posts with label Polymorphism. Show all posts
Showing posts with label Polymorphism. Show all posts

Monday, May 13, 2024

সাবটাইপিং

Subtyping: এখানে এমন একটি function কাজ করে যা argument হিসেবে নেয় কেবল একটি নির্দিষ্ট type ও তার subtype কে। Object oriented programming এ এটি subclassing (inheritance) এর মাধ্যমে হয়ে থাকে।
উদাহরণে Nationality abstract class এবং তার subclass UK,Bangladesh, China এর সাহায্যে আমরা Demo class এর showNat() মেথডের মাধ্যমে নাগরিকত্ব দেখাতে চাচ্ছি। এজন্য এখানে Nationality'র object কে argument হিসেবে pass করা হচ্ছে। তারপর প্রযোজ্য subclass এর des() কে call করা হচ্ছে। C++ এ pointer arithmetic এর মাধ্যমে এটি করা হচ্ছে। এখানে Nationality base/abstract class এর des() কে virtual function () ঘোষণা করা হয়েছে যেন ভিন্ন ভিন্ন subclass এর object এর des() ভিন্ন ভিন্ন বার্তা/কাজ দেখাতে পারে। এখানে একই function des() কে call করলে বিভিন্ন subclass এর object অনুযায়ি এটি আলাদা আলাদা আচরণ করছে। এখানে n হলো একটি Nationality pointer object। এবং n নির্দেশ(point) করছে কোন একটি subclass এর object এর addressকে। কাজেই সরাসরি subclass এর object এর মাধ্যমে des() কে call না করে Pointer এর মাধ্যমে এটি করা হচ্ছে।

উদাহরণ(C++):
#include ‹iostream›
#include ‹string›
class Nationality{
public:
Nationality (){}
virtual std::string des()=0;
};
class UK:public Nationality{
public:
UK(){}
std::string des(){
return "British\n";
}
};
class Bangladesh:public Nationality{
public:
std::string des(){
return "Bangladeshi\n";
}
};
class China:public Nationality{
public:
China(){}
std::string des(){
return "Chinese \n";}
};
class Demo {
public:
Demo(){}
void showNat(Nationality *n){
std::cout«n->des();
}
};
int main()
{
China c;
Demo a;
Nationality * n=&c;
a.showNat(n);
Bangladesh b;
n=&b;
a.showNat(n);
return 0;
}

JavaScript যেহেতু type safe language তাই আমাদের এখানে compile time এ Demo class এ একটি Nationality object তৈরি করা হয়েছে,আর তারপর তাকে assign করা হচ্ছে showNat() এর argument কে । এরপর runtime এ যে object ই pass করা হচ্ছে তার des() এর বার্তা আমরা screen এ দেখতে পাচ্ছি। কারণ সব object ই Nationality class এর subclass।

উদাহরণ(JavaScript):
class Nationality{
constructor(){}
des(){}
}
class Bangladesh extends Nationality {
constructor(){super();}
des(){
return " Bangladeshi \n";
}
}
class Uk extends Nationality {
constructor(){super();}
des(){
return " British \n";
}
}
class China extends Nationality {
constructor(){super();}
des(){
return "Chinese\n";
}
}
class Demo{
constructor (){
this.s=new Nationality();
}
showNat(n){
this.s=n;
document.write(this.s.des());
}
}
var a=new Demo();
a.showNat(new China());
a.showNat(new Bangladesh());

কৃতজ্ঞতাঃ এই লেখা তৈরি করতে দরকারি তথ্য নেয়া হয়েছে Wikipedia, internet ও আমার তৈরি করা নানা program থেকে।
মন্তব্য ও যোগাযোগঃ mrh4478@gmail.com

প্যারামেট্রিক পলিমরফিজম

Parametric Polymorphism: এখানে function কে এমন abstract ভাবে লেখা হয় যেন function টি বিভিন্ন value কে argument হিসেবে নিতে পারে তাদের data type এর উপর নির্ভর না করে। উদাহরণে C++ এ template এর মাধ্যমে add() function টিতে যেকোন data type কে argument হিসেবে নেবার সুযোগ করে দেয়া হয়েছে। add() প্রথমে দু'টি integer কে argument হিসেবে নিয়েছে, পরে দু'টি string কে এবং শেষে একটি string ও একটি integer কে argument হিসেবে নিয়েছে।

উদাহরণ(C++):
#include ‹iostream›
#include ‹string›
class Demo{
public:
Demo(){}
template ‹class T›
T add(T a,T c){
return a+c;
}
};
int main()
{
Demo m;
int w=5,y=7;
//add(int,int)
std::cout «m.add(5,7);
std::string s="Hello, ",z="there!";
//add(string,string)
std::cout«"\n"«m.add(s,z);
//add(string, int)
std::cout«"\n"«m.add("May",13);
return 0;
}

JavaScript উদাহরণটিতে add() এ কোন template বা data type ব্যবহার করা হয়নি কারণ JavaScript একটি type safe language অর্থাৎ compile time এ এটি data type check না করে একেবারে run time এ check করছে। যার কারণে run time এ যাই add() এ pass করা হচ্ছে তাই সে screen এ দেখাচ্ছে।

উদাহরণ(JavaScript):
class Demo{
constructor(){}
add(a,c){
return a+c;
}
}
var m=new Demo();
document.write(m.add(5,7));
document.write(m.add("hi","there!"));
document.write(m.add("May", 13));

কৃতজ্ঞতাঃ এই লেখা তৈরি করতে দরকারি তথ্য নেয়া হয়েছে Wikipedia, internet ও আমার তৈরি করা নানা program থেকে।
মন্তব্য ও যোগাযোগঃ mrh4478@gmail.com

Sunday, May 12, 2024

এড হক পলিমরফিজম

Polymorphism: এটি হচ্ছে একটি common interface যার মাধ্যমে বিভিন্ন ধরণের data type এর programming construct কে প্রতিনিধিত্ব করা যায়। সাধারণত: তিন ধরণের Polymorphism দেখতে পাওয়া যায় ,Christopher Stratchey এর ব্যাখ্যা অনুযায়ি:
•Ad hoc Polymorphism
•Parametric Polymorphism
•Subtyping
শুরু করা যাক Ad hoc Polymorphism দিয়ে:

Ad hoc Polymorphism: এটি এক ধরণের Polymorphic function কে নির্দেশ করে যা প্রয়োগ করা হয় ভিন্ন ধরণের data type argument এর উপর। অর্থাৎ এটি function overloading যা argument হিসেবে একেক সময় ভিন্ন ধরণের data type নিচ্ছে। উদাহরণে contact() function এ argument হিসেবে প্রথমবার string এবং আরেকবার string ও int data type pass করা হয়েছে। contact(string) function টি নাম দেখাচ্ছে আর contact(string,int) function টি নাম ও ফোন নাম্বার দেখাচ্ছে।

উদাহরণ( C++):
#include ‹iostream›
#include ‹string›
class Demo{
public:
std::string s;
int n;
Demo(){}
void contact(std::string name){
s=name;
std::cout«"Name: "«s«" \n";
}
void contact(std::string name,int phone){
s=name;
n=phone;
std::cout«"Name: "«s<<","«" Phone: "«n«" \n";
}
};
int main()
{
Demo a;
a.contact("ash",1230);
return 0;
}

উদাহরণ (JavaScript):
class Demo{
constructor(){}
contact(name,phone,ops){
this.s=name;
this.n=phone;
this.ops=ops;
}
show (){
switch (this.ops)
{
case 1:
return this.s;
case 2:
return this.s+" "+this.n;
default:
return " Sorry ,not defined!";
}
}
}
var s=new Demo();
s.contact("ash",1);
document.write(s.show());

কৃতজ্ঞতাঃ এই লেখা তৈরি করতে দরকারি তথ্য নেয়া হয়েছে Wikipedia, internet ও আমার তৈরি করা নানা program থেকে।
মন্তব্য ও যোগাযোগঃ mrh4478@gmail.com