খালি ক্লাসের অবজেক্টের সাইজ বের করার একটি উদাহরণ নিচে দেওয়া হল।
উদাহরণ
#include <bits/stdc++.h>
using namespace std;
class p1 {
public:
void first() {
cout << "\nThe parent class p1 function is called.";
}
};
class p2 {
};
int main() {
cout << "The size of non-empty class p1 = " << sizeof(p1);
cout << "\nThe size of empty class p2 = " << sizeof(p2);
p2 p;
cout << "\nThe size of object of empty class p2 = " << sizeof(p);
p1 o;
cout << "\nThe size of object of non-empty class p1 = " << sizeof(o);
return 0;
}
আউটপুট
The size of non-empty class p1 = 1 The size of empty class p2 = 1 The size of object of empty class p2 = 1 The size of object of non-empty class p1 = 1
উপরের প্রোগ্রামে, একটি খালি ক্লাস p2 তৈরি করা হয়েছে।
class p2 { };
ক্লাস এবং অবজেক্টের সাইজ নিচে প্রিন্ট করা হয়েছে -
cout << "The size of non-empty class p1 : " << sizeof(p1); cout << "\nThe size of empty class p2 : " << sizeof(p2); p2 p; cout << "\nThe size of object of empty class : " << sizeof(p); p1 o; cout << "\nThe size of object of non-empty class p1 : " << sizeof(o);