Write a c++ program to show the use of constructor.
// Cpp program to illustrate the
// concept of Constructors
#include <iostream>
using namespace std;
class counter{
public:
int count,num;
counter() //constructor with same class name
{
count=0; //initialize count value 0
}
void getdata() //function to input number to increement
{
cout<<"Enter a number to add \n";
cin>>num;
}
void increment()
{
count=count+num;
}
void display()
{
cout<<"incremented numbers id "<<count;
}
};
int main(){
counter c1;
c1.getdata();
c1.increment();
c1.display();
return 1;
}
output:
Comments
Post a Comment