কম্পিউটার টিউটোরিয়াল

C++-এ ভার্চুয়াল কপি কনস্ট্রাক্টর (Virtual Copy Constructor)

গভীরে যাওয়ার আগে সব রিলেটেড টার্ম রিফ্রেশ করি।

একটি কপি কনস্ট্রাক্টর (copy constructor) হলো একটি স্পেশাল কনস্ট্রাক্টর যা পাস করা অবজেক্টের একদম অনুরূপ একটি অবজেক্ট তৈরি করতে ব্যবহৃত হয়।

ভার্চুয়াল ফাংশন (virtual function) হলো একটি মেম্বার ফাংশন যা প্যারেন্ট ক্লাসে ডিক্লেয়ার করা থাকে এবং চাইল্ড ক্লাসে রিডিফাইন (ওভাররাইড) করা হয়।

ভার্চুয়াল কপি কনস্ট্রাক্টর ব্যবহার করে প্রোগ্রামার অবজেক্টের এক্সাক্ট ডেটা টাইপ না জানলেও অবজেক্ট তৈরি করতে পারবে।

C++-এ কপি কনস্ট্রাক্টর অন্য অবজেক্ট থেকে কপি করে অবজেক্ট তৈরি করতে ব্যবহৃত হয়। কিন্তু যদি প্রোগ্রাম রানটাইমে অবজেক্টের টাইপ নির্ধারণ করতে চায় (কম্পাইল টাইমে না, বরং ইউজার ইনপুট বা শর্ত অনুযায়ী রানটাইমে), তবে আমাদের স্পেশাল পাওয়ারযুক্ত কপি কনস্ট্রাক্টর লাগবে। সেজন্য ভার্চুয়াল কপি কনস্ট্রাক্টর ডিক্লেয়ার করা হয় যা রানটাইমে অবজেক্ট ক্লোনিং অফার করে।

উদাহরণ: ধরা যাক একটি ফিগার আছে যার এরিয়া বের করতে হবে প্রোগ্রামে। কিন্তু অবজেক্টের টাইপ রানটাইমে নির্ধারিত হবে - স্কোয়ার, রেক্ট্যাঙ্গেল বা সার্কেল হতে পারে। তাই ভার্চুয়াল কপি কনস্ট্রাক্টর ব্যবহার করব যা ইউজার ইনপুট অনুযায়ী অবজেক্ট কপি করবে।

ভার্চুয়াল কনস্ট্রাক্টর কাজ করার জন্য বেস ক্লাসে দুটি মেথড ডিফাইন করা থাকে:

clone() create()

কপি কনস্ট্রাক্টর ভার্চুয়াল clone মেথড ব্যবহার করে, আর ডিফল্ট কনস্ট্রাক্টর ভার্চুয়াল create মেথড ব্যবহার করে ভার্চুয়াল কনস্ট্রাক্টর তৈরি করে।

C++ প্রোগ্রাম উদাহরণ

#include <iostream>
using namespace std;

class figure{
    public:
    figure() { }
    virtual ~figure() { }
    virtual void ChangeAttributes() = 0;
    static figure *Create(int id);
    virtual figure *Clone() = 0;
};

class square : public figure{
    public:
    square(){
        cout << "square created" << endl;
    }
    square(const square& rhs) { }
    ~square() { }
    void ChangeAttributes(){
        int a;
        cout<<"The side of square";
        cin>>a;
        cout<<"Area of square is "<>r;
        cout<<"the area of circle is "<<((3.14)*r*r);
    }
    figure *Clone(){
        return new circle(*this);
    }
};

class rectangle : public figure{
    public:
    rectangle(){
        cout << "rectangle created" << endl;
    }
    rectangle(const rectangle& rhs) { }
    ~rectangle() { }
    void ChangeAttributes(){
        int a, b;
        cout<<"The dimensions of rectangle ";
        cin>>a>>b;
        cout<<"Area of rectangle is "<> input;
        while( (input != 1) && (input != 2) && (input != 3) ){
            cout << "Enter ID (1, 2 or 3 only): ";
            cin >> input;
        }
        figures = figure::Create(input);
    }
    ~User(){
        if( figures ){
            delete figures;
            figures = 0;
        }
    }
    void Action(){
        figure *newfigure = figures->Clone();
        newfigure->ChangeAttributes();
        delete newfigure;
    }
    private:
    figure *figures;
};

int main(){
    User *user = new User();
    user->Action();
    delete user;
    return 0;
}

আউটপুট

Enter ID (1, 2 or 3): 2
circle created
enter the radius of the circle 3
the area of circle is 28.26
  1. C++-এ ভেক্টর কপি করার উপায় (Ways to copy a vector in C++)

  2. C++-এ কনস্ট্রাক্টরে ভেক্টর পাস করা (Passing a vector to constructor in C++)

  3. C++-এ কপি কনস্ট্রাক্টর বনাম অ্যাসাইনমেন্ট অপারেটর

  4. C++-এ কপি কনস্ট্রাক্টর