Saturday, November 14, 2009

C++ Constant : Types And Uses


C++ Constant : Types And Uses

What is Constant ?

As similar to variable constant are data storage location. As the name implies constant's value do not change. They remain constant throughout the program. Unlike variable whose value can be changed anywhere in the program.

There are two types of constant in C++. They are as follows :
1) Literal Constant
float PI=3.14;
The value that is directly typed into the program is called literal constant.
Here 3.14 is called literal constant. You cannot assign a value to 3.14.
2) Symbolic Constant
Symbolic Constant are represented by name.

There are two ways to declare a symbolic constant. They are as follows :
1) By using preprocessor directive #define.
This is old way of declaring constant. It has now became obsolete way.
2) By using keyword const.
This way is appropriate way to declare constant.

A C++ Program example that demonstrate the use of constant by using preprocessor directive #define


/* Area Of Circle Program */

#include <iostream>

#define PI 3.14

using std::cout;
using std::cin;
using std::endl;

int main ()
{

    int r;

    cout << "Find the area of circle." << endl;
    cout << "Enter radius : ";
    cin >> r;

    float area = PI * r * r;

    cout << "The area of circle of radius     " << r << " is "
    << area << endl;

    return 0;
}


A C++ Program example that demonstrate the use of constant by using the keyword const


/* Area Of Circle Program */

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

const float PI = 3.14;

int main ()
{

    int r;

    cout << "Find the area of circle." << endl;
    cout << "Enter radius : ";
    cin >> r;

    float area = PI * r * r;

    cout << "The area of circle of radius " << r << " is " << area
    << endl;

    return 0;
}


Another C++ Program example that demonstrate the use of constant


/* Program that calculate total income of the year */

#include <iostream>

using std::cout;
using std::endl;

int main ()
{

    const int salary = 20000;
    float tax = (float) 10 / 100 * salary;        // tax is 10% of salary
    float monthlyIncome = salary - tax;
    // bonus is 5% of salary
    float yearlyBonus = (float) 5 / 100 * salary;
    float yearlyIncome = (monthlyIncome*12) + yearlyBonus;

    cout << "My yearly income is " << yearlyIncome << endl;

    return 0;
}


In the above program example, salary is declared as constant of type int. You can assign a value to constant only at the declaration time. This value could not be changed later on the program. If you do you will get compiler error " assignment of read-only variable 'salary' " and your program wont compile.

C++ Notes :

(1) The way to declare a string constant with #define :
#define HOBBY "Programming"
String constant must be enclosed with double-inverted commas.

(2) The way to declare a character constant with #define :
#define AGREE 'y'
Character constant must be enclosed with single-inverted commas.

(3) Numeric type of data are not enclosed with inverted commas.

(4) The advantage of using const keyword is that you can create constant of various data types by mentioning it explicitly.
For example :
const unsigned short int myVal = 40;

Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
Want to learn more? View List Of All Chapters

No comments:

Post a Comment