CoolInterview.com - World's Largest Collection of Interview Questions
Home| Ask Questions| About Us| Feedback| Contact Us|
 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

  • COOLINTERVIEW.COM LANGUAGES C++ DETAILS



    Question :
    Can the structures contain the constructor?
    If yes, then how?

    Contact Author  Contact Author


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

     Posted by: pramila    

    Contact Author  Contact Author
    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 Author  Contact Author
    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 Author  Contact Author
    struct Test
    {
    Test()
    {
    cout<<"Object Created Successfully";
    }
    };
    int main()
    {
    Test ob;
    return 1;
    }


     Posted by: Bhattad Jeetendra    

    Contact Author  Contact Author

    © CoolInterview.com
    Today's Special: Get Free Magazine from SAP now!

    A D V E R T I S E M E N T








    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

       


    Rate the above answer. Help us to know about the answer.
    Category C++ Interview Questions
    Rating (0.0) By 0 users
    Added 5/21/2007 6:44:17 PM
    Views 4565
    Rate it!
    Free Offers 300 Free Magazines for you. No credit card required. No Shipping Charges. Absolutely Free for 1 Year.

    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


    Sponsored Links  

    Google Search

    Google

    CoolInterview.com is a part of Vyom Network.
    Copyright ©2003-2009 Vyom Technosoft Pvt. Ltd., All Rights Reserved. Privacy Policy
    Page URL: http://www.coolinterview.com/interview/10986/Default.asp?cachecommand=bypass


    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial

    0.98