Professional Documents
Culture Documents
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
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
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.
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.
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.
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. 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.
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
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)
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.
PUNCTUATORS: The following characters are used as punctuators which are also
know as separators in C++. [ ] { } ( ) , ; : * ………. = #
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
1. Arithmetic operators: Those operators are operates only on numeric data types
operands are known as arithmetic operators.
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder.
&& (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)
= = Equal to
!= Not equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
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.
Type Conversion:-The process of converting one predefined data type into another is
called type conversion. C++ facilitates the type conversion in two forms:
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;
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.
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:
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++.
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
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
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;
};
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.
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
Here, the enum-name is the enumeration's type name. The list of names is
comma separated.
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.
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.
Trigraphs
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++
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