1 of 143

C++ PROGRAMMING

MAYANK SINGH (CODE_WITH_MAYANK)

2 of 143

What is C++?

  • C++ is a cross-platform language that can be used to create high-performance applications.
  • C++ was developed by “Bjarne Stroustrup”, as an extension to the C language.
  • C++ gives programmers a high level of control over system resources and memory.
  • The language was updated 3 major times in 2011, 2014, and 2017 to C++11, C++14, and C++17.

3 of 143

Why Use C++ ?

  • C++ is one of the world's most popular programming languages.
  • C++ can be found in today's operating systems, Graphical User Interfaces, and embedded systems.
  • C++ is an object-oriented programming language which gives a clear structure to programs and allows code to be reused, lowering development costs.
  • C++ is portable and can be used to develop applications that can be adapted to multiple platforms.
  • C++ is fun and easy to learn!
  • As C++ is close to C# and Java, it makes it easy for programmers to switch to C++ or vice versa

4 of 143

#include<iostream.h> 

#include <iostream.h> is a header file library that lets us work with input and output objects, such as cout , cin. Header files add functionality to C++ programs.

5 of 143

Cout Function

  • The cout object, together with the << operator,

is used to output values/print text:

  • It is similar to printf function

6 of 143

Cin Function

  • cin is a predefined variable that reads data

from the keyboard with the extraction operator

(>>).

  • It is similar to scanf function.

7 of 143

Program to print msg. ?

#include<iostream.h>

#include<conio.h>

Void main()

{

Cout<<“hello world….!”;

Getch();

}

8 of 143

OOPS

  • Object-oriented programming – As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.

9 of 143

Characteristics of an OOPS

10 of 143

CLASS

  • Class: The building block of C++ that leads to Object-Oriented programming is a Class. It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.

11 of 143

OBJECT

  • Object: An Object is an identifiable entity with some characteristics and behaviour. An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.

12 of 143

ENCAPSULATION

  • Encapsulation: In normal terms, Encapsulation is defined as wrapping up of data and information under a single unit. In Object-Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulate them.

13 of 143

ABSTRACTION

  • Abstraction: Data abstraction is one of the most essential and important features of object-oriented programming in C++. Abstraction means displaying only essential information and hiding the details. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation.

14 of 143

POLYMORPHISM

  • Polymorphism: The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.
  • A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person posses different behaviour in different situations. This is called polymorphism.

15 of 143

INHERITANCE

  • Inheritance: The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming.
  • Sub Class: The class that inherits properties from another class is called Sub class or Derived Class.

16 of 143

  • Super Class: The class whose properties are inherited by sub class is called Base Class or Super class.
  • Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

17 of 143

ACCESS SPECIFIERS (ACCESS MODIFIERS)

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. You will learn more about Inheritance later.

18 of 143

EXAMAPLE OF CLASS AND OBJECT

#Include<iostream.h>

#include<conio.h>

Class Abhi{ //defining class

Private: // access specifier

Int a,b; // data members

};

Int main()

{

Abhi a1; //creating object as a1

a1.a=12; // assigning values in data members

a1.b=13;

Cout<<“sum of members=”<<a1.a+a1.b;

}

Sum of members=25

19 of 143

Example of Public and Private Access specifier

class MyClass {�public:    // Public access specifier�int x;   // Public attribute�private:   // Private access specifier�int y;   // Private attribute�};

int main() {�  MyClass myObj;�  myObj.x = 25;//Allowed  myObj.y = 50;//Not allowed�  return 0;�}

By default, all members of a class are PRIVATE if you don't specify an access specifier:

20 of 143

Class with Methods

Methods are functions that belongs to the class.

There are two ways to define functions that belongs to a class:

  • Inside class definition
  • Outside class definition

Note: You access methods just like you access attributes; by creating an object of the

class and using the dot syntax (.)

21 of 143

Inside class definition

class MyClass {        // The class�  public:              // Access specifier

// Method/function defined inside the class�    void myMethod() {        cout << "Hello World!";�    }�};��

int main() {� // Create an object of MyClass

  MyClass myObj; 

// Call the method�  myObj.myMethod();    return 0;�}

22 of 143

Outside class definition

class MyClass {        // The class�  public: // Access specifier

// Method/function declaration�    void myMethod();   };�// Method/function definition outside the class�void MyClass::myMethod() {�  cout << "Hello World!";�}�

int main() {

// Create an object of MyClass�  MyClass myObj; 

// Call the method�  myObj.myMethod();  �  return 0;�}

23 of 143

EXAMPLE OF PRIVATE WITH METHODS

class test{

private:

int a,b;

public:

void display(){

cout<<"sum of a+b="<<a+b;

}

void setdata(int c,int d){

a=c;

b=d;

}

};

int main()

{

test aa;

aa.setdata(12,13);

aa.display();

return 0;

}

24 of 143

C++ Constructor

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. The constructor in C++ has the same name as class or structure.

There can be two types of constructors in C++.

    • Default constructor
    • Parameterized constructor

25 of 143

Default Constructor

A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.

#include <iostream>  

class Employee  

 {  

   public:  

    Employee()    

    {    

    cout<<"Default Constructor Invoked";    

    }    

};  

int main()   

{  

    Employee e1; //creating an object     Employee e2;   

    return 0;  

}  

26 of 143

Parameterized Constructor

A constructor which has parameters is called parameterized constructor. It is used to provide different values to distinct objects.

#include<iostream.h>

class demo{

int a,b;

public:

demo(int ac,int bc)

{

a=ac;

b=bc;

}

void show(){

cout<<"sum="<<a+b;

}

};

void main(){

demo a(12,13);

a.show();

}

27 of 143

Copy Constructor�A Copy constructor is an overloaded constructor used to declare and initialize an object from another object.

Copy Constructor is of two types:

  • User Defined constructor: 

The programmer defines the user-defined constructor.

  • Default Copy constructor: 
  • The compiler defines the default copy constructor. If the user defines no copy constructor, compiler supplies its constructor.

28 of 143

Syntax Of User-defined Copy Constructor:

class A  

{  

    A(A &x) //  copy constructor.  

   {  

       // copyconstructor.  

   }  

}   

29 of 143

30 of 143

When Copy Constructor is called

Copy Constructor is called in the following scenarios:1)When we initialize the object with another existing object of the same class type. For example, Student s1 = s2, where Student is the class.�2)When the object of the same class type is passed by value as an argument.�3)When the function returns the object of the same class type by value.

31 of 143

Destructor

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.

A destructor is defined like constructor. It must have same name as class. But it is prefixed with a tilde sign (~).

Note: C++ destructor cannot have parameters. Moreover, modifiers can't be applied on destructors.

32 of 143

33 of 143

34 of 143

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.

35 of 143

Declaration of friend function

36 of 143

37 of 143

38 of 143

Enumeration

Enum in C++ is a data type that contains fixed set of constants.

It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST) etc. The C++ enum constants are static and final implicitly.

C++ Enums can be thought of as classes that have fixed set of constants.

39 of 143

  • Enum improves type safety
  • Enum can be easily used in switch
  • Enum can be traversed
  • Enum can have fields, constructors and methods
  • Enum may implement many interfaces but cannot extend any class because it internally extends Enum class

Points to remember for C++ Enum

40 of 143

41 of 143

THIS Pointer (Keyword)

In C++ programming, 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.

42 of 143

43 of 143

44 of 143

Inheritance

 Inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are defined in other class.

In c++, the class which inherits the members of another class is called derived class and the class whose members are inherited is called base class. The derived class is the specialized class for the base class.

45 of 143

Advantage of C++ Inheritance

Code reusability: Now you can reuse the members of your parent class. So, there is no need to define the member again. So less code is required in the class.

46 of 143

Types Of Inheritance

five types of inheritance:

  • Single inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Multilevel inheritance
  • Hybrid inheritance

47 of 143

Derived Class

A Derived class is defined as the class derived from the base class.

The Syntax of Derived class:

class derived_class_name : visibility-mode base_class_name  

{  

    // body of the derived class.  

}  

48 of 143

derived_class_name: It is the name of the derived class.

visibility mode: The visibility mode specifies whether the features of the base class are publicly inherited or privately inherited. It can be public or private.

base_class_name: It is the name of the base class.

  • When the base class is privately inherited by the derived class, public members of the base class becomes the private members of the derived class. Therefore, the public members of the base class are not accessible by the objects of the derived class only by the member functions of the derived class.

49 of 143

  • When the base class is publicly inherited by the derived class, public members of the base class also become the public members of the derived class. Therefore, the public members of the base class are accessible by the objects of the derived class as well as by the member functions of the base class.

Note:

  • In C++, the default mode of visibility is private.
  • The private members of the base class are never inherited.

50 of 143

Single InheritanceSingle inheritance is defined as the inheritance in which a derived class is inherited from the only one base class.

A

B

Base

Class

Derived

Class

Class A gives properties to Class B

51 of 143

52 of 143

METHODS INHERIT

53 of 143

Multilevel Inheritance�

When one class inherits another class which is further inherited by another class, it is known as multi level inheritance in C++. Inheritance is transitive so the last derived class acquires all the members of all its base classes.

A

C

B

54 of 143

55 of 143

56 of 143

Multiple Inheritance�Multiple inheritance is the process of deriving a new class that inherits the attributes from two or more classes.��

A

B

C

D

Syntax of the Derived class

class D : visibility B-1, visibility B-2, ?  

{  

    // Body of the class;  

}   

57 of 143

58 of 143

Hybrid Inheritance�Hybrid inheritance is a combination of more than one type of inheritance.

A

B

C

D

MULTILEVEL INHERITANCE

MULTIPLE INHERITANCE

59 of 143

60 of 143

Hierarchical InheritanceHierarchical inheritance is defined as the process of deriving more than one class from a base class.

C

A

D

B

A is a base class

B,C,D is a derived class

61 of 143

62 of 143

PolymorphismThe term "Polymorphism" is the combination of "poly“ +"morphs" which means many forms. It is a Greek word.

Real Life Example Of Polymorphism

A lady behaves like a teacher in a classroom, mother or daughter in a home and customer in a market. Here, a single person is behaving differently according to the situations.

63 of 143

There are two types of polymorphism:

64 of 143

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. It is achieved by function overloading and operator overloading which is also known as static binding or early binding. Now, let's consider the case where function name and prototype is same.

STATIC BINDING

65 of 143

Overloading (Function and Operator)

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. In C++, we can overload:

  • methods,
  • constructors, and
  • indexed properties

66 of 143

Types of overloading in C++:

  • Function overloading
  • Operator overloading

67 of 143

Function 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++. In function overloading, the function is redefined by using either different types of arguments or a different number of arguments. It is only through these differences compiler can differentiate between the functions.

68 of 143

69 of 143

Operators Overloading

Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type. For example, C++ provides the ability to add the variables of the user-defined data type that is applied to the built-in data types.

The advantage of Operators overloading is to perform different operations on the same operand.

70 of 143

Operator that cannot be overloaded are as follows:

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

Syntax of Operator Overloading(outside class)

return_type class_name  : : operator op(argument_list)  

{  

     // body of the function.  

}

71 of 143

Rules for Operator Overloading

  • Existing operators can only be overloaded, but the new operators cannot be overloaded.
  • The overloaded operator contains atleast one operand of the user-defined data type.
  • We cannot use friend function to overload certain operators. However, the member function can be used to overload those operators.
  • When unary operators are overloaded through a member function take no explicit arguments, but, if they are overloaded by a friend function, takes one argument.
  • When binary operators are overloaded through a member function takes one explicit argument, and if they are overloaded through a friend function takes two explicit arguments.

72 of 143

OPERATOR

73 of 143

INCREMENT OPERATOR OVERLOADING

74 of 143

Binary operator overloading�Rule(left = calling)(right =argument)

75 of 143

RUN TIME POLYMORPHISM

DYNAMIC BINDING

Function Overriding

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. It enables you to provide specific implementation of the function which is already provided by its base class.

76 of 143

Single Inheritance overridding

77 of 143

Multiple Inheritance overridding

78 of 143

virtual function

A virtual function is a member function which is declared within a base class and is re-defined(Overriden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function. 

  • Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call.
  • They are mainly used to achieve Runtime polymorphism
  • Functions are declared with a virtual keyword in base class.
  • The resolving of function call is done at Run-time.

79 of 143

In late binding function call is resolved during runtime. Therefore compiler determines the type of object at runtime, and then binds the function call.

Late binding or Dynamic linkage

80 of 143

  • Virtual functions must be members of some class.
  • Virtual functions cannot be static members.
  • They are accessed through object pointers.
  • They can be a friend of another class.
  • A virtual function must be defined in the base class, even though it is not used.
  • The prototypes of a virtual function of the base class and all the derived classes must be identical. If the two functions with the same name but different prototypes, C++ will consider them as the overloaded functions.

Rules of Virtual Function

81 of 143

82 of 143

83 of 143

Pure Virtual Function

  • A virtual function is not used for performing any task. It only serves as a placeholder.
  • When the function has no definition, such function is known as "do-nothing" function.
  • The "do-nothing" function is known as a pure virtual function. A pure virtual function is a function declared in the base class that has no definition relative to the base class.
  • A class containing the pure virtual function cannot be used to declare the objects of its own, such classes are known as abstract base classes.
  • The main objective of the base class is to provide the traits to the derived classes and to create the base pointer used for achieving the runtime polymorphism.

84 of 143

85 of 143

Interfaces in C++ (Abstract Classes)

Abstract classes are the way to achieve abstraction in C++. Abstraction in C++ is the process to hide the internal details and showing functionality only. Abstraction can be achieved by two ways:

  1. Abstract class
  2. Interface

Abstract class and interface both can have abstract methods which are necessary for abstraction.

86 of 143

C++ Abstract class�In C++ class is made abstract by declaring at least one of its functions as <>strong>pure virtual function. A pure virtual function is specified by placing "= 0" in its declaration. Its implementation must be provided by derived classes.

87 of 143

Data Abstraction in C++

  • Data Abstraction is a process of providing only the essential details to the outside world and hiding the internal details, i.e., representing only the essential details in the program.

  • Data Abstraction is a programming technique that depends on the separation of the interface and implementation details of the program.

88 of 143

Data Abstraction can be achieved in two ways:�Abstraction using classes�Abstraction in header files.

Abstraction using classes: An abstraction can be achieved using classes. A class is used to group all the data members and member functions into a single unit by using the access specifiers. A class has the responsibility to determine which data member is to be visible outside and which is not.

Abstraction in header files: An another type of abstraction is header file. For example, pow() function available is used to calculate the power of a number without actually knowing which algorithm function uses to calculate the power. Thus, we can say that header files hides all the implementation details from the user.

89 of 143

Abstraction in header files:

90 of 143

Abstraction using classes:

91 of 143

C++ Strings

In C++, string is an object of string class that represents sequence of characters. We can perform many operations on strings such as concatenation, comparison, conversion etc.

92 of 143

�String Compare

Example of string comparison using strcmp() function.

C++ string class

93 of 143

�String Concat

Example of string concatenation using strcat() function.

94 of 143

String Copy

Example of copy the string using strcpy() function.

95 of 143

String Length

Example of finding the string length using strlen() function.

96 of 143

C++ Recursion

When function is called within the same function, it is known as recursion in C++. The function which calls the same function, is known as recursive function.

A function that calls itself, and doesn't perform any task after function call, is known as tail recursion. In tail recursion, we generally call the same function with return statement.

recursionfunction(){    

recursionfunction(); //calling self function    

   

97 of 143

98 of 143

99 of 143

C++ Files

The fstream library allows us to work with files.

To use the fstream library, include both the standard <iostream> AND the <fstream> header file:

100 of 143

Create and Write To a File

To create a file, use either the ofstream or fstream class, and specify the name of the file.

To write to the file, use the insertion operator (<<).

101 of 143

Read a File

To read from a file, use either the ifstream or fstream class, and the name of the file.

Note that we also use a while loop together with the getline() function (which belongs to the ifstream class) to read the file line by line, and to print the content of the file:

102 of 143

103 of 143

Read and write files

104 of 143

FSTREAM FILE HANDLING(WRITE MODE)

105 of 143

FSTREAM FILE HANDLING(APPEND MODE)

106 of 143

FSTREAM FILE HANDLING(READ MODE)

107 of 143

C++ Templates

A C++ template is a powerful feature added to C++. It allows you to define the generic classes and generic functions and thus provides support for generic programming. Generic programming is a technique where generic types are used as parameters in algorithms so that they can work for a variety of data types.

Templates can be represented in two ways:

  • Function templates
  • Class templates

108 of 143

Function Templates:

We can define a template for a function. For example, if we have an add() function, we can create versions of the add function for adding the int, float or double type values.

Class Template:

We can define a template for a class. For example, a class template can be created for the array class that can accept the array of various types such as int array, float array or double array.

109 of 143

Function Template

  • Generic functions use the concept of a function template. Generic functions define a set of operations that can be applied to the various types of data.
  • The type of the data that the function will operate on depends on the type of the data passed as a parameter.
  • For example, Quick sorting algorithm is implemented using a generic function, it can be implemented to an array of integers or array of floats.
  • A Generic function is created by using the keyword template. The template defines what function will do.

110 of 143

Syntax of Function Template

template < class Ttype> 

ret_type func_name(parameter_list)  {  

    // body of function.  

}  

111 of 143

Function TEMPLATE

112 of 143

CLASS TEMPLATE

A class template must be declared before any instantiation of a corresponding template class. A class template definition can only appear once in any single translation unit. A class template must be defined before any use of a template class that requires the size of the class or refers to members of the class.

113 of 143

114 of 143

C++ Exceptions Handling

When executing C++ code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things.

When an error occurs, C++ will normally stop and generate an error message. The technical term for this is: C++ will throw an exception (throw an error).

115 of 143

C++ try and catch

Exception handling in C++ consist of three keywords: trythrow and catch:

  • The try statement allows you to define a block of code to be tested for errors while it is being executed.
  • The throw keyword throws an exception when a problem is detected, which lets us create a custom error.
  • The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

116 of 143

SYNTAX

try {�  // Block of code to try�  throw exception; // Throw an exception when a problem arise�}�catch (type Arg) {�  // Block of code to handle errors�}

117 of 143

118 of 143

Handle Any Type of Exceptions (...)If you do not know the throw type used in the try block, you can use the "three dots" syntax (...) inside the catch block, which will handle any type of exception:

119 of 143

120 of 143

DATA STRUCTURE

AND ALGORITHMS

121 of 143

Why to Learn Data Structure and Algorithms?

As applications are getting complex and data rich, there are three common problems that applications face now-a-days.

  • Data Search − Consider an inventory of 1 million(106) items of a store. If the application is to search an item, search an item in 1 million(106) items every time slowing down the search. As data grows, search will become slower.
  • Processor speed − Processor speed although being very high, falls limited if the data grows to billion records.
  • Multiple requests − As thousands of users can search data simultaneously on a web server, even the fast server fails while searching the data.

122 of 143

Algorithms Basics

Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired output. Algorithms are generally created independent of underlying languages, i.e. an algorithm can be implemented in more than one programming language.

From the data structure point of view, following are some important categories of algorithms −

  • Search − Algorithm to search an item in a data structure.
  • Sort − Algorithm to sort items in a certain order.
  • Insert − Algorithm to insert item in a data structure.
  • Update − Algorithm to update an existing item in a data structure.
  • Delete − Algorithm to delete an existing item from a data structure.

123 of 143

Characteristics of an Algorithm

Not all procedures can be called an algorithm. An algorithm should have the following characteristics −

  • Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and their inputs/outputs should be clear and must lead to only one meaning.
  • Input − An algorithm should have 0 or more well-defined inputs.
  • Output − An algorithm should have 1 or more well-defined outputs, and should match the desired output.
  • Finiteness − Algorithms must terminate after a finite number of steps.
  • Feasibility − Should be feasible with the available resources.
  • Independent − An algorithm should have step-by-step directions, which should be independent of any programming code.

124 of 143

How to Write an Algorithm?

There are no well-defined standards for writing algorithms. Rather, it is problem and resource dependent. Algorithms are never written to support a particular programming code.

As we know that all programming languages share basic code constructs like loops (do, for, while), flow-control (if-else), etc. These common constructs can be used to write an algorithm.

�We write algorithms in a step-by-step manner, but it is not always the case. Algorithm writing is a process and is executed after the problem domain is well-defined. That is, we should know the problem domain, for which we are designing a solution�

125 of 143

Making Algorithm

PROBLEM − Design an algorithm to add two numbers and display the result.

Step 1 − START

Step 2 − declare three integers a, b & c

Step 3 − define values of a & b

Step 4 − add values of a & b

Step 5 − store output of step 4 to c

Step 6 − print c

Step 7 − STOP

126 of 143

Writing step numbers, is optional.We design an algorithm to get a solution of a given problem. A problem can be solved in more than one ways.

127 of 143

Algorithm Analysis

  • Efficiency of an algorithm can be analyzed at two different stages, before implementation and after implementation. They are the following −
  • A Priori Analysis − This is a theoretical analysis of an algorithm. Efficiency of an algorithm is measured by assuming that all other factors, for example, processor speed, are constant and have no effect on the implementation.
  • A Posterior Analysis − This is an empirical analysis of an algorithm. The selected algorithm is implemented using programming language. This is then executed on target computer machine. In this analysis, actual statistics like running time and space required, are collected.

128 of 143

Algorithm Complexity

Suppose X is an algorithm and n is the size of input data, the time and space used by the algorithm X are the two main factors, which decide the efficiency of X.

  • Time Factor − Time is measured by counting the number of key operations such as comparisons in the sorting algorithm.
  • Space Factor − Space is measured by counting the maximum memory space required by the algorithm.

The complexity of an algorithm f(n) gives the running time and/or the storage space required by the algorithm in terms of n as the size of input data

129 of 143

Data Definition

Data Definition defines a particular data with the following characteristics.

  • Atomic − Definition should define a single concept.
  • Traceable − Definition should be able to be mapped to some data element.
  • Accurate − Definition should be unambiguous.
  • Clear and Concise − Definition should be understandable

Data Object

Data Object represents an object having a data.

130 of 143

Data Type

  • Data type is a way to classify various types of data such as integer, string, etc. which determines the values that can be used with the corresponding type of data, the type of operations that can be performed on the corresponding type of data. There are two data types −
  • Built-in Data Type
  • Derived Data Type

131 of 143

Built-in Data Type

Those data types for which a language has built-in support are known as Built-in Data types. For example, most of the languages provide the following built-in data types.

    • Integers
    • Boolean (true, false)
    • Floating (Decimal numbers)
    • Character and Strings

132 of 143

Derived Data Type

Those data types which are implementation independent as they can be implemented in one or the other way are known as derived data types. These data types are normally built by the combination of primary or built-in data types and associated operations on them. For example −

    • List
    • Array
    • Stack
    • Queue

133 of 143

Basic Operations

The data in the data structures are processed by certain operations. The particular data structure chosen largely depends on the frequency of the operation that needs to be performed on the data structure.

    • Deletion
    • Sorting
    • Merging�
    • Traversing
    • Searching
    • Insertion

134 of 143

Array Representation

Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.

135 of 143

136 of 143

Basic Operations

Following are the basic operations supported by an array.

  • Traverse − print all the array elements one by one.
  • Insertion − Adds an element at the given index.
  • Deletion − Deletes an element at the given index.
  • Search − Searches an element using the given index or by the value.
  • Update − Updates an element at the given index.�

137 of 143

TRAVERSE

138 of 143

INSERTION

139 of 143

DELETION

140 of 143

SEARCHING

141 of 143

UPDATION

142 of 143

143 of 143

Dynamic Memory(using NEW KEYWORD)