0
#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?

4
Contributors
3
Replies
18
Views
4 Years
Discussion Span
Last Post by Lucaci Andrew
0

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){ }
};
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.

0

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;
    }
};
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.