Sunday, January 8, 2017

Main function in C++ -- or at least what works

/* A C listing with something odd - */
/* using a square() function twice */
#include <stdio.h>
#include <stdlib.h>

int main (void) {
return 0;
}

I was studying OOP with polymorphism, inheritance, and encapsulation. For some reason I picked up a book on C with C++. It's fun comparing to my memory of Daniel Shiffman's OOP and PHP's OOP.

Also, this is what happens if you compile a C++ program as a C program.

gcc square.c -o square-c.o
square.c:25:6: error: conflicting types for ‘square’
void square( int topleftX, int topleftY, int bottomleftX, int bottomleftY)

^ square.c:7:6: note: previous definition of ‘square’ was here
void square( int topleftX, int topleftY, long width)

^ square.c: In function ‘main’:
square.c:54:4: error: too few arguments to function ‘square’
square( pt_x3, pt_y3, side);

^ square.c:25:6: note: declared here
void square( int topleftX, int topleftY, int bottomleftX, int bottomleftY)


^ With polymorphism in C++ I can have multiple declarations of a function and the program figures out which one to use. Whereas with C, I cannot. This example illustrates function or method overloading.
See: https://en.wikipedia.org/wiki/Function_overloading

No comments:

Post a Comment