×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

C++ Empty class - Size, Declaration, Object Creation

Here, we will learn about C++ Empty Class, how to declare an empty class, how to create an empty class's object and what is the size of an empty class/object?

C++ Empty Class

C++ allows creating an Empty class, yes! We can declare an empty class and its object. The declaration of Empty class and its object are same as normal class and object declaration.

What is the size of an Empty class's object?

An Empty class's object will take only one byte in the memory; since class doesn't have any data member it will take minimum memory storage. One byte is the minimum memory amount that could be occupied.

(Reference: Q. 4.25 Test your C++ Skills by Yashavant Kanetkar)

Example of C++ empty class

Let's consider the following program

#include <iostream>
using namespace std;

class Example {
};

int main()
{
    Example objEx;
    cout << "Size of objEx is: " << sizeof(objEx) << endl;

    return 0;
}

Output

Size of objEx is: 1

Explanation

Here Example is an empty class which does not has any data member and member function, and objEx is the object of class Example. See the output "Size of objEx is: 1".

Related Tutorials

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.