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/

Recommended Answers

All 3 Replies

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.

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();
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.