4. Human resources slide 3
Phase-1:Creating a Program
Type a C++ program(typically referred to as Source code)
using an editor(Like TurboC++ ,Dev C++ , code blocks , Visual
studio, etc).
Online editor like:- Code chef , etc
And save C++ source code file names with extensions
like .cpp which indicates that a file contains C++ source
code.
Placement
Training
Introduction to C++
12. Human resources slide 3
Placement
Training
Introduction to C++
Why using namespace std ?
Namespace:-
Namespace is a declarative region that provide a scope to
the identifiers to avoid naming collisions in a program.
Demo: Need
int main()
{
int num=10;
float num=20; // Error--> two variables with same
name in same scope not allowed
cout<<"Value of Num:"<<num<<endl;
}
13. Human resources slide 3
Placement
Training
Introduction to C++
What are cout and cin?
In C, console input/output is achieved using printf() and
scanf() functions.
C++ supports an object oriented I/O system in the form
of streams.
Stream:- Stream refers to the flow of data from a
particular source to a specified destination.
In C++, classes representing each stream.
14. Human resources slide 3
Placement
Training
Introduction to C++
What are cout and cin?
Stream
class
istream
class
ostream
class
15. Human resources slide 3
Placement
Training
Introduction to C++
What are cout and cin?
Cout:- It is a pre-defined object of ostream class.
It represents output stream.
It is used to display output on the screen using Extraction
operator(<<).
Syntax:- cout<<num;
cin:- It is a pre-defined object of istream class.
It represents input stream.
It is used to take input from keyboard and store it in a
variable using Insertion operator(>>).
Syntax:- cin>>num;
21. Human resources slide 3
Placement
Training
Introduction to C++
Keywords:- Keywords are the reserved words.
They have some predefined meanings in C++.
22. Human resources slide 3
Placement
Training
Introduction to C++
Identifiers:- Identifiers are the fundamental building
blocks of program.
And used for the names given to variable, function , data
definition.
Rules of Naming Identifier:-
Identifiers can have alphabet , digits , underscore sign.
Ex:- int stud_Id;
They must not be a keyword or Boolean.
Ex:- int if; // Not allowed
They can be of any length.
23. Human resources slide 3
Placement
Training
Introduction to C++
Rules of Naming Identifier:-
They must not begin with a digit.
Ex:- int Num1; // Allowed
Special characters not allowed.
Ex1:- int emp_Id; // Allowed
Ex2:- int emp#Id; // NotAllowed
C++ is a case sensitive language.(Upper,lower,camelcase)
Ex1: int Num; // All are different
Ex2:- int num;
Ex3:- int NUM;
24. Human resources slide 3
Placement
Training
Introduction to C++
Constants:- Constants are the identifiers whose value can
not be changed during program execution.
Constants are used to represent fixed values.
Also, known as Literals.
const keyword is used to make a constant value.
Syntax:- const int Pie=3.1412;
25. Human resources slide 3
Placement
Training
Introduction to C++
Variable:-
Variable is a name given to memory location allocated by the
compiler to store different values .
Variables are used in c++ , where we need storage for any
value, which will change in program.
Example:- int empId = 101; // Declared and initialized
variable of integer type.
RAM
Memory location named empID
101
28. Human resources slide 3
Different Programming Paradigm
Programming:-Programming is giving instructions to the
computer in a language that it understands.
Instructions:- It is a combination of some words that must be
given in a proper sequence to get desired output.
Placement
Training
30. Human resources slide 4
Placement
Training
Introduction to C++
What is C++ ?
History of C++?
Why C++?
Benefits of C++?
C++ over C
31. Human resources slide 4
Placement
Training
Introduction to C++
What is C++?
• C++ is a powerful , efficient and general-purpose
programming language based on C.
• It was developed by “Bjarne Stroustrup” in 1979.
• Many of today from GUI applications to 3D graphics for
games to real-time mathematical simulations.
• Software development in C and C++ continues to offer
benefits for enterprises around the world, based on their
business objectives.
• Learning C++ is a wise investment for all
programmers.
33. Human resources slide 4
Placement
Training
Introduction to C++
History of C++
• The origin of C++ to 1979 by “ Mr. Bjarne Stroustrup ” who
was an employee of Bell AT and T Laboratory started working
on language “C with Classes”.
• In 1983 it was named to C++.
• During 1989- 1990 , a joint ANSI and ISO
committee standardize C++ language.
• “Alexander Stephanou” added extra features like
STL, Template, RTTI, etc.
• Later, C++11, C++14 ,C++17,C++23 versions of C++ was
released with additional features.
34. Human resources slide 4
Placement
Training
Introduction to C++
Why C++?
Following reasons why you should learn C++.....
• C++ is irreplaceable.
• It is sure to expand your knowledge on the Internal
architecture of the computer.
• 60% Stack Overflow Answer rate and active
community.
• C++ job opportunities and salary.
• C++ developers can expect an average of monthly
Rs.30000.
• Salary with over 7,700 jobs advertised every month.
35. Human resources slide 4
Placement
Training
Introduction to C++
Benefits of C++?
• C++ is highly Portable, efficient , powerful and fast
P.L.
• It is used for multi device, multi platform app
development.
• C++ is an Object Oriented P.L. include concepts like
Class, Objects, Abstraction, Encapsulation, Inheritance,
Polymorphism , etc.
36. Human resources slide 4
Placement
Training
Introduction to C++
Benefits of C++?
• OOP concepts allowed code reusability and makes
program very maintainable.
• C++ has a very big job market as it is used in various
industries like:- Finance, Games, App development , etc.
• It has rich function library.
• Modularity.
41. Human resources slide 4
Placement
Training
Introduction to C++
• Object Oriented Programming is a programming
style based on the concept of objects which are data
structures that contain Data and Code.
• Data:- In the form of field known as Attributes.
• Ex:- student object
• Attributes are:- studID , studName , studBranch , etc.
• Code in the form of procedures known as
Methods/Functions.
• Ex:- studSpMarks(), studSubMarks().
43. Human resources slide 4
Placement
Training
Introduction to C++
Objects:-
Everything in this world is an object from a Nail to
Hammer and Liver to Rocket.
• From human perspective an object is a tangible
thing that exist in time and space.
• An object represents an individual item , unit or
entity with a well-defined structure and behavior.
• Ex:- A car, a person, a training course , bank
account , etc.
44. Human resources slide 4
Placement
Training
Introduction to C++
Characteristics of an Object:-
1) State. 2) Behavior. 3) Identity. 4) Responsibility.
1. State: It represents the current value of an
attribute.
Types of state:- i)Static state .(ii)Dynamic state.
i) Static state:- Like- Id, Name , Gender , etc.
ii) Dynamic state:- Like- Age, Mobile number.
45. Human resources slide 4
Placement
Training
Introduction to C++
Characteristics of an Object:-
2) Behavior:- How an object act or react.
Ex:-A Faculty object : behavior is taking lectures.
3) Identity:- It distinguish one object to another.
It provide uniqueness.
Ex:- Emp_Id = 101 , Stud_Id = s01, etc.
4) Responsibility:- The role it serves.
Ex:- A Faculty object : To do allotted work(4Lectures).
46. Human resources slide 4
Placement
Training
Introduction to C++
Class:-
Why class?
• The main purpose of C++ programming is to add object
orientation to the C programming language .
• Classes are the central feature of C++ that supports
object-oriented programming and are often called
user-defined types.
What is class?
1). Class can be compared to a template or Mold out
of which similar kind of objects are created.
47. Human resources slide 4
Placement
Training
Introduction to C++
Class:-
2)A class is like a blueprint for an object.
3)It combines data representation and
methods/Functions for manipulating that data
into one neat package.
4)The data(variables) and functions (methods) within
a class are called members of the class.
48. Human resources slide 4
Placement
Training
Introduction to C++
Class:-
2)A class is a user defined data type that
has data and functions as a member.
Example: Student class and studId , studName
are the data members .
Studmarks(), studResult(), are the member
functions(properties) .[s1,s2,s3 are the same
kind of objects]
50. Human resources slide 4
Placement
Training
Introduction to C++
OOP Concepts:-
1)Abstraction. 2) Encapsulation.
3) Inheritance. 4) Polymorphism.
1) Abstraction:- Data abstraction is one of the most
essential and important feature of object-
oriented programming.
• Data abstraction refers to providing only essential
information about the data to the outside world,
hiding the background details or implementation.
51. Human resources slide 4
Placement
Training
Introduction to C++
OOP Concepts:-
Abstraction : (Ex:- Car, Coffee machine, ATM , etc.)
Real life ex:- Consider a real-life example of a man
driving a car.
The man only knows that pressing the accelerators will
increase the speed of car or applying brakes will stop
the car .
But he does not know about how on pressing
accelerator the speed is increasing,
He does not know about the inner mechanism of the
car or the implementation of accelerator, brakes etc. in
the car. This is what abstraction is.
52. Human resources slide 4
Placement
Training
Introduction to C++
OOP Concepts:-
• Abstraction in programming: We can implement
Abstraction in programming using classes.
• Class helps us to group data members and
member functions using available
access specifiers .
• A Class can decide which data member will be visible
to outside world and which is not.
53. Human resources slide 4
Placement
Training
Introduction to C++
OOP Concepts:-
• Advantages of Data Abstraction:
• Helps the user to avoid writing the low-level code
• Avoids code duplication and increases reusability.
• Can change internal implementation of class
independently without affecting the user.
• Helps to increase security of an application or program as
only important details are provided to the user.
54. Human resources slide 4
Placement
Training
Introduction to C++
OOP Concepts:-
2) Encapsulation:- In OOP, Encapsulation is defined as the
data and the functions binding together in a single unit
(class) that manipulates them.
Real life Ex: In a company there are different sections
like the accounts section, finance section, sales section
etc.
The finance section handles all the financial transactions
and keep records of all the data related to finance ,
similarly other department handles.
55. Human resources slide 4
Placement
Training
Introduction to C++
OOP Concepts:-
2) Encapsulation Real life Ex:-
• Now there may arise a situation when for some reason
an official from finance section needs all the data about
sales in a particular month.
• In this case, he is not allowed to directly access the data
of sales section. He will first have to contact some other
officer in the sales section and then request him to give
the data.
• This is what encapsulation is. Here the data of sales
section and the employees that can manipulate them
are wrapped under a single name “sales section”.
57. Human resources slide 4
Placement
Training
Introduction to C++
Encapsulation Program:- Thus, we can say that here, the
variable x and the functions get() and set() are bind
together which is nothing but encapsulation.
• As using encapsulation also hides the data. In the above
example the data of any of the section like sales, finance
or accounts is hidden from any other section.
• In C++ encapsulation can be implemented using Class
and access modifiers.
58. Human resources slide 4
Placement
Training
Introduction to C++
OOP Concepts:-
3) Inheritance:- Inheritance is one of the most important
feature of Object-Oriented Programming.
The capability of a class to derive properties and
characteristics from another class is
called Inheritance.
Sub Class: The class that inherits properties from another
class is called Sub class or Derived Class.
Super Class: The class whose properties are inherited by
sub class is called Base Class or Super class.
59. Human resources slide 4
Placement
Training
Introduction to C++
OOP Concepts:-
• 3) Inheritance Examples[Vehicles, shapes,
fruits,Employee]
• Consider a group of vehicles.
• You need to create classes for Bus, Car and
Truck.
• The methods fuelAmount(), capacity(), applyBrakes()
will be same for all the three classes.
• If we create these classes avoiding inheritance, then we
must write all these functions in each of the three classes
as shown in below figure:
60. Human resources slide 4
Placement
Training
Introduction to C++
Example of Inheritance
61. Human resources slide 4
Placement
Training
Introduction to C++
Example of Inheritance
62. Human resources slide 4
Placement
Training
Introduction to C++
OOP Concepts:-
4)Polymorphism:-It is one of the most imp feature of OOP.
The word Polymorphism means Many forms.
Polymorphism = Poly + Morph
(Many)
(Forms)
Definition: We can define polymorphism, as the ability of
a message to be displayed in more than one form.
Example:- Consider an electricity, passed to different
objects like Light , Fan , Laptop, Desktop , etc.
63. Human resources slide 4
Placement
Training
Introduction to C++
Polymorphism Example: A Boy
64. Human resources slide 4
Placement
Training
Introduction to C++
Polymorphism Example: Shape class
65. Human resources slide 4
Placement
Training
Introduction to C++
Types of Polymorphism
66. Human resources slide 4
Placement
Training
Introduction to C++
OOP Concepts:-
4)Polymorphism:-
Advantages of Polymorphism:
1) Generic code
2) Extensibility
3) Code sharing
4) Reduction in complexity
Disadvantages of Polymorphism:
1) Just by looking at the source code, it is not possible to
determine which code is executed.