can you give a struct well defined values by using constructors???
I am a newer to come here
how does constructor work?
what differenvd betwteen the struct on c and c++??
waiting.........................
can you give a struct well defined values by using constructors???
I am a newer to come here
how does constructor work?
what differenvd betwteen the struct on c and c++??
waiting.........................
Can you re-post that with an intellegent, well formed question and good punctuation?
Waiting....
Woop?
I only to kown the difference between structs in C and structs in C++.!
i am so sorry for it!!
i am a primary !
a newer to c++
prog-bman he probly doesnt speak english as his first language, there's no differnce between structures in C and C++ but because C++ has classes structures are not used as much as they are in C.
Actually, there's little difference between structs and classes in C++, so structs in C++ are about as different as classes are vs. structs in C. A C++ struct is just a class with everything defaulting to public instead of private. So, if you know about classes, then you know what structs can do too. If not, there's lots of places to learn, from books to webpages, etc. Hope that helps pacific.
Except that there are no classes in C, you're right. In C++, classes and structs are identical except for:
a) Their name.
b) Their default access method.
As stated, everyting is public by default in a struct, private by default in a class. Otherwise they're identical.
Quzah.
Hope is the first step on the road to disappointment.
Heh, I guess I wasn't clear enough there. I meant:
classes are vs. (structs in C)
rather than:
(classes are vs. structs) in C
if you know what I mean. Language should have something like () to group things together too! Oh well..
No, I don't have a clue as to what you're trying to say. Is it really that hard to type a full sentence? ......... Let me try. You pick the correct one:
a) How is a class in C++ different from a struct in C++?
b) How is a class in C++ different from a struct in C?
b) How is a struct in C++ different from a struct in C?
Anything else and you'll have to form your own sentences. See, it's really not that hard.
Quzah.
Hope is the first step on the road to disappointment.
i come from hongkong so sorry!
thang you above answers!
how does constructor work?Code:#include <iostream> using namespace std; class Car { public: int length; int width; double height; //Constructor is used to initialize the members: Car(int l, int w, double h) { length = l; width = w; height = h; } }; int main() { Car mycar(30, 10, 5.5); cout<<"My car's height is: "<<mycar.height<<" feet tall"<<endl; cout<<"My car's length is: "<<mycar.length<<" feet long"<<endl; return 0; }
Yes, in C++ you can.Originally Posted by pacific
In C++, when an instance of a class (or struct) is created, the constructor is called to initialize that object. The constructor is just a function like any other function, except that it never returns any value and you are allowed to initalize member variables with an initialization list (see my change to 7stud's code).Originally Posted by pacific
A struct in C is just a way to combine different variables into a single package. You cannot add member functions to do work with the data the struct holds. In C++, a struct is basically a class (with the previously mentioned small differences). That means that you can make functions that are part of the struct/class and provide different types of access to the data, like private, protected, and public. These things cannot be done in C.Originally Posted by pacific
I would use an initialization list for this, even though they are just ints, it is good practice to get into:Originally Posted by 7studCode://Constructor is used to initialize the members: Car(int l, int w, double h) : length(l), width(w), height(h) { }