cpp symbolic constant

symbolic constant in C++

There are two ways of creating symbolic constant in c++

1. using the qualifier constant
2. defining a set of integers constants using enum keyword

in the both c and c++ any value declared as constant(const) can not be modified by the program in any way. however, there are some differences in implementation. in c++ we can use const in constant expression, such as

const int size=15;
char name[size];

This would be illegal in c. const allows us to create typed constants instead of having to use #define to create constants that have no type information with long and short, if we use the const modifier alone, it set defaults to int , let us take example

const size=10;

means const int size=10; The named constant are just like variables except that their values cannot be changed. c++ requires a const to be initialized. ANSI C does not required an initializer, if none is given it initialize the const to 0

The scoping of const values differs. A const in c++ programming defaults to the internal linkage and therefore it is local to the file where it is declared. In ANSI C PROGRAMMING, const values are global in nature. They are visible outside the file in which they are declare. however they can be made local by declaring them as static. To give a const value an external linkage so that it can be referenced from another file, we must explicitly define it as an extern in c++

example

extern const value=200;

another mothod of naming integer constants is by enumeration as under
enum{x,y,z}; this defines x,y and z as integer constant with values 0,1 and 2 respectively. this is equivalent to

const x=0;
const y=1;
const z=2;

so we can also assign integers values to x,y and z explicitly example

enum{x=100,y=900,z=700};

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise