You are on page 1of 4

1/22/2011

Object oriented programming using C++

• Explain constant qualifier with examples? (3


marks)

The const qualifier explicitly declares a data object as something that


cannot be changed. Its value is set at initialization.
Symbolic constants can be created in C++ by:
1. Using the keyword as const and enum.
2. Instead of using #define as in C, const can be used to create typed
constants that have no type information.
3. Const modifiers default to int type.
4. Constant value is local to the file in which it is declared. To make it
global it must be declared as extern.

Example 1
const float pi = 3.14159;
const int size = 100;
char marks[size];
the declaration const size = 100;
means constant in size = 100;
*Global values can be assigned as extern const sum = 100;

Const integer values can also be assigned using enumeration as in


example 2

Example 2
enum{a,b,c};
default values of a, b and c are 0,1 and 2,which is equal to:
const a=0;
const b=1;
const c=2;
Another method would be by declaring values explicitly as shown
enum(a=2, b=-10, c=100);

1
1/22/2011

• Explain header file in C++? (3 marks)

Lines beginning with a hash sign (#) are directives for the preprocessor.
Directive #include<iostream.h> tells the preprocessor to include the
iostream standard file. This specific file (iostream) includes the
declarations of the basic standard input-output library in C++,

The old-style C++ program includes the header file as


#include <iostream.h> with extension ‘.h ’
If you are working on a new C++ compiler
#include<iostream>
using namespace std;
Must be used instead of
#include<iostream.h>

2
1/22/2011

• Explain reference variable with example?

A reference variable gives an alias name to the already defined variable


its syntax is:
Data-type & reference_name = variable_name

For example:

Float sum = 100;


Float & marks = sum;

Indicates that marks is an alternative name for the declared variable


called sum.
Since both the variables refer to the same data in the memory, they will
have same value of 100.If the value of the sum is changed, it affects the
value of the marks too.
Important points for using reference variable:
*A reference variable must be initialized in the declaration part.
*& in this context is not the address operator.

3
1/22/2011

• What is identifier? Rules for identifiers?


(7 marks)

Identifier refers to the names of the program elements such as variables,


functions and arrays.
Or
A name associated with a function or data object and used to refer to
that function or data object.

Rules for writing an identifier:


1. Identifier are the sequence of characters chosen from the set A-Z,a-
z,0-9 and _(underscore)
2. No blank spaces are allowed in between two words
3. C++ is a case sensitive language, so that ALPHA and Alpha are
treated as two different identifiers
4. The underscore symbol is generally emended in the identifier.
5. Identifier may be reasonable length of 8-10 characters through ANSI
C++ places no limit on length.
6. Variable names generally start with an alphabet and followed by
letters, digits or a combination of both.
7. The variable names cannot start with a digit.
8. A keyword cannot be used as a variable name.
9. Also one must follow the practice of choosing names for variables,
which indicate the roles of these variables in the program.

You might also like