473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

[C++] why we have a constructor?

Im a student who is majoring in computer.
in C++, why we have a constructor ?
I think there is some reasons for Bjarne Stroustrup to make constructor in C++

Thank you ^^
Oct 10 '07 #1
6 1247
Firecore
114 100+
Im a student who is majoring in computer.
in C++, why we have a constructor ?
I think there is some reasons for Bjarne Stroustrup to make constructor in C++

Thank you ^^
Mabye because it makes our lives easier so the class is initialised when its declared? I guess thats a good reason.

That is actually a very good question.
Im not being sarcastic.
Oct 10 '07 #2
Meetee
931 Expert Mod 512MB
Im a student who is majoring in computer.
in C++, why we have a constructor ?
I think there is some reasons for Bjarne Stroustrup to make constructor in C++

Thank you ^^
Hi,

Have you search on google regarding this issue? Read http://www.cplusplus.com/doc/tutorial/classes.html for your answer.

Regards
Oct 10 '07 #3
Im a student who is majoring in computer.
in C++, why we have a constructor ?
I think there is some reasons for Bjarne Stroustrup to make constructor in C++

Thank you ^^
Hi darkkal,

In C++ there is a use of constructor.
with the use of constructor u can define the variables value when the program runs. it will call automatically no need to call explicitly.
when u want to use constructor just make the same name as your class. and define the values of the variable.
this is the basic idea abt constructor.
Oct 10 '07 #4
sicarie
4,677 Expert Mod 4TB
Okay, imagine you created this robot. It cooks, it cleans, it plays video games. It's the best robot ever. However, when you created it, you didn't put any logic in it so that you could order it to do things, and when you turned it on, it decided to go become a member of a reggae band in Jamaica.

Without a constructor, your code is off in Jamaica, unreachable. It is no use to you because you can't call it, you can't instantiate it, you can't even tell it exists. When you call a constructor, you finally create a modifiable instance of your class, and that's when you can use it and the methods it creates. Obviously, this is not a perfect analogy, there are exceptions to this, but that is the gist of it.
Oct 10 '07 #5
hi, i'm also doing c++ programming.my idea of a constructor is that it help you to initialisize private members in a class. because if you don't use it the compiler will implicitely do it for you.
thanks.
Oct 10 '07 #6
sicarie
4,677 Expert Mod 4TB
hi, i'm also doing c++ programming.my idea of a constructor is that it help you to initialisize private members in a class. because if you don't use it the compiler will implicitely do it for you.
thanks.
Well, good practice would indicate that you also include get/set methods, but if you know the values on initialization it is a good idea to set those in the default constructor as well.
Oct 10 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
6
by: Nafai | last post by:
Hello. I want to do something like this: class A { // It's virtual protected: float* data; int n; public: A(int a); virtual float* createData(); //...
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
2
by: vakap | last post by:
function show() { var s = '' ; for (var i = 0; i<arguments.length; s += '\n'+arguments) ; typeof(window) != 'undefined' ? window.alert(s) : WScript.Echo(s) ; } function f(){}...
7
by: ad | last post by:
When a class (say class1) inherited form another class (Class0). The constructor in the class0 will be executing before the constructor of class1. Can we over the constructor?
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
4
by: Deep | last post by:
Can I use a class in this manner, where a constructor is of templated: template<typename T> class my_class{ public: int x_; public: template<typename U> my_class(const my_class<U>& other ) :...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.