You are on page 1of 20

C++

C++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs in Murray Hill,
New Jersey, USA, as an enhancement to the C language and originally named C with
Classes but later it was renamed C++ in 1983. Supports Object oriented technology to
develop software and superset of C.

C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions
of UNIX. C++ is a statically typed, compiled, general-purpose, case-sensitive
programming language. C++ is regarded as a middle-level language, as it comprises a
combination of both high-level and low-level language features. C++ is a superset of C,
and that virtually any legal C program is a legal C++ program.

Note − A programming language is said to use static typing when type checking is
performed during compile-time as opposed to run-time.

Object-Oriented Programming

Object oriented Programming is defined as an approach that provides a way of


modularizing programs by creating partitioned memory area for both data and
functions that can be used as templates for creating copies of such modules on demand.
Writing object-oriented programs involves creating classes, creating objects from those
classes, and creating applications, which are stand-alone executable programs that use
those objects. After being created, classes can be reused over and over again to develop
new programs.

Basic Concepts of Object oriented Programming

1. Class

A class is a user defined data type. A class is a logical abstraction. Once a class has been
defined, we can create any number of objects belonging to that class. Thus a class is a
collection of objects of similar types. For example mango, apple and orange are
members of class fruit. A class specifies both code and data. When you define a class,
you declare the data that it contains and the code that operates on that data. Data is
contained in instance variables defined by the class known as data members, and code
is contained in functions known as member functions. The code and data that constitute
a class are called members of the class.

2. Object

An object is an identifiable entity with specific characteristics and behavior. An object is


said to be an instance of a class. Defining an object is similar to defining a variable of
any data type. They may represent a person, a place or any item that the program has to
handle.
3. Constructors

A constructor is a special member function whose task is to initialize the object of its
class. It is special because its name is the same as the class name. The constructor is
invoked whenever an object of its class is created. It is called constructor because it
construct the value of data members of the class.

4. Encapsulation

The wrapping up of data and function into a single unit called class is known as
encapsulation and keeps both safe from outside interference and misuse. The data is not
accessible to the outside world, and only those functions which are wrapped in the class
can access it. These function provide the interface between the object`s data and the
program. This insulation of the data from direct access by the program is called data
hiding or information hiding.

5. Data abstraction

Abstraction refers to the act of representing essential feature without including the
background details .Classes defined a list of attributes as size, weight and cost, and
functions to operate on these attributes. The internal details of the objects are hidden
which makes them abstract. The technique of hiding internal details in an object is
called data abstraction.
Since the classes use the concept of abstraction, they are known as ADT.

6. Inheritance

Inheritance is the mechanism by which one class can inherit the properties of another. It
allows a hierarchy of classes to be build, moving from the most general to the most
specific. When one class is inherited by another, the class that is inherited is called the
base class. The inheriting class is called the derived class. In general, the process of
inheritance begins with the definition of a base class. The base class defines all qualities
that will be common to any derived class. . In OOPs, the concept of inheritance provides
the idea of reusability. In essence, the base class represents the most general description
of a set of traits. The derived class inherits those general traits and adds properties that
are specific to that class.

7. Polymorphism

Polymorphism (from the Greek, meaning “Ability to take more than one forms”) is a
feature that allows one interface to be used for a general class of actions. The concept of
polymorphism is often expressed by the phrase “one interface, multiple methods.” The
concept of polymorphism is implemented using the overloaded function and operators.

 The overloaded member functions are selected and invoking by matching


arguments, both type and numbers. This information is known to the compiler at
the compile time and compiler is able to select the appropriate function for a
particular call at the compile time itself. This is called EARLY
BINDING/STATIC BINDING or STATIC LINKING and also known as
COMPILE TIME POLYMORPHISM.

 If the member function could be selected while the program is running. This is
known as RUN TIME POLYMORPHISM. Also known as LATE
BINDING/DYNAMIC BINDING. C++ supports a mechanism known as virtual
functions to achieve run time Polymorphism.

 The process of making an operator to exhibit different behavior in different


instances is known as Operator Overloading.

 Using a single function name to perform different type of task is known as


Function Overloading.

Need for Object oriented Programming

Object-oriented programming scales very well, from the most trivial of problems to the
most complex tasks. It provides a form of abstraction that resonates with techniques
people use to solve problems in their everyday life. Object-oriented programming was
developed because limitations were discovered in earlier approaches to programming.
There were two problems. First, functions have unrestricted access to global data.
Second, unrelated functions and data, the basis of the procedural paradigm, provide a
poor model of the real world.

Benefits of Object oriented Programming

1. Simplicity: Software objects model real world objects, so the complexity is reduced
and the program structure is very clear.
2. Modularity: Each object forms a separate entity whose internal workings are
decoupled from other parts of the system.
3. Modifiability: It is easy to make minor changes in the data representation or the
procedures in an OO program. Changes inside a class do not affect any other part of a
program
4. Extensibility: adding new features or responding to changing operating
environments can be solved by introducing a few new objects and modifying some
existing ones.
5. Maintainability: objects can be maintained separately, making locating and fixing
problems easier.
6. Re-usability: objects can be reused in different programs.

Use of C++

1. C++ is used by hundreds of thousands of programmers in essentially every


application domain.
2. C++ is being highly used to write device drivers and other software that rely on
direct manipulation of hardware under real time constraints.
3. C++ is widely used for teaching and research because it is clean enough for successful
teaching of basic concepts.
4. Anyone who has used either an Apple Macintosh or a PC running Windows has
indirectly used C++ because the primary user interfaces of these systems are written in
C++.

Applications of Object Oriented Programming

1. Real-time systems.
2. Simulation and Modeling.
3. Object- oriented Database.
4. Hypertext, Hypermedia, and expert text.
5. AI and expert systems.
6. Neural networks and office parallel programming.
7. Decision support and office automation systems
8. CAM/CAD systems.

C++ CHARACTER SET

Character set is an asset of valid characters that a language can recognize. A character
can represents any letter, digit, or any other sign. Following are some of the C++
character set.
LETTERS A to Z and a to z
DIGITS 0 – 9
SPECIAL SYMBOLS + -* A \ [] {} = != < > . ‘ ‘ ; : & #
WHITE SPACE Blank space , horizontal tab (- >), carriage return , Newline.
OTHER CHARACTERS 256 ASCII characters as data or as literals.
TOKENS

The smallest lexical unit in a program is known as token. A token can be any keyword,
Identifier, Literals, Punctuators, and Operators.

KEYWORDS: (63) Keywords are the certain reserved words that convey a special
meaning to the compiler. These are reserve for special purpose and must not be used as
identifier name. Following are some of the Keywords.eg. for, if, else, this etc

IDENTIFIERS: Identifiers are programmer defined names given to the various


program elements such as variables, functions, arrays, objects, classes, etc. It may
contain digits, letters and underscore, and must begin with a letter or underscore. C++
is case sensitive as it treats upper and lower case letters differently. The following are
some valid identifiers:
Total, sum, _help, h_ram60 etc.

LITERALS: The data items which never change their value throughout the program
run. There are several kind of literals:

 Integer constant
 Character constant
 Floating constant
 Boolean constant
 String constant
 Wide-Character Constant

Integer constant: Integer literals are whole numbers without any fractional part. An
integer literal must have at least one digit and must not contain any decimal point. It
may contain either + or - sign. A number with no sign is assumed as positive. C++
allows three types of integer literals:

(i) Decimal Integer Literals: - An integer literal without leading 0 (zero) is called decimal
integer literals e.g., 123, 786, +97, etc.
(ii) Octal Integer Literals: - A sequence of octal digit starting with 0 (zero) is taken to be
an octal integer literal (zero followed by octal digits). e.g., 0345, 0123, etc.
(iii) Hexadecimal Integer Literals: - Hexadecimal Integer Literals starts with 0x or 0X
followed by any hexa digits. e.g., 0x9A45, 0X1234, etc.
(iv) An integer literal can also have a suffix that is a combination of U and L, for
unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in
any order.eg. 215l, 215L, 215u, 215U,215UL(unsigned long)

Character Constant: A character constant is single character which is enclosed within


single quotation marks. If the literal begins with L (uppercase only), it is a wide
character literal (e.g., L'x') and should be stored in wchar_t type of variable. Otherwise,
it is a narrow character literal (e.g., 'x') and can be stored in a simple variable of char
type.
A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or a
universal character (e.g., '\u02C0').

Floating Constant: Numbers which are having the fractional part are referred as
floating numbers or real constants; it may be a positive or negative number. A number
with no sign is assumed to be a positive number. e.g 2.0, 17.5, -0.00256, 314159E-5.

Boolean Constant:- There are two Boolean literals and they are part of standard C++
keywords −
 A value of true representing true.
 A value of false representing false.
You should not consider the value of true equal to 1 and value of false equal to 0.

String Constant: It is a sequence of letters surrounded by double quotes. E.g “abc”.

Wide-character Constant: (2 to 4 Byte) The wchar_t type is a wide-character literal in


ANSI C++ and intended for character sets that cannot fit a character into a single byte
and begin with the letter L. eg- L’ab’

PUNCTUATORS: The following characters are used as punctuators which are also
know as separators in C++. [ ] { } ( ) , ; : * ………. = #

Punctuator Name Function


[] Brackets These indicates single and multidimensional array subscripts
() Parenthesis These indicate function calls and function parameters.
{} Braces Indicate the start and end of compound statements.
; Semicolon This is a statement terminator.
: Colon It indicates a labeled statement
* Asterisk It is used as a pointer declaration
… Ellipsis These are used in the formal argument lists of unction
prototype to indicate a variable number of arguments.

= Equal to It is used as an assigning operator.


# Pound sign This is used as preprocessor directives.
, Comma It is used as a separator.

There are certain characters in C++ when they are preceded by a backslash they will
have special meaning and they are used to represent like newline (\n) or tab (\t). Here,
you have a list of some of such escape sequence codes −
Escape sequence Meaning
\\ \ character
\' ' character
\" " character
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo Octal number of one to three digits
\xhh . . . Hexadecimal number of one or more digits

OPERATORS: An operator is a symbol or character or word which trigger some


operation on its operands.
(i) Unary operators: Those which require only one operand to operate upon. e.g. unary
- , unary + , ++ , - - ! .
(ii) Binary operators: Binary operators require two operands to operate upon. e.g. +, *,
/, -, etc.
(iii) Ternary Operator :Ternary operator require three operands to operate upon.
Conditional operator (? :) is a ternary operator in C++.

1. Arithmetic operators: Those operators are operates only on numeric data types
operands are known as arithmetic operators.

+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder.

2. Logical Operators: In addition to the relational operator, C++ contains three


logical operators. Relational operators often are used with logical operators to
construct more complex decision making expressions.

&& (Logical AND) op1 && op2 op1 and op2 are both true
|| (Logical OR) op1 || op2 either op1 or op2 is true
! (Logical NOT) !op1 op1 is false (it is unary operator)

3. Relational Operator: These operators are used to compare two values. If


comparison is true, the relational expression results into the value 1 and if the
comparison is false its result will be 0. The six relational operators are:

= = Equal to
!= Not equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to

4. Conditional operator ( ? : ) The conditional operator (? :) is a ternary operator


i.e., it require three operands. The general form of conditional operator is:

expression1? expression2: expression3;

Where expression1 is a logical expression, which is either true or false. If


expression1 evaluates to true i.e., 1, then the value of whole expression is the
value of expression2, otherwise, the value of the whole expression is the value of
expression3. For ex. Min = a<b? a : b ; Here if expression (a<b ) is true then the
value of a will be assigned to min otherwise value of b will be assigned to min.

5. Assignment Operator: C++ offers an assignment operator (=) to assign a value to


an identifier. The assignment statement that make use of this operator are
written in the form:
var = expression ;
where var generally represents a variable and expression may be a constant or a
variable or an expression.
C++ offers special shorthand operators that simplify the coding of a certain type
of assignment statement. e.g., a = a + 10; can be written as a+=10;
Following are some examples of C++ shorthand:
x -=10; equivalent to x = x -10 ;
x*=3 ; equivalent to x = x * 3 ;
x/=2 ; equivalent to x = x/2 ;
x%=z equivalent to x = x % z ;

6. Comma operator ( , ):-The comma operator (,) is used to separate two or more
expressions that are included where only one expression is expected. When the
set of expressions has to be evaluated for a value, only the right most expression
is considered.
For example,
a = (b =3, b +2);
Would first assign the value 3 to b, and then assign b+2 to variable a. So, at the
end, variable a would contain the value 5 while variable b would contain value 3.
7. Explicit type casting operator:- Type casting operators allow you to convert a
datatype of a given variable to another datatype. There are several ways to do
this in C++. The simplest one, which has been inherited from the C language, is
to precede the expression to be converted by the new type enclosed between
parentheses ( ):
int i;
float f =3014;
i = ( int ) f;

The previous code converts the float number 3.14 to an integer value (3), the
remainder is lost. Here, the typecasting operator was (int).

8. sizeof():- This operator accepts one parameter, which can be either a type or a
variable itself and returns the size in bytes of that type or object:

a= sizeof (char);

This will assign the value 1 to a because char is a one-byte long type. The value
returned by sizeof is a constant, so it is always determined before program
execution.
9 Increment and Decrement Operators (++ , - -) :- The increment operator (++)
adds 1 to its operand and decrement operator (--) subtract one from its operand.

a = a + 1; is same as ++a; or a++; a = a – 1 ; is same as --a; or a--;

 Prefix increment/decrement :- When an increment or decrement operator


precedes its operand, it is called prefix increment or decrement (or pre-increment
/ decrement). In prefix increment/decrement , C++ perform the increment or
decrement operation before using the value of the operand. e.g.,

If sum = 10 and count =10 then


Sum = sum + (++count);
First count incremented and then evaluate sum = 21.
 Postfix increment/decrement: - When an increment or decrement operator
follows its operand, it is called postfix increment or decrement (or post-
increment / decrement). In postfix increment/decrement , C++ first uses the
value of the operand in evaluating the expression before incrementing or
decrementing the operand’s value. e.g.,
If sum = 10 and count =10 then
Sum = sum +(count++);
First evaluate sum = 20 , and then increment count to 11.

Basic Data Types


Size and range of Basic Data Types

Expressions:-An expression in C++ is any valid combination of operators, constants,


and variables.
1. Pure Expressions:-If an expression has all operand of same data types then it is
called a pure expression.
2. Mixed Expressions:-If an expression has operands of two or more different data
types then it is called a mixed expression.
3. Arithmetic Expressions:-Arithmetic expression can either be integer expressions
or real expressions. Sometimes a mixed expression can also be formed which is a
mixture of real and integer expressions.
4. Integer Expressions:- Integer expressions are formed by connecting all integer
operands using integer arithmetic operators.
5. Real Expressions:- Real expressions are formed by connecting real operands by
using real arithmetic operators.
6. Logical Expressions:- The expressions which results evaluates either 0 (false) or 1
(true) are called logical expressions. The logical expressions use relational or
Logical operators.

Type Conversion:-The process of converting one predefined data type into another is
called type conversion. C++ facilitates the type conversion in two forms:

1. Implicit type conversion:- An implicit type conversion is a conversion


performed by the compiler without programmer’s intervention. An implicit
conversion is applied generally whenever different data types are intermixed in
an expression. The C++ compiler converts all operands upto the data type of the
largest data type’s operand, which is called type promotion.
2. Explicit type conversion :- An explicit type conversion is user-defined that forces
an expression to be of specific data type.
Type Casting:- The explicit conversion of an operand to a specific type is called
type casting.

Statements:-Statements are the instructions given to the Computer to perform any


kind of action.

1. Null Statement:- A null statement is nothing only a ;. A null (or empty


statement have the following form:
; // only a semicolon (;)
2. Compound Statement :-A compound statement is a group of statements
enclosed in the braces { }.

Input Output (I/O) In C++

The cout Operator (Output Operator): The identifier cout is a predefined object in
C++ that represents the standred output stream . cout sends all out put to the screen.
The operator << is called the “insertion or put to operator”, known as bitwise left
shift operator

The cin operator (Input Operator): The cin operator is used to get input from the
keyboard. When a program reaches the line with cin, the user at the keyboard can enter
values directly into variables. The identifier cin is a predefined object in C++ that
represents the standred output stream.
The operator >> is known as “Extraction or get from operator”. Also known as
bitwise right shift operator.
Cascading of Operator:

When shift operators ( << and >>) are used more than one time in a single statement
then it is called as cascading of operators. e.g cout<< roll<< age<< endl;

COMMENTS in a C++ program


Comments are the line that compiler ignores to compile or execute. There are two types
of comments in C++.

1. Single line comment: This type of comment deactivates only that line where
comment is applied. Single line comments are applied with the help of “ //” . e.g //
cout<<tomorrow is holiday the above line is proceeding with // so compiler wont
access this line.
Multi line Comment: This Type of comment deactivates group of lines when applied.
This type of comments are applied with the help of the operators “/*” and “*/ ”. These
comment mark with /* and end up with */. This means everything that falls between
/*and */ is considered even though it is spread across many lines.

Preprocessor Directives:

#include is the preprocessor directive used in C++ programs. This statement tells the
compiler to include the specified file into the program. This line is compiled by the
processor before the compilation of the program.

Defining Constants:- There are two simple ways in C++ to define constants.
 Using #define preprocessor.
 Using const keyword.

 #define directive (Defined constants)

We can define our own names for constants that we use very often without having to
resort to memory-consuming variables, simply by using the #define preprocessor
directive. Its format is:

#define identifier value


#define PI 3.14

 Declared constants (const)

With the const prefix we can declare constants with a specific type in the same way as
we would do with a variable. For example:
const int pathwidth = 100;

typedef keyword:

Using the keyword typedef, we can create an alias (a synonym) for existing
fundamental or compound datatypes in C++.

Syntax: typedef existing_type new_type_name ;


where existing_type is a C++ fundamental or compound type and new_type_name is
the name for the new type we are defining. For example:
typedef char C;
typedef unsigned int WORD;

C mychar, anotherchar, *ptc1;


WORD myword;

typedef does not create different types. It only creates synonyms of existing types.
That means that the type of myword can be considered to be either WORD or unsigned
int, since both are in fact the same type. typedef can be useful to define an alias for a
type that is frequently used within a program. It is useful if we want to use has a name
that is too long or confusing

Derived Data type:


The datatypes which are extracted / derived from fundamental data types are called
derived datatypes. These datatypes can be derived by using the declaration operator or
punctuators for e.g Arrays, function, Pointer, Class, Structure, union etc.
Class: A class represents a group of similar objects. To represent class in C++ it offers a
user defined datatypes called CLASS. Once a Class has been defined in C++, Object
belonging to that class can easily be created. A Class bears the same relationship to an
object that a type does to a variable.
Syntax of CLASS:
class class_name
{
Private:
Data members 1

Data members n
Member functions 1

Member functions n

Public:
Data members 1

Data members n
Member functions 1

Member functions n
};//end of class
Class name object of Class; // creating an object of class. Private and Public are the
access specifiers to the class.

STRUCTURE:
A Structure is a collection of variables of different data types referenced under one
name .It also may have same data types. The access to structure variables is by default
global i.e they can be accessed publicly throughout the program.
Syntax of structure.
struct structure_name
{
Structure variable 1;
Structure variable n;
}; // end of structure

Structure_name structure object // creating object variable of structure.


e.g
struct student
{
int roll;
float marks;
};

Student s;
Access to structure variables
Structure variable can be accessed by their objects only as shown below structure
object_name. structure object_name. variable
e.g

student . roll
here student is the structure and roll is the member of the structure.

UNION:
A memory location shared between two different variables of different datatypes at
different times is know as Union. Defining union is similar as defining the structure.
Syntax of Union:

union show
{
int I;
char ch;
};

Union show obj;

References:
A reference is an alternative name for an object. A reference variable provides an alias
for a previously defined variable. A reference declaration consists of base type, an &
(ampersand), a reference variable name equated to a variable name .the general syntax
form of declaring a reference variable is as follows.
Type & ref_variable = variable_name;
Where is type is any valid C++ datatype, ref_variable is the name of reference variable
that will point to variable denoted by variable_name.
e.g int a= 10;
int &b= a;
then the values of a is 10 and the value of b is also 10;

ERRORS:
There are many types of error that are encountered during the program run. following
are some of them:
1. Compiler error.: The errors encountered during the compilation process are called
Compiler error. Compiler error are of two types
• Syntax error.
• Semantic error.
Syntax Error: Syntax error is the one which appears when we commit any grammatical
mistakes. These are the common error and can be easily corrected. These are produced
when we translate the source code from high level language to machine language.
e.g cot<<endl; This line will produce a syntax error as there is a grammatical mistake in
the word cout
Semantic error: These errors appear when the statement written has no meaning. e.g a
+ b =c; this will result a semantically error as an expression should come on the right
hand side of and assignment statement.

2. Linker Errors: Errors appear during linking process e.g if the word main written as
mian . The program will compile correctly but when link it the linking window will
display errors instead of success.

3. Run Time error: An abnormal program termination during execution is known as


Run time Error.
e.g. If we are writing a statement X = (A + B) /C;
the above statement is grammatically correct and also produces correct result. But what
happen if we gave value 0 to the variable c, this statement will attempt a division by 0
which will result in illegal program termination. Error will not be found until the
program will be executed because of that it is termed as run time error.

4. Logical Error.: A logical error is simply an incorrect translation of either the


problem statement or the algorithm.
e.g: root1 = -b + sqrt(b * b -4*a*c) / (2 *a)
the above statement is syntactically correct but will not produce the correct answer
because the division have a higher priority than the addition, so in the above statement
division is performed first, then addition is performed but in actual practice to do
addition performed then divide the resultant value by (2* a).

Manipulators:
Manipulators are the operators used with the insertion operator << to modify or
manipulate the way data is displayed. There are two types of manipulators endl and
setw.
1. The endl manipulator: The endl manipulator outputs new line. It takes the compiler
to end the line of display.
cout << ” Kendriya Vidyalaya Sangathan”<<endl; cout<< ” Human Resource and
Development”;
The output of the above code will be
Kendriya Vidyalaya Sangathan Human Resource and development
2. The Setw Manipulator: The setw manipulator causes the number (or string) that
follows it in the stream to be printed within a field n characters wide where n is the
arguments to setw (n)

Enumerated Types

An enumerated type declares an optional type name and a set of zero or


more identifiers that can be used as values of the type. Each enumerator is
a constant whose type is the enumeration.

Creating an enumeration requires the use of the keyword enum. The


general form of an enumeration type is −

enum enum-name { list of names } var-list;

Here, the enum-name is the enumeration's type name. The list of names is
comma separated.

For example, the following code defines an enumeration of colors called


colors and the variable c of type color. Finally, c is assigned the value "blue".

enum color { red, green, blue } c;


c = blue;

By default, the value of the first name is 0, the second name has the value
1, and the third has the value 2, and so on. But you can give a name, a
specific value by adding an initializer. For example, in the following
enumeration, green will have the value 5.

enum color { red, green = 5, blue };

Here, blue will have a value of 6 because each name will be one greater
than the one that precedes it.
A variable provides us with named storage that our programs can
manipulate. Each variable in C++ has a specific type, which determines the
size and layout of the variable's memory; the range of values that can be
stored within that memory; and the set of operations that can be applied to
the variable.

The name of a variable can be composed of letters, digits, and the


underscore character. It must begin with either a letter or an underscore.
Upper and lowercase letters are distinct because C++ is case-sensitive −

Trigraphs

A few characters have an alternative representation, called a trigraph


sequence. A trigraph is a three-character sequence that represents a single
character and the sequence always starts with two question marks.
Trigraphs are expanded anywhere they appear, including within string
literals and character literals, in comments, and in preprocessor directives.

Following are most frequently used trigraph sequences −

Trigraph Replacement
??= #
??/ \
??' ^
??( [
??) ]
??! |
??< {
??> }
??- ~

All the compilers do not support trigraphs and they are not advised to be
used because of their confusing nature

Whitespace in C++

A line containing only whitespace, possibly with a comment, is known as a


blank line, and C++ compiler totally ignores it. Whitespace is the term used
in C++ to describe blanks, tabs, newline characters and comments.
Whitespace separates one part of a statement from another and enables the
compiler to identify where one element in a statement, such as int, ends and
the next element begins.
Practice Session:
1. What is the name of the function that should be present in all c++ program?
Ans. main()

2. What are C++ comments?


Ans. comments are internal documentation of a program which helps the program for
many purposes.

3. What is indentation of a program?


Ans. It is the systematic way of writing the program which makes it very clear and
readable.

4. What is #include directives?


Ans. it instructs the compiler to include the contents of the file enclosed within the
brackets into the source file.

5. What is role of main() in c++ program?


Ans. This is the first line that a C++ compiler executes. Program starts and end in this
function.

6. What is a header file?


Ans. Header file provide the declaration and prototypes for various token in a program.

7. What is the purpose of comments and indentation?


Ans. the Main purpose of comments and indentation is to make program more
readable and understandable.

8. What are console input /output functions?


Ans. Console I/O functions are cout and cin.

9. Write an appropriate statement for each of the following:


1. Write the values for a&b in one unseperated by blanks and value of after two
blanks lines.
Ans. cout<<a<<b<<endl<<endl<<c;
2. Read the values for a,b and c.
Ans. cin>>a>>b>>c;
3. Write the values for a and b in one line, followed by value of c after two balnk
lines.
Ans. cout<a<<b<<’\n\n’<<c;

10. What type of errors occurs while programming?


Ans. There are three types of errors generally occur are:
1. Syntax error
2.Semantic error
3. Type error.

11. How ‘/’ operator is different from ‘%’ operator?


Ans. ‘/’ operator is used to find the quotient whereas % operator is used to find the
remainder.

12. Which type of operator is used to compare the values of operands?


Ans. Relational operators.

13. How will you alter the order of evaluation of operator?


Ans. We can use parentheses to alter the order of evaluation of an equation.

14. What is the unary operator? Write 2 unary operator.


Ans. The operator which needs only one operand is called as unary operator. The ‘++’
(increment) and ‘ ’(decrement) operators.

15. What is output operator and input operator?


Ans. The output operator (“<<”) is used to direct a value to standard output. The input
operator (“>>”) is used to read a value from standard input.

16. What will be the output of following code:


void main()
{
int j=5;
cout<<++j<<j++<<j; // in cascading processing starts from right to left }
Ans. 7 5 5

17. What will be the output of following code: void main()


{
int j=5;
cout<<++j + j++ +j++; // values will be: 6 6 7 (From left to right)
}
Ans. 19

18. What will be the output of following code: void main()


{
int a=5, k;
k= a++ +a+ ++a;
cout<<k;
}
Ans. 18(Because in evaluation of expression first of all prefix are evaluated, then it’s
value is assigned to all occurrences of variable)

Units of Memory:
The smallest unit is bit, which mean either 0 or 1.
1 bit = 0 or 1
1 Byte = 8 bit
1 Nibble = 4 bit
1 Kilo Byte = 1024 Byte= 210 Byte
1 Mega Byte = 1024 KB= 210 KB
1 Gega Byte = 1024 MB= 210 MB
1 Tera Byte = 1024 GB= 210 GB
1 Peta Byte =1024 TB= 210 TB
1 Exa Byte =1024 PB= 210 PB
1 Zetta Byte = 1024 EB= 210 EB
1 Yotta Byte = 1024 ZB= 210 ZB

Characteristics of a Good Program:


Following are the characteristics of a good program.
1. Effective and efficient: The program produces correct results and is faster, taking
into
account the memory constraints.
2. User friendly: The program should be user friendly. The user should not be confused
during the program execution . The user should get correct direction and alerts when he
is
going through the program.
3. Self documenting code: A good program must have self documenting code. This
code
will help the programmer to identify the part of the source code and clarify their
meaning
in the program.
4. Reliable: The good program should be able to cope up from any unexpected
situations like
wrong data or no data.
5. Portable: The program should be able to run on any platform, this property eases the
use
of program in different situations.
6. Robustness: Robustness is the ability of the program to bounce back an error and to
continue operating within its environment

You might also like