Do we have some provision in C similar to inheritance in C++???Thank you in advance.
13 1976
Do we have some provision in C similar to inheritance in C++???Thank you in advance.
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.
Savage
If I am not mistaken what he means is that you are allowed to do the following -
struct a {
-
int a, b;
-
};
-
-
struct b {
-
struct a inheritLike;
-
char b;
-
};
-
now later in your program or function you could write -
struct b foo_struct;
-
-
// more stuff
-
-
foo_struct.inheritLike.a = 0;
-
Hope this helped!
If I am not mistaken what he means is that you are allowed to do the following -
struct a {
-
int a, b;
-
};
-
-
struct b {
-
struct a inheritLike;
-
char b;
-
};
-
now later in your program or function you could write -
struct b foo_struct;
-
-
// more stuff
-
-
foo_struct.inheritLike.a = 0;
-
Hope this helped!
No need for that,he can do: -
struct a {
-
int a, b;
-
};
-
-
struct b:a
-
{
-
int c;
-
};
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. -
b bInst;
-
bInst.a=bInst.b=bInst.c;
Savage
If I try to compile the following just fails... -
struct a {
-
int a, b;
-
};
-
-
struct b:a
-
{
-
int c;
-
};
-
-
int main ()
-
{
-
return 0;
-
}
-
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 "inheritance in C" what I wrote before but I was wrong, sorry :p.
If I try to compile the following just fails... -
struct a {
-
int a, b;
-
};
-
-
struct b:a
-
{
-
int c;
-
};
-
-
-
int main()
-
{
-
-
b bInst;
-
bInst.a=bInst.b=bInst.c;
-
-
-
return 0;
-
}
-
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 "inheritance in C" what I wrote before but I was wrong, sorry :p.
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?
Savage
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?
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++.
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?
Well the compiler I used was Visual Studio 08 Team Edition, creating a Win32 Console Project, still doesn't compile
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
52
As for reading stuff you can also find this book (it's free):
http://www.cs.rit.edu/~ats/books/ooc.pdf
Well the compiler I used was Visual Studio 08 Team Edition, creating a Win32 Console Project, still doesn't compile
Can't you just use .cpp files instead of .c files ??? That would compile your code as C++ and you could use inheritance.
Otherwise, you use an embedded struct member as has been suggested.
well, I suppose I could but then it would be a cpp program. Wouldn't it? The topic was if we could use inheritance in C in the first place :P
There is no inheritance in C.
Is there some reason you can't use C++??
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
52
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?
I still think the answer is to use C++ or Java. Now what was the question?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: AIM |
last post by:
Error in msvc in building inheritance.obj to build hello.pyd
Hello,
I am trying to build the boost 1.31.0 sample extension hello.cpp.
I can not compile the file inheritance.cpp because the two...
|
by: Graham Banks |
last post by:
Does using multiple inheritance introduce any more performance overhead than
single inheritance?
|
by: JKop |
last post by:
I'm starting to think that whenever you derive one class from another, that
you should use virtual inheritance *all* the time, unless you have an
explicit reason not to. I'm even thinking that...
|
by: Morgan Cheng |
last post by:
It seems no pattern defined by GoF takes advantage of multiple
inheritance. I am wondering if there is a situation where multiple
inheritance is a necessary solution.
When coding in C++, should...
|
by: davidrubin |
last post by:
Structural inheritance (inheriting implementation) is equivalent to
composition in that a particular method must either call 'Base::foo' or
invoke 'base.foo'. Apparantly, The Literature tells us to...
|
by: Steve Jorgensen |
last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling
a particular case of implementation inheritance that would be easy and obvious
in a fully OOP language, but is not at...
|
by: Matthew Louden |
last post by:
I want to know why C# doesnt support multiple inheritance? But why we can
inherit multiple interfaces instead? I know this is the rule, but I dont
understand why. Can anyone give me some concrete...
|
by: Ben Blank |
last post by:
I'm writing a family of classes which all inherit most of their methods and
code (including constructors) from a single base class. When attempting to
instance one of the derived classes using...
|
by: Shawnk |
last post by:
Some Sr. colleges and I have had an on going discussion relative to when and
if
C# will ever support 'true' multiple inheritance.
Relevant to this, I wanted to query the C# community (the...
|
by: Bart Simpson |
last post by:
I remember reading on parashift recently, that "Composition is for code
reuse, inheritance is for flexibility" see
(http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4)
This confused...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |