#include <stdio.h> // for printf()
// int translate(float x, y); // compile error: unknown type name 'y'
int translate(float x, float y, float z);
int translate(float, float, float); // same function prototype
// int translate(int, int); // compile error: overloading not allowed in C
void func(); // indeterminate number of arguments in C
void noargs(void); // no arguments
int main()
{
float x, y, z; // OK
printf("%d\n", translate(0,1,2)); // 3
func();
func(0);
func(1,2);
noargs();
// noargs(0); // compile error: too many arguments
return 0;
}
/*
int translate(float, float, float) // compile error: parameter name omitted
{} // not complaining about no return value
*/
/*
int translate(float x, float y, float z)
{} // garbage return value, no compile or run-time error
*/
int translate(float x, float y, float z)
{
return x+y+z;
}
void func(){}
void noargs(void){}
/*
gcc translate.c -o translate
./translate
3
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
// int translate(float x, y); // compile error: 'y' has not been declared
int translate(float x, float y, float z);
int translate(int, int); // overloading in C++
void func(); // No arguments in C++
void noargs(void); // No arguments
int main()
{
float x, y, z; // OK
cout << translate(1,2) << endl; // 0, call translate(int, int)
cout << translate(1,2,3) << endl; // 1+2, translate(float, float, float)
func();
// func(0); // compile error: too many arguments
noargs();
// noargs(0); // compile error: too many arguments
return 0;
}
int translate(int, int) {return 0;}
int translate(float x, float y, float)
{
// return x+y+z; // compile error: 'z' was not declared in this scope
return x+y;
}
void func(){}
void noargs(void){}
/*
g++ Translate.cpp -o Translate
./Translate
0
3
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h>
// warning: data definition has no type or storage class:
cfunc(); // warning: (arg) type defaults to 'int' in declaration of 'cfunc'
main() // warning: return type defaults to 'int'
{
int val;
printf("Type an integer: ");
scanf("%d", &val);
printf("%c\n", cfunc(val)); // call cfunc() with 1 arg OK: no warning
return 0; // no error or warning here
}
/*
cfunc(char c) {} // compile error: conflicting types for 'cfunc' (char arg)
*/
cfunc(int i, int j) // warning: return type defaults to 'int'
{ // int args OK, j not used: no warning
if(i == 0) {return 'a';} // no error
if(i == 1) {return 'g';} // or warning
if(i == 5) {return 'z';}
return 'c';
}
/*
gcc return.c -o return
./return
Type an integer: -1
c
./return
Type an integer: 0
a
./return
Type an integer: 1
g
./return
Type an integer: 2
c
./return
Type an integer: 5
z
./return
Type an integer: m
c
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
char cfunc(int);
int main()
{
int val;
cout << "Type an integer: ";
cin >> val;
cout << cfunc(val) << endl;
return 0;
}
char cfunc(int i)
{
if(i == 0) {return 'a';}
if(i == 1) {return 'g';}
if(i == 5) {return 'z';}
return 'c';
}
/*
g++ Return.cpp -o Return
./Return
Type an integer: -1
c
./Return
Type an integer: 0
a
./Return
Type an integer: 1
g
./Return
Type an integer: 2
c
./Return
Type an integer: 5
z
./Return
Type an integer: m // val = 0
a
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 3-1. Create a header file (with an extension of '.h' [or '.hpp']). In this file, declare a group of functions by varying the argument lists and return values from among the following: void, char, int, and float. Now create a .cpp file that includes your header file and creates definitions for all of these functions. Each definition should simply print out the function name, argument list, and return type so you know it's been called. Create a second .cpp file that includes your header file and defines int main(), containing calls to all of your functions. Compile and run your program.
void f(void);
char g(char, int);
int h(int, float);
int overloaded(float);
void overloaded(int, int);
char overloaded(char, int, int, float);
#include "functions.hpp"
#include <iostream>
using std::cout;
using std::endl;
void f(void)
{
cout << "void f (void)" << endl;
}
char g(char c, int i)
{
cout << "char g (char " << c << ", int " << i << ")" << endl;
return 0;
}
int h(int i, float f)
{
cout << "int h (int " << i << ", float " << f << ")" << endl;
return 0;
}
int overloaded(float f)
{
cout << "int overloaded (float " << f << ")" << endl;
return 0;
}
void overloaded(int i, int j)
{
cout << "void overloaded (int " << i << ", int " << j << ")" << endl;
}
char overloaded(char c, int i, int j, float f)
{
cout << "char overloaded (char " << c << ", int " << i
<< ", int " << j << ", float " << f << ")" << endl;
return 0;
}
/*
g++ -c functions.cpp // compile without linking, create `functions.o'
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include "functions.hpp"
int main()
{
f();
g('a', 0);
h(0, 1.2);
overloaded(2.1);
overloaded(1, 2);
overloaded('a', 0, 1, 2.1);
return 0;
}
/*
g++ -c functions.cpp // compile without linking, create obj file `functions.o'
g++ -c main.cpp // compile without linking, create object file `main.o'
g++ *.o -o main // link object files and create the executable
./main // run program
void f (void)
char g (char a, int 0)
int h (int 0, float 1.2)
int overloaded (float 2.1)
void overloaded (int 1, int 2)
char overloaded (char a, int 0, int 1, float 2.1)
ar cr libfunctions.a functions.o // create static library
g++ main.o -L. -lfunctions -o main // by default, use shared library (.so)
// if available, otherwise use existing library (.a)
g++ -static main.o -L. -lfunctions -o main // use static library (.a)
./main
void f (void)
char g (char a, int 0)
int h (int 0, float 1.2)
int overloaded (float 2.1)
void overloaded (int 1, int 2)
char overloaded (char a, int 0, int 1, float 2.1)
g++ -c -fPIC functions.cpp // create functions.o to be used for shared library
// -fPIC - Position-independent code flag
g++ -shared -fPIC functions.o -o libfunctions.so // create shared library
g++ main.o -L. -lfunctions -o main // make executable using shared library
// by default (now available), otherwise use existing library (.a)
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:. // add current directory (.)
// to dynamic linker search path
echo $LD_LIBRARY_PATH
:.
g++ main.o -L. -lfunctions -Wl,-rpath,. -o main // no $LD_LIBRARY_PATH
// -Wl,-rpath,. - pass option -rpath,. to the linker
// -rpath,. - add current directory (.) to run-time search path
./main
void f (void)
char g (char a, int 0)
int h (int 0, float 1.2)
int overloaded (float 2.1)
void overloaded (int 1, int 2)
char overloaded (char a, int 0, int 1, float 2.1)
ldd main // print shared libraries used by the program
rm *.o // clean (delete object files)
*/