Do we have some provision in C similar to inheritance in C++???Thank you in advance.
Inheritance in C++
Collapse
X
-
Tags: None
-
C is not OOP language in general(but with little more effort you can almost use it as if it is),but you should be able to inherit from the struct,because struct is nothing but a class where all members are public.Originally posted by shaheda begumDo we have some provision in C similar to inheritance in C++???Thank you in advance.
Savage -
If I am not mistaken what he means is that you are allowed to do the following
[code=c]
struct a {
int a, b;
};
struct b {
struct a inheritLike;
char b;
};
[/code]
now later in your program or function you could write
[code=c]
struct b foo_struct;
// more stuff
foo_struct.inhe ritLike.a = 0;
[/code]
Hope this helped!Comment
-
No need for that,he can do:Originally posted by Andr3wIf I am not mistaken what he means is that you are allowed to do the following
[code=c]
struct a {
int a, b;
};
struct b {
struct a inheritLike;
char b;
};
[/code]
now later in your program or function you could write
[code=c]
struct b foo_struct;
// more stuff
foo_struct.inhe ritLike.a = 0;
[/code]
Hope this helped!
[CODE="c"]
struct a {
int a, b;
};
struct b:a
{
int c;
};[/CODE]
Now struct b has both a and b plus its own c,and can access them as if they were part of b and not of a.
[CODE="c"]
b bInst;
bInst.a=bInst.b =bInst.c;[/CODE]
SavageComment
-
If I try to compile the following just fails...
[code=c]
struct a {
int a, b;
};
struct b:a
{
int c;
};
int main ()
{
return 0;
}
[/code]
I put what you gave as code to a simple just main program but it fails to compile. Can you help? But one thing I don't understand is how can I use " : " in order to inherit because ansi C it doesn't allow me to use that. Also for the above post I thought you meant with the term "inheritanc e in C" what I wrote before but I was wrong, sorry :p.Comment
-
I tried TC,Borland C++ Builder 6(created c console app),and VC 2005 Pro.On all 3 this compiles.Can you tell me what you got as error?Originally posted by Andr3wIf I try to compile the following just fails...
[code=c]
struct a {
int a, b;
};
struct b:a
{
int c;
};
int main()
{
b bInst;
bInst.a=bInst.b =bInst.c;
return 0;
}
[/code]
I put what you gave as code inside a main function but it fails to compile. Can you help? But one thing I don't understand is how can I use " : " in order to inherit because ansi C it doesn't allow me to use that. Also for the above post I thought you meant with the term "inheritanc e in C" what I wrote before but I was wrong, sorry :p.
SavageComment
-
Having one struct extend another is just a variation on one class extending another. You can't do that it C -- at least not ANSi C. If a compiler allows you, I bet it is regarding your code as C++.Originally posted by SavageI tried TC,Borland C++ Builder 6(created c console app),and VC 2005 Pro.On all 3 this compiles.Can you tell me what you got as error?
In any case, for inheritance to really leverage, you need polymorphism. C doesn't have this -- it's not an object-oriented language like C++, Java, etc... Why pretend it is?
And maybe the real point, is why are you asking? What are you trying to do? what is your goal?Comment
-
It is possible to write object oriented programs in plain C. This is often
uglier, and more dangerous than doing it in an object oriented langauge but
you can do it.Hope below link will help you indeed.
http://aspspider.info/magicalspell4u/?Quest=C-Oops
-Thanks
52Comment
-
Can't you just use .cpp files instead of .c files ??? That would compile your code as C++ and you could use inheritance.Originally posted by Andr3wWell the compiler I used was Visual Studio 08 Team Edition, creating a Win32 Console Project, still doesn't compile
Otherwise, you use an embedded struct member as has been suggested.Comment
-
-
It's also possible to write object oriented programs in COBOL or assembler. But why not use a language that supports what you are trying to do? You still haven't described your problem adequately. What is the context, what are you trying to do and why?Originally posted by alijannaty52It is possible to write object oriented programs in plain C. This is often
uglier, and more dangerous than doing it in an object oriented langauge but
you can do it.Hope below link will help you indeed.
http://aspspider.info/magicalspell4u/?Quest=C-Oops
-Thanks
52
I still think the answer is to use C++ or Java. Now what was the question?Comment
Comment