473,394 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Inheritance in C++

Do we have some provision in C similar to inheritance in C++???Thank you in advance.
Dec 31 '07 #1
13 1976
Savage
1,764 Expert 1GB
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
Dec 31 '07 #2
Andr3w
42
If I am not mistaken what he means is that you are allowed to do the following

Expand|Select|Wrap|Line Numbers
  1. struct a {
  2.   int a, b;
  3. };
  4.  
  5. struct b {
  6.  struct a inheritLike;
  7.  char b;
  8. };
  9.  
now later in your program or function you could write

Expand|Select|Wrap|Line Numbers
  1. struct b foo_struct;
  2.  
  3.  // more stuff
  4.  
  5.  foo_struct.inheritLike.a = 0;
  6.  
Hope this helped!
Dec 31 '07 #3
Savage
1,764 Expert 1GB
If I am not mistaken what he means is that you are allowed to do the following

Expand|Select|Wrap|Line Numbers
  1. struct a {
  2.   int a, b;
  3. };
  4.  
  5. struct b {
  6.  struct a inheritLike;
  7.  char b;
  8. };
  9.  
now later in your program or function you could write

Expand|Select|Wrap|Line Numbers
  1. struct b foo_struct;
  2.  
  3.  // more stuff
  4.  
  5.  foo_struct.inheritLike.a = 0;
  6.  
Hope this helped!
No need for that,he can do:
Expand|Select|Wrap|Line Numbers
  1. struct a {
  2.   int a, b;
  3. };
  4.  
  5. struct b:a
  6. {
  7.     int c;
  8. };
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.
Expand|Select|Wrap|Line Numbers
  1. b bInst;
  2. bInst.a=bInst.b=bInst.c;
Savage
Dec 31 '07 #4
Andr3w
42
If I try to compile the following just fails...

Expand|Select|Wrap|Line Numbers
  1. struct a {
  2.     int a, b;
  3. };
  4.  
  5. struct b:a
  6. {
  7.     int c;
  8. };
  9.  
  10. int main ()
  11. {
  12.    return 0;
  13. }
  14.  
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.
Dec 31 '07 #5
Savage
1,764 Expert 1GB
If I try to compile the following just fails...

Expand|Select|Wrap|Line Numbers
  1. struct a {
  2.     int a, b;
  3. };
  4.  
  5. struct b:a
  6. {
  7.     int c;
  8. };
  9.  
  10.  
  11. int main()
  12. {
  13.  
  14.     b bInst;
  15.     bInst.a=bInst.b=bInst.c;
  16.  
  17.  
  18.     return 0;
  19. }
  20.  
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
Dec 31 '07 #6
BigDaddyLH
1,216 Expert 1GB
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?
Jan 1 '08 #7
Andr3w
42
Well the compiler I used was Visual Studio 08 Team Edition, creating a Win32 Console Project, still doesn't compile
Jan 1 '08 #8
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
Jan 1 '08 #9
Andr3w
42
As for reading stuff you can also find this book (it's free):
http://www.cs.rit.edu/~ats/books/ooc.pdf
Jan 1 '08 #10
weaknessforcats
9,208 Expert Mod 8TB
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.
Jan 1 '08 #11
Andr3w
42
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
Jan 1 '08 #12
weaknessforcats
9,208 Expert Mod 8TB
There is no inheritance in C.

Is there some reason you can't use C++??
Jan 1 '08 #13
BigDaddyLH
1,216 Expert 1GB
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?
Jan 2 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
4
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...
5
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...
10
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...
14
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...
22
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...
45
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...
60
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...
6
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...
0
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...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.