LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

C++ like Program

Hello ,
Does below program have a C++ style??
 
////==================Strart================================////
#include <ansi_c.h>
#include <cvirte.h>
typedef struct A{
                 int  (* add)(int a,int b);
                 int  (* sub)(int a,int b);
                 void (* output)(char *);
                 } STA;
                
int add(int a, int b)                
{
  return a+b;
}
int sub(int a, int b)                
{
  return a-b;
}
void output(char *str)
{
 printf("%s",str);
}
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpszCmdLine, int nCmdShow)
{  
 STA sta;
 if (InitCVIRTE (hInstance, 0, 0) == 0)
  return -1;    /* out of memory */
 
 sta.add =add;
 sta.sub =sub;
 sta.output=output;
 
 printf("2+1 = %d\n",(*sta.add)(2,1));
 (*sta.output)("hello,world!\n"); 
 getchar();
 return 0;
}
////==================End================================////
0 Kudos
Message 1 of 3
(2,990 Views)

Message Edited by Dennis Knutson on 04-21-2007 03:28 PM

0 Kudos
Message 2 of 3
(2,981 Views)
Assuming the code works as intended, and that by C++ you mean writing an OO program in C++, I'd say:

While you have achieved some form of encapsulation in the struct, and while you do have some re-use due to the struct data type, it's not like OO because:

1.  No implementation hiding.  The inner workings of the struct are exposed.  No control over visibility.
2.  No inheritance, so no polymorphism either.
3.  Explicit dereferencing of pointers to get to the "methods" in the struct.

Menchar





0 Kudos
Message 3 of 3
(2,931 Views)