C++ Short Notes
C++ Short Notes
No C C++
.
2) Data is less secured in C. In C++, you can use modifiers for class
members to make it inaccessible for outside
users.
5) In C, you can't use functions in structure. In C++, you can use functions in structure.
7) In C, scanf() and printf() are mainly used C++ mainly uses stream cin and cout to perform
for input/output. input and output operations.
9) C programs are divided into procedures C++ programs are divided into functions and
and modules classes.
10) C does not provide the feature of C++ supports the feature of namespace.
namespace.
11) Exception handling is not easy in C. It C++ provides exception handling using Try and
has to perform using other functions. Catch block.
C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories
of AT&T (American Telephone & Telegraph), located in U.S.A.
The namespace is a logical division of the code which is designed to stop the
naming conflict.
scope resolution operator(::) is used to define the member function outside the
class.
#include<iostream.h> includes the standard input output library functions. It provides cin and
cout methods for reading from input and writing to output respectively.
C++ I/O operation is using the stream concept. Stream is the sequence of bytes or flow of
data. It makes the performance fast.
If bytes flow from main memory to device like printer, display screen, or a network
connection, etc, this is called as output operation.
If bytes flow from device like printer, display screen, or a network connection, etc to main
memory, this is called as input operation.
The cout is a predefined object of ostream class. It is connected with the standard output
device, which is usually a display screen. The cout is used in conjunction with stream
insertion operator (<<) to display the output on a console
The cin is a predefined object of istream class. It is connected with the standard input
device, which is usually a keyboard. The cin is used in conjunction with stream extraction
operator (>>) to read the input from a console.
A variable is a name of memory location. It is used to store data. Its value can be changed
and it can be reused many times. It is a way to represent memory location through symbol
so that it can be easily identified.
Keywords are the reserved words that have a special meaning to the compiler. You cannot
use it as a variable name, constant name etc.
A list of 30 Keywords in C++ Language which are not available in C language are given
below.
asm dynamic_cast namespace reinterpret_cast bool
C++ operators
The precedence of operator species that which operator will be evaluated first and next. The
associativity specifies the operators direction to be evaluated, it may be left to right or right to
left.
C++ identifiers in a program are used to refer to the name of the variables, functions, arrays,
or other user-defined data types created by the programmer. They are the basic requirement
of any language. Every language has its own rules for naming the identifiers.
identifiers represent the essential elements in a program which are given below:
● Constants
● Variables
● Functions
● Labels
● Defined data types
Some identifier naming rules are common in both C and C++. They are as follows:
● Only alphabetic characters, digits, and underscores are allowed.
● The identifier name cannot start with a digit, i.e., the first letter should be
alphabetical. After the first letter, we can use letters, digits, or underscores.
● In C++, uppercase and lowercase letters are distinct. Therefore, we can say that C++
identifiers are case-sensitive.
● A declared keyword cannot be used as a variable name.
C++ expression consists of operators, constants, and variables which are arranged
according to the rules of the language. It can also contain function calls which return values.
An expression can consist of one or more operands, zero or more operators to compute a
value.
● The C++ if statement tests the condition. It is executed if condition is true.
● The C++ if-else statement also tests the condition. It executes if block if condition is
true otherwise else block is executed.
● The C++ if-else-if ladder statement executes one condition from multiple statements.
● The C++ switch statement executes one statement from multiple conditions.
● The C++ for loop is used to iterate a part of the program several times. If the number
of iteration is fixed, it is recommended to use for loop than while or do-while loops.
● In C++, we can use for loop inside another for loop, it is known as nested for loop
● If we use double semicolon in for loop, it will be executed infinite times
● In C++, while loop is used to iterate a part of the program several times. If the
number of iteration is not fixed, it is recommended to use while loop than for loop.
● We can also create infinite while loop by passing true as the test condition.
● In C++, we can use while loop inside another while loop, it is known as nested while
loop.
● The C++ do-while loop is used to iterate a part of the program several times. If the
number of iteration is not fixed and you must have to execute the loop at least once,
it is recommended to use do-while loop.
● The C++ break is used to break loop or switch statement. It breaks the current flow of
the program at the given condition. In case of inner loop, it breaks only inner loop.
● The C++ continue statement is used to continue loop. It continues the current flow of
the program and skips the remaining code at specified condition. In case of inner
loop, it continues only inner loop.
● The C++ goto statement is also known as jump statement. It is used to transfer
control to the other part of the program.
● The single line comment starts with // (double slash).
● The C++ multi line comment is used to comment multiple lines of code. It is
surrounded by slash and asterisk (/∗ ..... ∗/).
● array in C++ is a group of similar types of elements that have contiguous memory
location.array index starts from 0
● Advantages of arrays - code optimization, random access, easy to traverse data,
easy to manipulate data, easy to sort data
● Disadvantages of arrays - fixed size
● . To pass array to function in C++, we need to provide only array name.
● The pointer in C++ language is a variable, it is also known as locator or indicator that
points to an address of a value.
Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving strings,
trees etc. and used with arrays, structures and functions.
2) We can return multiple values from function using pointer.
3) It makes you able to access any memory location in the computer's memory.
Usage of pointer
There are many usage of pointers in C++ language.
1) Dynamic memory allocation
In c language, we can dynamically allocate memory using malloc() and calloc() functions
where pointer is used.
2) Arrays, Functions and Structures
Pointers in c language are widely used in arrays, functions and structures. It reduces the
code and improves the performance.
The sizeof() is an operator that evaluates the size of data type, constants, variable. It is a
compile-time operator as it returns the size of any variable or a constant at the compilation
time.
Array of pointers - In C++, the name of an array is considered às a pointer, i.e., the name of
an array contains the address of an element. C++ considers the array name as the address
of the first element.
A void pointer is a general-purpose pointer that can hold the address of any data type, but it
is not associated with any data type.
A reference is a variable that is referred to as another name for an already existing variable.
The reference of a variable is created by storing the address of another variable.
A pointer is a variable that contains the address of another variable. It can be dereferenced
with the help of (*) operator to access the memory location to which the pointer points.
C++ reference and pointer seem to be similar, but there are some differences that exist
between them. A reference is a variable which is another name of the existing variable, while
the pointer is variable that stores the address of another variable.
function pointer is a pointer used to point functions. It is basically used to store the address
of a function
A new operator is used to create the object while a delete operator is used to delete the
object. When the object is created by using the new operator, then the object will exist until
we explicitly use the delete operator to delete the object.
In C language, we use the malloc() or calloc() functions to allocate the memory dynamically
at run time, and free() function is used to deallocate the dynamically allocated memory
Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming
is a methodology or paradigm to design a program using classes and objects. It simplifies
the software development and maintenance by providing some concepts:
● Object - Any entity that has state and behavior is known as an object. For example:
chair, pen, table, keyboard, bike etc. It can be physical and logical.
● Class - Collection of objects is called class. It is a logical entity.
● Inheritance - When one object acquires all the properties and behaviours of parent
object i.e. known as inheritance. It provides code reusability. It is used to achieve
runtime polymorphism. Types of inheritance are
○ Single inheritance
○ Multiple inheritance
○ Hierarchical inheritance
○ Multilevel inheritance
○ Hybrid inheritance
Compile time polymorphism: The overloaded functions are invoked by matching the type and
number of arguments. This information is available at the compile time and, therefore,
compiler selects the appropriate function at the compile time.
Run time polymorphism is achieved when the object's method is invoked at the run time
instead of compile time. It is achieved by method overriding which is also known as dynamic
binding or late binding.
In C++, constructor is a special method which is invoked automatically at the time of object
creation. It is used to initialize the data members of new object generally.
There can be two types of constructors in C++.
● Default constructor - A constructor which has no argument is known as default
constructor. It is invoked at the time of creating object.
● Parameterized constructor - A constructor which has parameters is called
parameterized constructor. It is used to provide different values to distinct objects.
There are two ways to pass value or data to function in C language: call by value and call by
reference.
● While calling a function, we pass values of variables to it. Such functions are known
as “Call By Values”.
● While calling a function, instead of passing the values of variables, we pass address
of variables(location of variables) to the function known as “Call By References.
When function is called within the same function, it is known as recursion in C++
This pointer - this is a keyword that refers to the current instance of the class. There can be
3 main usage of this keyword in C++.
● It can be used to pass current object as a parameter to another method.
● It can be used to refer current class instance variable.
● It can be used to declare indexers.
Friend Function - If a function is defined as a friend function in C++, then the protected and
private data of a class can be accessed using the function.By using the keyword friend
compiler knows the given function is a friend function.For accessing the data, the declaration
of a friend function should be done inside the body of a class starting with the keyword
friend.
In C++, aggregation is a process in which one class defines another class as any entity
reference. It is another way to reuse the class. It is a form of association that represents
HAS-A relationship.
If we create two or more members having the same name but different in number or type of
parameter, it is known as C++ overloading.
Function Overloading is defined as the process of having two or more function with the same
name, but different in parameters is known as function overloading in C++.
The advantage of Function overloading is that it increases the readability of the program
because you don't need to use different names for the same action.
When the compiler is unable to decide which function is to be invoked among the overloaded
function, this situation is known as function overloading.
If derived class defines same function as defined in its base class, it is known as function
overriding in C++. It is used to achieve runtime polymorphism.
A C++ virtual function is a member function in the base class that you redefine in a derived
class. It is declared using the virtual keyword.
Exception Handling in C++ is a process to handle runtime errors. We perform exception
handling so the normal flow of the application can be maintained even after runtime errors.
Data Description
Type
fstream It is used to create files, write information to files, and read information from
files.