0

I Have a C header file which has common structures which are used by various modules.

Im developing my code using C++, few of the newly written modules are in C++.

We have to use the same structures defined in that C header file.
What i am thinking of doing is,

Take the same structure and convert to a class and add few member functions to that class which act upon the structure variables. I would like to know whether this is really possible or not.

Or can i do this ?
Use the same structure in my own header file and then add member functions to the structure.

Please help/

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by vmanes
0

If the assignment is to use the C structures and header file as is, then you should not make changes to it.

But, you could create classes that use those structs as data members, or even derive class(es) from those structures and give them added functionality.

Remember that in C++, a struct will act the same as a class. The only difference between struct and class in C++ (besides the name) is that structs give members public visibility by default, and class members are private by default.

0

So can a derive a class from a structure like this:

struct A
{

int a;
};

class B :: public A
{
public:
int getA() { return a;}
};

int main()
{
B a;
cout<<a.getA();
}

0

Yes.

Two things.
First, please use code tags, it makes the code easier to read.

Second, only one colon in the header for class B, like this:

class B : public A

Edited by happygeek: fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.