Computer Applications
Answer
The benefits of a private class (i.e., a class with a private constructor) are:
- Controlled object creation — By making the constructor private, we can control the number of objects of a class that can be created. We can also enforce certain rules or constraints on how the object is created and used.
- Better memory management — By limiting the number of objects of a class that can be created, we can reduce memory usage and improve performance.
Related Questions
Write two characteristics of a constructor.
Write a class specifier (along with its constructor) that creates a class student having two private data members : rollno and grade and two public functions init( ) and display( ).
(Do not write full definitions of member functions except for constructor).Here is a skeleton definition of a class :
class Sample { int i; char c; float f; : //public members : }Implement the constructor.
Define a constructor function for a Date class that initializes the Date objects with given initial values. In case initial values are not provided, it should initialize the object with default values.