Você está na página 1de 2

Explain numeric, character and Boolean data types.

Answer
Various data types in C++:
Numeric: This is a fundamental type provided by C++ language. Integers, Floating point
types come under this.
Boolean: Can have one of the two values, true or false. It is used to express the results of
a logical operations. They are internally stored as number. A non-zero value is considered
true whereas zero is false.
Eg:
Void f(int x, int y)
{
Bool b;
If (x == y)
b = true;
}

Thus, value of b becomes true if x equals y.


A common use of bool is as the type of the result of a function that tests some condition.
e.g. bool isopen(File *fp);
Character:
A variable of type char can hold any character of the implementation’s character set. It
takes 1 byte of storage and it can hold 256 different values.

E.g. char c = ‘a’;


Since character types are integral types, arithmetic and logical operations apply on them.

What is Typecasting. Explain with examples.


Answer
Typecasting: C++ is very strict about type compatibility. Different variable types must be
cast when their values are assigned to each other. Type cast operator is used for explicit
type conversion of variables. A type name behaves as if it is a function for converting
values to a designated type.

E.g. average = sum/float(i);


The variable ‘i’ is converted to float and used to calculate the average.

Explain :: operator with an example.


Answer
:: Operator: ‘::’ is known as Scope Resolution Operator. C++ is a block structured
language. Different program modules are written in various blocks. Same variable name
can be used in different blocks. Scope of a variable extends from the point of declaration
to the end of the block. A variable declared inside a block is ‘local’ variable. Blocks in
C++ are often nested.
e.g.
….
….
{
int x = 10;
….
….
{
int x = 20;
…..
….
}
….
}

The declaration of the inner block hides the declaration of same variable in outer block.
This means, within the inner block, the variable x will refer to the data object declared
therein. To access the global version of the variable, C++ provides scope resolution
operator.

In the above example, x has a value of 20 but ::x has value 10.
Similarly, this operator is used when a member function is defined outside the class

e.g.
Class MyClass
{
int n1, n2;
public:
{
void func1(); ---------Function Declaration
}
};

public void MyClass::func1() ---Use of Scope Resolution Operator to write


function definition outside class definition
{
// Function Code
}

What is const qualifier? Explain with an example.


The qualifier const can be applied to the declaration of any variable to indicate that its
value will not be changed. Once a variable is declared with const qualifier, it no longer
remains a variable (i.e. its value can not be changed). A const must be initialized with
some value.
e.g. const TYPE x = 10; //where TYPE is any data type.
const int x = 5; // This will define x as constant with value 5

Note that once const is defined, its value can not be changed by arithmetic operations or
assignment.

Você também pode gostar