In C language, we can both declare and define a function inside the structure itself. here we have to mention the access specifier (public by default) before the declaration of the function takes place. the prototype is as follows:
struct structurename
{
public:
void function_name();/*this is only declaration*/
void function_name2(){
/*definition part also can be written here*/
}
}
Can we define functions in structures in c language? give me a detail answer.?
no you cannot define function in structures
Reply:In both C and C++ you cannot define a functions in them...Although structures are scaled-back versions of classes they are not elements that represent a defining object. They are a bit of a relic (in terms of C++) which only exist for backwards compatibility for C. use them sparingly (or ideally, not at all)...I recommend you use Classes exclusively (assuming you are using C++). If you are using C then you will have no choice but to put variables only in structures and have the related functions outside it.
I strongly recommend you use C++ and skip using struct's all together and use classes. Struct's serve no value in using them if you upgrade to C++. It's your choice though.
Have fun!
Reply:NO YOU CAN"T
cheap flowers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment