0% found this document useful (0 votes)
54 views10 pages

C++ Short Notes

Uploaded by

Rutumbara Chakor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views10 pages

C++ Short Notes

Uploaded by

Rutumbara Chakor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Difference between c and c++

No C C++
.

1) C follows the procedural style C++ is multi-paradigm. It supports both


programming. procedural and object oriented.

2) Data is less secured in C. In C++, you can use modifiers for class
members to make it inaccessible for outside
users.

3) C follows the top-down approach. C++ follows the bottom-up approach.

4) C does not support function overloading. C++ supports function overloading.

5) In C, you can't use functions in structure. In C++, you can use functions in structure.

6) C does not support reference variables. C++ supports reference variables.

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.

8) Operator overloading is not possible in C. Operator overloading is possible in C++.

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.

12) C does not support the inheritance. C++ supports inheritance.

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.

This pointer holds the address of the current object.


A class template is used to create a family of classes and functions

C++ Features - Simple, Machine Independent or Portable, Mid-level programming language,


Structured programming language, Rich Library, Memory Management, Fast Speed,
Pointers, Recursion, Extensible, Object Oriented, Compiler based

#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.

#include <conio.h> includes the console input output library functions.

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.

Rules for defining variables -


● A variable can have alphabets, digits and underscore.
● A variable name can start with alphabet and underscore only. It can't start with digit.
● No white space is allowed within variable name.
● A variable name must not be any reserved word or keyword e.g. char, float etc.

Types Data Types

Basic Data Type int, char, float, double, etc

Derived Data Type array, pointer, etc

Enumeration Data Type enum


User Defined Data Type structure

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

explicit new static_cast false catch

operator template friend private class

this inline public throw const_cast

delete mutable protected true try

typeid typename using virtual wchar_t

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.

Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left


Multiplicative */% Left to right

Additive +- Right to left

Shift << >> Left to right

Relational < <= > >= Left to right

Equality == !=/td> Right to left

Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Right to left

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left

Comma , Left to right

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.

Symbol Name Description

& (ampersand sign) Address operator Determine the address of a variable.

∗ (asterisk sign) Indirection operator Access the value of an address.

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

● Polymorphism - When one task is performed by different ways i.e. known as


polymorphism. In C++, we use Function overloading and Function overriding to
achieve polymorphism.

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.

● Abstraction - Hiding internal details and showing functionality is known as abstraction


● Encapsulation - Binding (or wrapping) code and data together into a single unit is
known as encapsulation.

ADVANTAGE OF OOPS OVER PROCEDURE ORIENTED PROGRAMMING


● OOPs makes development and maintenance easier where as in Procedure-oriented
programming language it is not easy to manage if code grows as project size grows.
● OOPs provide data hiding whereas in Procedure-oriented programming language a
global data can be accessed from anywhere.
● OOPs provide ability to simulate real-world event much more effectively.

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.

The function in C++ language is also known as procedure or subroutine in other


programming languages. To perform any task, we can create function. A function can be
called many times. It provides modularity and code reusability.
Advantages of functions - code optimization, code reusability

There are two types of functions in C programming:


1. Library Functions: are the functions which are declared in the C++ header files such as
ceil(x), cos(x), exp(x), etc.
2. User-defined functions: are the functions which are created by the C++ programmer, so
that he/she can use it many times. It reduces complexity of a big program and optimizes the
code

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++

A destructor works opposite to constructor; it destructs the objects of classes. It can be


defined only once in a class. Like constructors, it is invoked automatically.

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.

C++ Structure is a collection of different data types.

In C++, there are three access specifiers:


● public - members are accessible from outside the class
● private - members cannot be accessed (or viewed) from outside the class
● protected - members cannot be accessed from outside the class, however, they can
be accessed in inherited classes

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.

Operator that cannot be overloaded are as follows:


● Scope operator (::)
● Sizeof
● member selector(.)
● member pointer selector(*)
● ternary operator(?:)

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.

In C++, we use 3 keywords to perform exception handling:


● try
● catch, and
● throw
The C++ try block is used to place the code that may occur exception. The catch block is
used to handle the exception.

Data Description
Type

fstream It is used to create files, write information to files, and read information from
files.

ifstream It is used to read information from files.

ofstream It is used to create files and write information to the files.

You might also like