• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

C++ code to C code

Can you tell me how to convert the Base class,Derived class(inheritance property)in C code without ignoring the idea of inheritance property in C?
Also can you give any suggestions on how to handle the Public,Private,Protected functions of a class in C,how should i port them
0
gaatri
Asked:
gaatri
1 Solution
 
WynCommented:
you'd better not convert it.
Regards
Wyn
0
 
LDCCommented:
You should look for the original C++ preprocessor, which translated C++ into C.
0
 
KangaRooCommented:
That is difficult indeed. I have seen C code using structures with a function pointer. This would provide the basics of virtual members.
Originally, I think, C++ was implemented, by AT&T, as a macro (?) front end which produced ordinary C code.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
nietodCommented:
>> look for the original C++ preprocessor

>> I think, C++ was implemented, by AT&T, as
>> a macro (?) front end
Bjarne stroustrup wrote the first C++ compiler called "Cfront" as a source code "pre-processor" that converted the C++ code into C code that could be compiled to produce the application.  He mantained it for many years and the source code was avaialble to the public.  However, he didn't support it in recent years so his copy would not support recent changes, like exception, templates, namespaces etc.  

Anyways C can represent a derived class as a structure that has the bease class has its first member, like

****C++*****

class Base
{
   int i;
};

class Derived : public Base
{
    char ch;
};

******C******
typedef struct
{
    int i;
} Base;

typedef struct
{
   Base b;
   char ch;
} Derived;


Now that allows you to perform the same sort of functionallity of C++'s inheriticance in C.  That does not mean that those C structurees are in any way compatible with a C++ class.

Obviously there is no way to handle private and protected in C.  You have to make sure no to "touch" members when you don't have access to them.  If you use a pre-processor program like "cfront", it woudl not generate code to access an private member when inappropriate.  
0
 
nietodCommented:
Opps, that was meant to be acomment.  Sorry.

Well, its for 5 pts, I guess no one will be too mad at me.  :-)
0
 
ArvindtnCommented:
I don't know whether namespace works in C, but if it could be used in C, then i think we can create a round about way to implement private and protected member datas using Namespaces. somebody please Correct me if iam wrong
0
 
nietodCommented:
C didn't originally support namspaces, although when the C++ standard was created a C standard was also created and they did addopt a few extra features from C++, but I think this was all little things, like use of constants.  So I really doubt that C supports namespaces.  
0
 
sisuCommented:
Hi,
  In pure C compiler U don't get any sort of oop's concepts. If Ur working in c++ environment then only U get these features.

   
0
 
KangaRooCommented:
I don't agree. OOP programming is, as you say, a concept. You can follow this concept even in C. And you can break it in C++. OOP programming is not a language, it is a way to solve programming problems.
0
 
nietodCommented:
I agree with kangaroo.  The proof being the fact that early C++ compilers, like Cfront converted C++ code into C code.  Since the C++ code was OO, the C code was too.  It was just ugly to look at.
0
 
ozoCommented:
ugly to maintain too
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now