#include<iostream.h>
#include<conio.h>
class cal
{
private:
int a;
public:
cal(int x)
{
a=x;
}
void increament()
{
a=a+1;
}
void display()
{
cout<<"\n Value is"<<a;
}
};
main()
{
clrscr();
cal c1(3);
c1.display();
c1.increament();
c1.increament();
c1.increament();
c1.display();
getch();
}
------------------------------------------------------------------------------------------------------------------------------------
A constructor resembles an instance method, but it differs from a method in that it never has an explicit return-type, it is not inherited (though many languages provide access to the superclass's constructor, for example through the super keyword in Java), and it usually has different rules for scope modifiers. Constructors are often distinguished by having the same name as the declaring class. They have the task of initializing the object's data members and of establishing the invariant of the class, failing if the invariant isn't valid. A properly written constructor will leave the object in a valid state. Immutable objects must be initialized in a constructor.
Programmers can also use the term constructor to denote one of the tags that wraps data in an algebraic data type. This is a different usage than in this article.[dubious – discuss] For more information, see algebraic data type.
No comments:
Post a Comment