hi guys i am newbie in c/c++ staff and friend of mine told me that you can mix c/c++ and i thought it's cool but i want to see how it's work so please help.. if you have any simple example will appreciate ..thanks

A C++ compiler will happily compile the following code.

#include <iostream>
#include "stdio.h"

int main()
{
  std::cout << "Hello ";
  printf("world");
  return 0;
}

C++ is (almost) a superset of C. If it's valid C code, it's valid C++. The only time one has to be careful is linking C++ against code that was compiled with a C compiler, but if you compile it all with a C++ compiler, it's fine.

Also, remember that although most C headers are now "C++ safe", if you want to link a C function of your own to C++ code, you need to declare and/or define it as a "C" function. Example:

extern "C" void myCfunction(int arg); // Declaration

extern "C" void myCfunction(int arg) // Definition - not required if compiled with
// C compiler, but needed if compiled with C++ compiler.
{
   // Do something with arg
}

thank you friend...it really help me a lot .. but it's possible to have two separate classes (one c and other c++ class )then compile to together to produce one class...example will be appreciate...again thank you so much...

C does not have classes, per se. However, it does have structs, which are something like C++ classes, although all members and functions are public by default. So, you can use a C struct as an element of a C++ class (I've done than upon occasion).

So, an example:

extern "C" struct foo
{
    int memberA;
    float memberB;
};

class bar : private struct foo // Structure foo is now a private part of class "bar"
{
private:
    const char* name;
public:
    bar();  // Default constructor
    bar( const bar& ); // Copy constructor
    virtual ~bar(); // Destructor
    bar& operator=(const bar&); // Assignment operator

    int getMemberA() const { return foo::memberA; }
    float getMemberB() const { return foo::memberB; }
};

Member functions in class "bar" can change or read values in struct "foo", but because it is private to class "bar", external entities cannot access or modify its values. Note the getter methods shown will provide access to the data, but not allow them to be changed. This is a useful construct when using C++ to access computer hardware.

It should be noted in my example above that the members of struct foo were not initialized. This should ABSOLUTELY be done in the constructors in order to avoid "bad things" from happening... :-)

C does not have classes, per se. However, it does have structs, which are something like C++ classes, although all members and functions are public by default.

There are no member functions in a C struct, which is a bit of a huge difference.

There are no member functions in a C struct, which is a bit of a huge difference.

True, but you can "wrap" a C struct in a C++ class and give it member functions and such.

Which makes it a C++ struct, which is identical in all but default privacy to a C++ class.

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.