Skip to content

Instantly share code, notes, and snippets.

@peta909
Last active February 20, 2019 02:19
Show Gist options
  • Save peta909/d03ff88673a2c30c2c3c7048a5f1bd17 to your computer and use it in GitHub Desktop.
Save peta909/d03ff88673a2c30c2c3c7048a5f1bd17 to your computer and use it in GitHub Desktop.
C++ explaination for use of inline and const keywords; and recursive functions
#include <iostream>
#include <string>
using namespace std;
enum Animals { Bear, Cat, Chicken };
//enum Birds { Eagle, Duck, Chicken }; // error! Chicken has already been declared!
enum class Fruits { Apple, Pear, Orange };
enum class Colours { Blue, White, Orange }; // no problem!
inline int test(const int i)//inline key word 'REQUEST' compiler to place the function code inline saving procedural calls and rets.
{
//i = 6; //const parameter so cant change it
int a = 5;
cout << i << endl;
return a * a;
}
float recursive(float j)
{
if (j > 1000)
return 77777;
return recursive(j * j);
}
float recursive(float j, float k)//function overloading
{
return j + k;
}
int main()
{
const int i = 34;
int j = 23;
//i = 34; //error as const i cannot be changed
int arr1[i];
//int arr2[j];//array size must be fixed
int* arr3 = new int[j];//variable size array using keyword 'new'
const int* a = &i;//const pointer that point to addr of const variable
//int const* b = &j;//error as const point that point to addr of non const j
int* const c = new int(4);//address in *c is constant
//c= new int(4)//error as c is a const point that point to the previous variable int
//void my_Func() const; //can be added to function declaratrion in Class
test(5);
int k;
k = recursive(10);
cout << k << endl;
cout << recursive(33, 44) << endl;
getchar();
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment