All const does is prevent wou from using the variable to which it applies as an L-Value (on the left of an assignment). That is to say, if you declare something as const the compiler will flag an error if you then try to modify it. if you declare somthing as volitile, it means that it could be changed by something other than the immediate code which is executing, such as another thread or a hardware device, thus informing the compiler to refrain from performing optimisations such as the following. ORIGINAL CODE: bool aCondition = true; while (aCondition); AFTER OPTIMISATION: bool aCondition = true; while (true); In the example above the compiler nitices that the condition is true imediatly before entering the while loop and therefore substututes the test for an unconditional infinte loop. if on the other hand the code looked like this: volitile bool aCondition = true; while (aCondition); the compiler would refrain from performing the previous optimisation in the knowlege that the value of aCondition (having been declared as volitile) may change and the loop must therefore not be assumed to be unconditionaly infinite.
volatile int means the code and fom outside from code can changes the value but in const volatile int, code cannot changes the value but fron ouside can change the value
Well, uh, const unsigned int and const signed int..
in stdio.h:extern int printf (const char *fmt, ...);
int const *p declares a 'p' pointer, that points to a constant integer
//Array Passer //Demonstrates relationship between pointers and arrays #include <iostream> using namespace std; void increase(int* const array, const int NUM_ELEMENTS); void display(const int* const array, const int NUM_ELEMENTS); int main() { cout << "Creating an array of high scores.\n\n"; const int NUM_SCORES = 3; int highScores[NUM_SCORES] = {5000, 3500, 2700}; cout << "Displaying scores using array name as a constant pointer.\n"; cout << *highScores << endl; cout << *(highScores + 1) << endl; cout << *(highScores + 2) << "\n\n"; cout << "Increasing scores by passing array as a constant pointer.\n\n"; increase(highScores, NUM_SCORES); cout << "Displaying scores by passing array as a constant pointer to a constant.\n"; display(highScores, NUM_SCORES); return 0; } void increase(int* const array, const int NUM_ELEMENTS) { for (int i = 0; i < NUM_ELEMENTS; ++i) array [i] += 500; } void display(const int* const array, const int NUM_ELEMENTS) { for (int i = 0; i < NUM_ELEMENTS; ++i) cout << array[i] << endl; }
int max (const int a, const int b) { return a>b?a:b; } int maxn (const int a[], const unsigned n) { assert (a!=0 && n!=0); // invalid argument(s)! unsigned i, m; m = a[0]; for (i=1; i<n; ++i) m = max (m, a[i]); return m; }
#include<iostream> class base { int m_data; public: base(const int data):m_data(data){} base(const base& cpy):m_data(cpy.m_data){} base& operator=(const int rhs){m_data=rhs;return(*this);} base& operator=(const base& rhs){m_data=rhs.m_data;return(*this);} virtual ~base(){} }; class derived { public: derived(const int data):base(data){} derived(const derived& cpy):base(cpy){} derived& operator=(const int rhs){return(base::operator=(rhs));} derived& operator=(const derived& rhs){return(base::operator=(rhs));} virtual ~derived(){} }; int main() { derived d=42; }
Use the following functions: int sum (const int* const a, const unsigned size) { int s = 0; for (unsigned i=0; i<size; ++i) s += a[i]; return s; } double average (const int* const a, const unsigned size) { return (double) sum (a, size) / size; } Example usage: int main () { int[3] a = {74, 42, 64}; printf ("Sum = %d\n", sum (a, 3)); // Sum = 180 printf ("Average = %d\n", average (a, 3)); // Average = 60.0 return 0; }
#include<iostream> struct point { int x; int y; point (const int _x, const int _y): x {_x}, y {_y} {} double distance (const point&) const; }; double point::distance (const point& p) const { int w = x - p.x; int h = y - p.y; return sqrt (h*h + w*w); } std::ostream& operator<< (std::ostream& os, const point& p) { return os << '{' << p.x << ", " << p.y << '}'; } int main() { using namespace std; point a {5,10}; point b {7,2}; double d = a.distance (b); std::cout << "The distance between coordinates " << a << " and " << b << " is " << d << ".\n" << std::endl; }
int comp(const int a1[], const int a2[], const int size) { int i; for(i = 0; i < size; ++i) { if(a1[i] != a2[i]) { return 0; } } return 1; }
struct rectangle { private: int top, bottom, left, right; public: int get_width()const{return(right-left);} int get_height()const{return(bottom-top);} int get_perimeter()const{return((get_width()+get_height())*2);} }
int main(const int argc, const char** argv) {// Usage:// print the first 10 Fibonacci numbers recursivelyfib_rec(10);// print the first 10 Fibonacci numbers iterativelyfib_it(10);return 0;}// simple starting interface for recursive algorithmvoid fib_rec(const unsigned int max) {printf("F0 = 0\n");_fib_rec(0, 1, 1, max);}// recursive part of algorithmvoid _fib_rec(const unsigned int f0, unsigned const int f1, unsigned const int current, unsigned const int max) {printf("F%d = %d\n", current, f1);if(current < max) {_fib_rec(f1, f0 + f1, current + 1, max);}}// iterative solutionvoid fib_it(const unsigned int max) {int current;int f0 = 0;int f1 = 1;int temp;printf("F0 = 0\n");for(current = 1; current