#include<iostream.h>
class counter
{
private:
int count;
public:
counter():count(0)
{
count 1 
}
};
int main()
{
counter c1;
return 0;
}

whats wrong in this program i am not able to know?

Recommended Answers

All 3 Replies

What are you trying to achieve in line 9 with count 1

For a default constructor that initializes count to zero:

class counter {
    int count;
public:
    counter() : count(0){ }
};

Hi, Abhinisha..

u just want to demonstrate a progarm with default constructor,right?

U could do that in a much simpler way the altering the code u have..

#include<iostream.h>
class counter
{
private:
int count;
public:
counter():count(0)
{
cout<<"Default constructor\n";
cout<<"count="<<count;
}
};
int main()
{
counter c1;
return 0;
}

Once the object c1 is created, the constructor counter wil be called which prints Default constructor followed by the value of counter as initialised by u i the next line..

Feel free to ask if u find any problem still.

Still, you could've formatted the code a bit. nullptr's example points out very well the use of a default constructor. For multiple elements to be initialized, you could go the plain old fashion way of:

class Foo{
    int a, b, c, d;
    float e, f, g, h;
public:
    Foo() {
        a = b = c = d = 0;
        e = f = g = h = 0;
    }
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.