HP aC++/HP C A.06.25 Programmer's Guide
Examples: HP aC++ Calling HP C
The following examples show a C++ program, calling_c.C that calls a C function,
get_name. The C++ program contains a main function.
//************************************************************
// This is a C++ program that illustrates calling a function *
// written in C. It calls the get_name() function, which is *
// in the “get_name.c” module. The object modules generated *
// by compiling the “calling_c.C” module and by compiling *
// the “get_name.c” module must be linked to create an *
// executable file. *
//************************************************************
#include <iostream.h>
#include “string.h”
//************************************************************
// declare the external C module
extern “C” char* get_name();
class account
{
private:
char* name; // owner of the account
protected:
double balance; // amount of money in the account
public:
account(char* c) // constructor
{ name = new char [strlen(c) +1];
strcpy(name,c);
balance = 0; }
void display()
{ cout << name << “ has a balance of “
<< balance << “\n”; }
};
int main()
{
account* my_checking_acct = new account (get_name());
// send a message to my_checking_account to display itself
my_checking_acct->display();
}
The following example shows the module get_name.c. This function is called by the
C++ program.
/****************************************************/
/* This is a C function that is called by main() in */
/* a C++ module, “calling_c.C”. The object */
/* modules generated by compiling this module and */
/* by compiling the “calling_c.C” module must be */
/* linked to create an executable file. */
/****************************************************/
#include <stdio.h>
#include “string.h”
char* get_name()
250 Mixing C++ with Other Languages