কম্পিউটার

কিভাবে C++ এ একটি কপি কনস্ট্রাক্টর বাস্তবায়ন করবেন?


এখানে আমরা দেখব কিভাবে কপি কনস্ট্রাক্টরগুলি C++ এ প্রয়োগ করা হয়। আলোচনা করার আগে আমাদের জানা উচিত কপি কনস্ট্রাক্টর কি।

কপি কনস্ট্রাক্টর একটি কনস্ট্রাক্টর যা আগে তৈরি করা একই শ্রেণীর একটি বস্তুর সাথে শুরু করে একটি অবজেক্ট তৈরি করে। কপি কনস্ট্রাক্টর −

ব্যবহার করা হয়
  • একই ধরনের অন্য বস্তু থেকে একটি অবজেক্ট শুরু করুন।
  • কোন বস্তুকে একটি ফাংশনে আর্গুমেন্ট হিসেবে পাস করার জন্য কপি করুন।
  • কোন বস্তুকে একটি ফাংশন থেকে ফিরিয়ে আনতে অনুলিপি করুন।

যদি একটি কপি কনস্ট্রাক্টর একটি ক্লাসে সংজ্ঞায়িত না হয়, তাহলে কম্পাইলার নিজেই একটিকে সংজ্ঞায়িত করে। যদি ক্লাসে পয়েন্টার ভেরিয়েবল থাকে এবং কিছু গতিশীল মেমরি বরাদ্দ থাকে, তাহলে একটি কপি কনস্ট্রাক্টর থাকা আবশ্যক। কপি কনস্ট্রাক্টরের সবচেয়ে সাধারণ ফর্মটি এখানে দেখানো হয়েছে −

classname (const classname &obj) {
// body of constructor
}

এখানে, obj একটি বস্তুর একটি রেফারেন্স যা অন্য অবজেক্ট শুরু করতে ব্যবহৃত হচ্ছে।

উদাহরণ

#include <iostream>
using namespace std;
class Line {
   public:
      int getLength( void );
      Line( int len ); // simple constructor
      Line( const Line &obj); // copy constructor
      ~Line(); // destructor
   private:
      int *ptr;
};
// Member functions definitions including constructor
Line::Line(int len) {
   cout << "Normal constructor allocating ptr" << endl;
   // allocate memory for the pointer;
   ptr = new int;
   *ptr = len;
}
Line::Line(const Line &obj) {
   cout << "Copy constructor allocating ptr." << endl;
   ptr = new int;
   *ptr = *obj.ptr; // copy the value
}
Line::~Line(void) {
   cout << "Freeing memory!" << endl;
   delete ptr;
}
int Line::getLength( void ) {
   return *ptr;
}
void display(Line obj) {
   cout << "Length of line : " << obj.getLength() <<endl;
}
// Main function for the program
int main() {
   Line line(10);
   display(line);
   return 0;
}

আউটপুট

Normal constructor allocating ptr
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!

আসুন আমরা একই উদাহরণ দেখি কিন্তু একটি ছোট পরিবর্তনের সাথে একই ধরণের বিদ্যমান বস্তু ব্যবহার করে আরেকটি অবজেক্ট তৈরি করতে −

উদাহরণ

#include <iostream>
using namespace std;
class Line {
   public:
      int getLength( void );
      Line( int len ); // simple constructor
      Line( const Line &obj); // copy constructor
      ~Line(); // destructor
   private:
      int *ptr;
};
// Member functions definitions including constructor
Line::Line(int len) {
   cout << "Normal constructor allocating ptr" << endl;
   // allocate memory for the pointer;
   ptr = new int;
   *ptr = len;
}
Line::Line(const Line &obj) {
   cout << "Copy constructor allocating ptr." << endl;
   ptr = new int;
   *ptr = *obj.ptr; // copy the value
}
Line::~Line(void) {
   cout << "Freeing memory!" << endl;
   delete ptr;
}
int Line::getLength( void ) {
   return *ptr;
}
void display(Line obj) {
   cout << "Length of line : " << obj.getLength() <<endl;
}
// Main function for the program
int main() {
   Line line1(10);
   Line line2 = line1; // This also calls copy constructor
   display(line1);
   display(line2);
   return 0;
}

আউটপুট

Normal constructor allocating ptr
Copy constructor allocating ptr.
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!
Freeing memory!

  1. ফ্লাড ফিল অ্যালগরিদম – সি++ এ পেইন্টে ফিল() কিভাবে প্রয়োগ করা যায়

  2. C++ এ কপি কনস্ট্রাক্টর এবং অ্যাসাইনমেন্ট অপারেটরের মধ্যে পার্থক্য

  3. কিভাবে C++ ব্যবহার করে OpenCV-এ একটি লাইন আঁকবেন?

  4. C++ এ রেখার প্রতিফলন