One Object Created coderinme

One Object Created coderinme

One Object Created coderinme
A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class. When you define a class, you define a blueprint for a data type.
Define a class from which only one object can be created. If more than one object is created the program should terminate

#include<iostream.h>
#include<conio.h>
#include <stdlib.h>
class Once{
static int objectCount;
public:
Once(){
if(objectCount==1)
{
objectCount++;
}
else
{
cout<<"More than one object is created...."<<endl;
exit(0);
}
}
void exists(){
cout<<"An object is created."<<endl;
}
};
int Once::objectCount = 1;
int main()
{
Once obj;
obj.exists();
getch();
return 0;
}

For more programs in C++, visit our C++ Coder Archives

One Object Created coderinme

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

Leave a reply:

Your email address will not be published.