I am doing C++ from 2 years or so , I say why we need constructors in class and not in structures , and why we cant do constructor overloading in structure...
Asked
Active
Viewed 370 times
-3
-
9The moral of the story is you can't learn `C++` by listening to gossip. You need to get a good book and work methodically through it a few hours a day: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Galik Feb 07 '16 at 20:09
2 Answers
9
why we need constructors in class
We don't.
// Perfectly valid
class Foo
{
public:
int x
};
why we cant do constructor overloading in structure...
We can.
// Look, ma!
struct Bar
{
Bar operator+(const Bar& other);
};
I don't know where you heard these claims but it certainly wasn't from trying it out.
A user-defined type declared with the keyword struct
is a class.

Community
- 1
- 1

Lightness Races in Orbit
- 378,754
- 76
- 643
- 1,055
-
firstly...thank you for replying so early... case one for that class thing... there is a default constructor which would be invoked implicitly which we can also write as `Foo(){x=0;//or perhaps anything}` ...secondly according to my knowledge we cant do this thing in struct... – Shreyan Mehta Feb 07 '16 at 20:14
-
A struct is a class that has default of public visibility, so saying that "a struct is a class" without qualification is plain false. The code `struct {` is *equivalent* to `class { public:`. – kfsone Feb 07 '16 at 20:15
-
2@kfsone, there is a difference between "syntax" and "semantics" of a programming language. "class" and "struct" behave semantically the same, but you need a different syntax (keyword "public" or "private", resp.) to get the same visibility. – Kijewski Feb 07 '16 at 20:18
-
@Kay that's why they are equivalent not the same. "A struct *is* a class with default public visibility" would be correct, but it's not true to say they are the same. Most compilers have a warning when you pre-declare something as a struct but later define it as a class or vice-versa. They are *not* the same. – kfsone Feb 07 '16 at 20:21
-
2@kfsone, did you read PreferenceBean's answer, the linked one? C++ does not know structs. It has two syntaxes to define classes. – Kijewski Feb 07 '16 at 20:23
-
2@ShreyanMehta: _"secondly according to my knowledge we cant do this thing in struct..."_ Your knowledge is incorrect. Take a moment to try it out. You'll see! (Disclaimer: I can't vouch for what early-1990s Turbo C++ on a DOS emulator will do. We are talking about ISO C++ here.) – Lightness Races in Orbit Feb 07 '16 at 20:31
-
1@kfsone: I didn't say they were the same. I said a "struct" (whatever that is) is actually a class, which is completely true. However, that is not the same as claiming that a program containing the token `struct` is the same as a program containing the token `class` in its place. This is sometimes true, but not always. The key point, however, is that _both_ tokens are used in the declaration and definitions of _classes_. As well as the whole traits thing, the validity of the following makes this crystal clear: `struct Foo; class Foo {};` – Lightness Races in Orbit Feb 07 '16 at 20:31
-
@PreferenceBean With clang -Wall that produces `test.cpp:3:1: warning: 'Foo' defined as a class here but previously declared as a struct [-Wmismatched-tags]`. You're arguing the back-end half of the situation while omitting the important front-end half: the input `struct Foo{` and `class Foo{` are not equivalent. Once you get past the visibility, they are identical. My point is you're throwing out the front-end half of the story in the telling of the back-end. – kfsone Feb 07 '16 at 20:35
2
The only difference between struct
and class
in C++ is visibility; a struct defaults to public
while a class defaults to private
.
Once you get past that initial visibility, however, they are indistinguishable. A struct is a class with default public visibility.
These two pieces of code have exactly the same effect.
struct MyClass {
MyClass(int i) : m_i(i) {}
int getI() const { return m_i; }
void setI(int i) { m_i = i; }
private:
int m_i;
};
// is exactly the same as
class MyClass {
public:
MyClass(int i) : m_i(i) {}
int getI() const { return m_i; }
void setI(int i) { m_i = i; }
private:
int m_i;
};
or put another way
class MyClass {
int m_i;
public:
MyClass(int i) : m_i(i) {}
int getI() const { return m_i; }
};
struct MyClass {
private:
int m_i;
public:
MyClass(int i) : m_i(i) {}
int getI() const { return m_i; }
};

kfsone
- 23,617
- 2
- 42
- 74
-
2Please stop spreading [this misleading wording](http://stackoverflow.com/a/34108140/560648). By making "a struct" a thing distinct from "a class", you only propagate the sort of misunderstanding that has led to this very question. – Lightness Races in Orbit Feb 07 '16 at 20:09