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

C++-এ বক্স ক্লাস তৈরি করে ভলিউম ক্যালকুলেশন ও কম্পারিজন


একটি Box ক্লাস তৈরি করতে হবে যাতে:

  • l, b, h প্রাইভেট অ্যাট্রিবিউট (দৈর্ঘ্য, প্রস্থ, উচ্চতা)
  • ডিফল্ট কনস্ট্রাক্টর (০ দিয়ে আরম্ভ) এবং প্যারামিটারাইজড কনস্ট্রাক্টর
  • প্রতিটি অ্যাট্রিবিউটের জন্য গেটার মেথড
  • CalculateVolume() ফাংশন ভলিউম রিটার্ন করবে
  • less-than অপারেটর (<) ওভারলোড করুন বক্স কম্পেয়ার করতে
  • স্ট্যাটিক কাউন্ট ভেরিয়েবল রাখুন বক্সের সংখ্যা ট্র্যাক করতে

তিনটি বক্স (0,0,0), (5,8,3), (6,3,8) নিয়ে টেস্ট করলে আউটপুটে প্রতিটি বক্সের ডেটা, ছোট বক্সের ভলিউম এবং মোট বক্সের সংখ্যা দেখাবে।

সমাধানের ধাপসমূহ

  • CalculateVolume(): return l*b*h
  • operator<: প্রথমে l مقایনা করুন, তারপর b, তারপর h
  • স্ট্যাটিক count ভেরিয়েবল কনস্ট্রাক্টরে ইনক্রিমেন্ট করুন

উদাহরণ

নিচের ইমপ্লিমেন্টেশনটি দেখুন:

#include <iostream> using namespace std; class Box {     int l, b, h; public:     static int count;     Box() : l(0), b(0), h(0) { count++; }     Box(int length, int breadth, int height) : l(length), b(breadth), h(height) { count++; }     int getLength() const {return l;}     int getBreadth() const {return b;}     int getHeight() const {return h;}     long long CalculateVolume() const {         return 1LL * l * b * h;     }     bool operator<(const Box& another) const {         if (l != another.l) return l < another.l;         if (b != another.b) return b < another.b;         return h < another.h;     } }; int Box::count = 0; int main(){     Box b1;     Box b2(5,8,3);     Box b3(6,3,8);     printf("Box 1: (length = %d, breadth = %d, width = %d)\n",b1.getLength(), b1.getBreadth(), b1.getHeight());     printf("Box 2: (length = %d, breadth = %d, width = %d)\n",b2.getLength(), b2.getBreadth(), b2.getHeight());     printf("Box 3: (length = %d, breadth = %d, width = %d)\n",b3.getLength(), b3.getBreadth(), b3.getHeight());     if(b3 < b2){         cout << "Box 3 is smaller, its volume: " << b3.CalculateVolume() << endl;     }else{         cout << "Box 3 is smaller, its volume: " << b2.CalculateVolume() << endl;     }     cout << "There are total " << Box::count << " box(es)"; } 

ইনপুট

b1; b2(5,8,3); b3(6,3,8);

আউটপুট

Box 1: (length = 0, breadth = 0, width = 0) Box 2: (length = 5, breadth = 8, width = 3) Box 3: (length = 6, breadth = 3, width = 8) Box 3 is smaller, its volume: 120 There are total 3 box(es)

  1. সি++ STL ব্যবহার করে অ্যারে প্যালিনড্রোম কিনা তা যাচাই করার প্রোগ্রাম

  2. সি++ প্রোগ্রাম: sin(x) এবং cos(x) এর মান গণনা করা

  3. C++-এ ডায়াগোনাল এবং স্ক্যালার ম্যাট্রিক্স যাচাই করার প্রোগ্রাম

  4. C++-এ কিউবের ভলিউম এবং সারফেস এরিয়া বের করার প্রোগ্রাম