Microsoft Interview Question
Software Engineer / Developersjust use the key word struct instead of class.
struct and class are exactly similar (they both can have constructors). But struct's members are public by default and class members are private by default.
so declare a struct and have its constructor in its private region.
Can somebody clarify this question? In C, struct is public, there are no access specifiers in C because data abstraction is an OO concept. So does this imply that we need to mimic OO behavior using struct in C?
@hyan, what did the interviewer say?
@hemant - can you explian your solution?
This is not confusing. C struct does not allow c++ class features. However, C allows function pointer. Therefore, the point is how to use function pointer to implement c++ constructor concept.
As you can see from the responses more than one person found the question confusing, including myself. I believe it is primarily because of the part of your question that says, ‘using the struct in c?’. This is confusing because c++ has the equivalent of the struct in c. So does the question mean, "How do you do this in c++?" or “How do you do this in c?"
Sounds perfect Hyan. for more of C++ flavor into this, we could also add this pointer into it :).
polymorphic C++ classes includes vptr and vtable that you have already done nicely.
If we add this pointer to all member functions that would add more of C++ flavor
int parent_doAdd(struct PARENT *this)
{
printf("parent add\n");
}
int child_doSub(struct CHILD *this)
{
printf("child sub\n");
}
- Hemant Jain February 14, 2009