CoolInterview.com - World's Largest Collection of Interview Questions
 Interview Questions  
 Our Services  

Get 9,000 Interview Questions & Answers in an eBook.


  • 9500+ Pages
  • 9000 Question & Answers
  • All Tech. Categories
  • 14 MB Content

    Get it now !!


    Send your Resume to 6000 Companies


  • INTERVIEW QUESTIONS LANGUAGES C++ DETAILS
    Question :
    Can the structures contain the constructor?
    If yes, then how?

    Contact   Contact
    Category C++ Interview Questions
    Rating (0.0) By 0 users
    Added on 5/21/2007 6:44:17 PM
    Views 4689
    Rate it!
    Answers:

    structures do not contain functions and constructor is a special function.hence,structures can't contain constructors



     Posted by: pramila    

    Contact pramila  Contact pramila

    answer is completely wrong.

    see structure behaves diffrently depending on c or c++ language

    1>if ur in c++ then u can write function as a part of structure .
    2> but if ur in c programming then u cannot write functions as a part of structure.

    see following code for c++( keep file name with cpp (filaname.cpp)format.

    then try this code
    #include <stdio.h>
    #include <conio.h>
    #include <iostream.h>
    struct A
    {
    public:
    int a;

    public:
    A set_A(A a);

    };

    A set_A(A a1)
    {
    a1.a=10;
    return a1;
    }


    int main()
    {
    A var,ret;
    var.a=0;
    ret=set_A(var);
    cout<<"
    Return value is:-"<<ret.a;
    return 0;
    }

    repeate same thing by just renaming that file as filename.c & try it out u can easily find difference.



     Posted by: Amit saraf    

    Contact Amit saraf  Contact Amit saraf

    There is two basic difference between struct in C and C++:
    1> Structures in C++ are similar to Classes in C++ only that by default the access specifier in structure is public whereas in Classes it is private.

    2> While declaring a instance of a struct you can just use the name of the structure, and you don't need to qualify it with the keyword struct.

    STRUCTURES IN C++

    File:- abc.h

    #ifndef ABC_H
    #define ABC_H


    struct abc
    {
    abc();
    };

    #endif

    File:- abc.cpp

    #include "abc.h"
    #include <iostream.h>

    abc::abc()
    {
    cout<<"Structure Constructor"<<endl;
    }

    File:- MainFile.cpp

    #include<iostream.h>
    #include"abc.h"


    int main()
    {
    abc *a = new abc();
    return 0;
    }



     Posted by: Sandipan Karmakar    

    Contact Sandipan Karmakar  Contact Sandipan Karmakar

    struct Test
    {
    Test()
    {
    cout<<"Object Created Successfully";
    }
    };
    int main()
    {
    Test ob;
    return 1;
    }



     Posted by: Bhattad Jeetendra    

    Contact Bhattad Jeetendra  Contact Bhattad Jeetendra


    If you have the better answer, then send it to us. We will display your answer after the approval.
    Name :*
    Email Id :*
    Answer :*
    Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
    Process Verification  Enter the above shown code:*
    Inform me about updated answers to this question

       
    Related Questions
    View Answer
    What is difference between copy constructor and constructor?

    View Answer
    What is importance of const. pointer in copy constructor?

    View Answer
    Why can't we overload the sizeof, :?, :: ., .* operators in c++?

    View Answer
    What happens to the member pointers when an exception occurs in constructor, while allocating memory?How can we over come this ?

    View Answer
    Is there any way to write a class such that no class can be inherited from it. Please include code

    View Answer
    What is virtual constructors/destructors?


    View Answer
    What do you mean by pure virtual functions?


    View Answer
    What is namespace?


    View Answer
    What is RTTI?


    View Answer
    What is a template?


    View Answer

    Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

    Notify me when better answer is posted!
    Email:

    View ALL C++ Interview Questions

    User Options
    Sponsored Links


    Copyright ©2003-2009 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions
    Page URL: http://www.coolinterview.com/interview/10986/default.asp?cachecommand=bypass


    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Dedicated Servers | Joke of the Day

    0.53