- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the size of an object of an empty class in C++?
The following is an example to find the size of object of an empty class.
Example
#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; }
Output
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
In the above program, an empty class p2 is created.
class p2 { };
The size of classes and objects is printed as follows −
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);
- Related Articles
- Why is the size of an empty class not zero in C++?
- What is the difference between a class and an object in C#?
- Creating an empty case-sensitive HybridDictionary Class in C#
- What is the Capacity property of an ArrayList class in C#?
- Getting size in memory of an object in PHP?
- What is the scope of an internal variable of a class in C#?
- Get super class of an object in Java
- Can we create an object of an abstract class in Java?
- How to check if an object is an instance of a Class in JavaScript?
- What is an empty set?
- What is an array class in C#?
- Does JVM creates an object of the Main class?
- How to create an empty class in Python?
- What is an object pool in C#?
- What is the difference between an interface and an abstract class in C#?

Advertisements