Contact:-9447974300

Chapter-1 Review of C++ Programming

1. OOPS means ……………………………

Object Oriented Programming Language

2. C++ is developed by …………………….

Bjarne Stroustrup.( യാനേ)

3. What is Character Set?

A character set is a set of characters a language can identify .It is also known as the alphabet of a programming language. Most C++ Compiler follows ASCII character set. They include letters, digits, special symbols, white spaces etc.

4. What are token? What are different types of tokens used in C++?

Tokens are the fundamental building blocks of a program. They are also known as Lexical unit. There are five types of tokens in C++.

1. Key words: Key words are tokens that carry a specific meaning to the language compiler.

eg: for, int, do, if etc…

2. Identifiers: Identifiers are user defined words that are used to name different program elements such as memory locations, statements, functions, classes etc.. Identifiers used for naming memory location is known as variables.

3. Literal: Literals are data items that never change their values during the program running. They are also known as constants. There are 4 types of literals

a) Integer literal: Tokens formed only by digits. It must have at least one digit and must not have decimal point. It may contain +ve or _ve sign as first character. If there is no sign it is treated as positive. Other characters are not allowed. Eg. 15, -20, +40

b) Floating Point Literal(Real constants):They have fractional part. They can be written in two forms: Fractional and exponential form. They have at least one digit and a decimal point. They have either +ve or –ve sign. If there is no sign it is treated as positive. In exponential form, there are two parts, Mantissa (Parts appearing before E)and Exponential(Power). Mantissa may be either integer or a valid fractional part. The Mantissa is followed by letter E or e and the exponent. The exponent must be an integer.

Eg: 52.15, -715.12, .458E04

c) Character Literal : Single character enclosed with single quotes and that never changes its value during program run. Value of character constant is ASCII value of the character. Non graphical symbols can be represented by using escape sequence which consists of a back slash(\) followed by one or more characters. (eg. \n, \a..). ( ‘,”, ? etc are also written with escape sequence because they carry special purpose). Eg. Valid Character Literal: ‘s’, ‘$’,’\n’ Invalid Character Literal: ‘82’, “k”, ‘\8’

d). String Literals: Sequence of one or more characters enclosed with in a pair of double quotes. Eg. “computer”. Every string in C++ terminated by a null (\0) character.

4. Punctuators: Special symbols that have syntactic or semantic meaning to the compiler. Eg: #, : , ’ , ” ,() ,[]

5. Operators: Operators are the tokens that trigger some kind of operations. The operations applied on a set of data called operands.

Based on the number of operands, operators are classified into three, Unary operator, Binary operator and Ternary operator .

A unary operator acts on a single operand. ( eg +(positive),-(negative)

A binary operator acts on two operands. (eg. Arithmatic, relational etc..)

A ternary operator(? 🙂 acts on three operands. ( conditional ? 🙂

Based on the nature of operation operators are classified into three Arithmetic, Relational and Logical operator.

a). Arithmetic operator

These are used for addition, subtraction, multiplication and division(+,-,*,/).C++ provides a special operator modulus operator(%) for getting remainder of division operation. For example 10 % 2 returns 0.

b) Relational operators

Relational operators are used to compare values. They produces only ‘True’ of ‘False’ values. The following are the relational operators in C++. Eg: ( <,>, <=,>=, ==, !=)

c). Logical operator

Logical operators allow us to combine two or more conditions. and(&&):( Result will be true only if both the combined expressions have true value) or(||):Result will be true only if any of the combined expressions have true value ) not(!): To get the negation of the give expression

There are some other operators such as,

d). Input-Output operators

The Input-Output operators are used to take input and display output. The operator(>>) is used for taking input, and the operator(<<) is used to display the output. The >> operator is called extraction or get from operator. The operator << is called insertion or put to operator. It is a binary operator.

The input operator is used with standard input stream, cin and the output operator is used with standard output stream, cout.The input operator takes the value through cin and stores in a variable.

Cin >> number;

Cout << number+2;

Cascading of I/O operators

The multiple use of input or output operators in a single statement is called cascading of I/O operators.

Eg. cin>>a>>b;

e). Assignment operator (=)

The assignment operator is used to assign a value to a variable. For example a=10 Assigns the value 10 to a

The symbol = assigns a value, where as the symbol == compare two values.

5. What are rules in naming of identifiers?

a). It may contain an arbitery long sequence of letters, digits and _(under score)

b). First character must be letter or _

c). White space and special characters are not allowed.

d). Key words can not be used.

e). Upper and Lower case letters are treated separately(case sensitive).

Eg. Valid identifiers: _dob, FOR, Count

Invalid identifiers: sum of, 1styear, for…..

6. What are Data Types? And Explain different data types in C++?

Data types are the means to identify the nature of the data and the set of operations that can be performed on the data. There are three types of data types C++,

a).Fundamental data type (built in Data types): They are defined in the C++ compiler. There are 5 types of Fundamental data types,

1). Char: They are symbols covered by the character set of C++ language. It uses only one byte of memory (storing in memory with its ASCII value). Eg: ‘m’,’f’

2). Int : Whole numbers without fractional part. It can be positive or negative, or zero. It uses two bytes of memory and can store whole numbers from -32768 to +32767.

3). Float : They are numbers with fractional part. It uses four bytes of memory. It can be represented either by scientific notation or mantissa exponential method.

4). Double: It is used for handling large floating point numbers. It uses 8 bytes of memory.

5). Void :uses zero bytes of memory.

b). Derived data types: They are derived from fundamental data types by some grouping or alteration in size. Eg. Array, pointer etc…

c). User defined data types: The programmer can define there own data types. Eg. Struct, enum, union, class etc..

7.What is type conversion?

Type conversion means converting one data type to another data type. There are two types of type conversion, Implicit type conversion (Type Promotion) and Explicit type conversion(Type Casting)

a). Implicit type conversion also known as automatic type conversion is performed by the compiler. The conversion is always from lower type to higher type, it is also known as type promotion.

b) Explicit type conversion Type casting refers to conversion that is performed explicitly using cast operator. The operator used for this purpose is known as cast operator. The cast operator takes on the format cast type (expression)

for example : int a = (int) 10.5 , Here the value 10.5 is converted to integer type

8. What are variables?

Name given to a memory location is known as variable. It is use to store and retrieve data. It have three part:

a) Variable name b). Memory address(starting address of the allocated memory)

c). Content

9. What are expressions?

An expression consists of operator and operand. There are different types of arithmetic expressions in C++: Some are,

a). Integer expression: All operands in the expressions are integers. An integer expression yields an integer result.

b). Floating point (decimal ) expression: All operands in the expression are floating points(decimals).A floating point expression yields a floating point result.

c). Relational expression ; The relational expression consists of relational operators. They are also called conditions.

d). Logical expression : A logical expression is an expression whose value is either True or False. For example x>y is a logical expression, since it can be either ‘True’ or ‘False’.

10. What is a statement? And Explain different types of statements?

Statement is the smallest executable unit of programming language. It uses (;) as delimiter. All the statements except declaration statement are executable.

a). Declaration statements: It is used to declare the type of the variable or constant used in the program. Eg. Int x, y ; int a=10;

b).Assignment statement: It is used to assign a value to a variable eg. A=10; b=c+d;

c). Input statement: It allows the user to store data in the memory (RAM) during the program execution. In C++ get from or extract operator(>>) is used for it.

Eg. Cin >> a;

d). Output statement: It allows the user to make available the results through any output device. In C++ put to or insertion operator(<<)is used for it. Eg. Cout >> a;

11. What are Jump statements?

Jump statements are used to jump unconditionally to a different statement. It is used to alter the flow of control unconditionally. There are four types of jump statements in C++ : break, continue, go to and return.

a). return statement is used to transfer control back to the calling program or to come out of a function

#include<iostream>

using namespace std;

int fact(int n); //function prototype

int main()

{

int n=0, result=0;

cout<<“Enter Number: “;

cin>>n;

result=fact(n);

cout<<“Factorial of ” <<n <<” is: ” <<result <<endl;

}

int fact(int n) //function definition

{

int f=1;

for(int i=1; i<=n; i++)

{

f=f*i;

}

return f;

}

b).break statement is used to terminate a loop or switch statement.

#include <iostream>

using namespace std;

int main(){

int num =10;

while(num<=200) {

cout<<“Value of num is: “<<num<<endl;

if (num==12) {

break;

}

num++;

}

cout<<“Hey, I’m out of the loop”;

return 0;

}

Output:

Value of num is: 10

Value of num is: 11

Value of num is: 12

Hey, I’m out of the loop

c).continue statement is used to continue to the beginning of a loop. When a continue statement is executed in a loop it skips the remaining statements in the loop and proceeds with the next iteration of the loop.

// C++ program to explain the use

// of continue statement

#include <iostream>

using namespace std;

int main()

{

// loop from 1 to 10

for (int i = 1; i <= 10; i++) {

// If i is equals to 6, continue to next iteration without printing

if (i == 6)

continue;

else

// otherwise print the value of i

cout << i << ” “;

}

return 0;

}

Output:

1 2 3 4 5 7 8 9 10

d). goto statement is used for unconditional jump. It transfers the control from one part of the program to another.

Syntax: goto label: (A label is an identifier followed by a colon)

Syntax1 | Syntax2

—————————-

goto label; | label:

. | .

. | .

. | .

label: | goto label;

Eg:

#include <iostream>

using namespace std;

int main()

{

ineligible:

cout<<“You are not eligible to vote!\n”;

cout<<“Enter your age:\n”;

int age;

cin>>age;

if (age < 18){

goto ineligible;

}

else

{

cout<<“You are eligible to vote!”;

}

}

Output:

You are not eligible to vote!

Enter your age:

16

You are not eligible to vote!

Enter your age:

7

You are not eligible to vote!

Enter your age:

22

You are eligible to vote!

12. When a C++ program is compiled, the following errors are reported:

a) Undefined symbol cin

b) Undefined symbol cout

(i) What can be the reason for these errors? (ii) How can it be solved?

About the Author

Leave a Reply