a small quetion on c++ constructor!!

This is a discussion on a small quetion on c++ constructor!! within the C++ Programming forums, part of the General Programming Boards category; can you give a struct well defined values by using constructors??? I am a newer to come here how does ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    24

    Question a small quetion on c++ constructor!!

    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.........................

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Can you re-post that with an intellegent, well formed question and good punctuation?
    Waiting....
    Woop?

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    24
    I only to kown the difference between structs in C and structs in C++.!
    i am so sorry for it!!

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    24
    i am a primary !
    a newer to c++

  5. #5
    Banned
    Join Date
    Oct 2004
    Posts
    250
    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.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    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.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    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..

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    24
    i come from hongkong so sorry!
    thang you above answers!

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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;
    }

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Quote Originally Posted by pacific
    can you give a struct well defined values by using constructors???
    Yes, in C++ you can.
    Quote Originally Posted by pacific
    how does constructor work?
    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).
    Quote Originally Posted by pacific
    what differenvd betwteen the struct on c and c++??
    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.


    Quote Originally Posted by 7stud
    Code:
     //Constructor is used to initialize the members:
        Car(int l, int w, double h)
        {
            length = l;
            width = w;
            height = h;
        }
    I would use an initialization list for this, even though they are just ints, it is good practice to get into:
    Code:
     //Constructor is used to initialize the members:
        Car(int l, int w, double h) : length(l), width(w), height(h)
        {
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  2. Parameterless Constructor - Struct
    By vb.bajpai in forum C++ Programming
    Replies: 12
    Last Post: 07-10-2007, 11:17 AM
  3. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  4. Replies: 3
    Last Post: 03-25-2006, 11:59 PM
  5. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 03:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21