SlideShare a Scribd company logo
VIth SEMESTER
OCSE-306T
C++
Subject Teacher: Dr. Ashutosh Gupta,
Department of Instrumentation & Control Engineering, BVCOE New Delhi
Content
• UNIT 1:
• Review of C, Difference between C and C++, Procedure Oriented and Object Oriented Approach.
• Basic Concepts: Objects, classes, Principals like Abstraction, Encapsulation, Inheritance and Polymorphism.
• Dynamic Binding, Message Passing. Characteristics of Object-Oriented Languages.
• Abstract data types, Object & classes, attributes, methods, C++ class declaration, Local Class and Global Class, State
identity and behaviour of an object, Local Object and Global Object, Scope resolution operator, Friend Functions, Inline
functions,
• Constructors and destructors, instantiation of objects, Types of Constructors, Static Class Data, Array of Objects,
Constant member functions and Objects, Memory management Operators.
• UNIT 2:
• Inheritance, Types of Inheritance, access modes public, private & protected, Abstract Classes, Ambiguity resolution
using scope resolution operator and Virtual base class, Aggregation, composition vs classification hierarchies,
Overriding inheritance methods, Constructors in derived classes, Nesting of Classes.
• UNIT 3:
• Polymorphism, Type of Polymorphism Compile time and runtime, Function Overloading, Operator Overloading (Unary
and Binary) Polymorphism by parameter, Pointer to objects, this pointer, Virtual Functions, pure virtual functions.
• UNIT 4:
• Manipulating strings, Streams and files handling, formatted and Unformatted Input output. Exception handling,
Generic Programming function template, class Template Standard Template Library: Standard Template Library,
Overview of Standard Template Library, Containers, Algorithms, Iterators, Other STL Elements, The Container Classes,
General Theory of Operation, Vectors. ASHUTOSH
Dept. of Electrical Engineering
UNIT 1:
• Review of C, Difference between C and C++, Procedure Oriented and Object Oriented Approach.
• Basic Concepts: Objects, classes, Principals like Abstraction, Encapsulation, Inheritance and Polymorphism.
• Dynamic Binding, Message Passing. Characteristics of Object-Oriented Languages.
• Abstract data types, Object & classes, attributes, methods, C++ class declaration, Local Class and Global Class, State
identity and behaviour of an object, Local Object and Global Object, Scope resolution operator, Friend Functions, Inline
functions,
• Constructors and destructors, instantiation of objects, Types of Constructors, Static Class Data, Array of Objects,
Constant member functions and Objects, Memory management Operators.
ASHUTOSH
Dept. of Electrical Engineering
Difference between C and C++
C C++
C was developed by Dennis Ritchie between the year 1969
and 1973 at AT&T Bell Labs.
C++ was developed by Bjarne Stroustrup in 1979.
C does no support polymorphism, encapsulation, and
inheritance which means that C does not support object
oriented programming.
C++ supports polymorphism, encapsulation, and
inheritance because it is an object oriented programming
language.
C is (mostly) a subset of C++. C++ is (mostly) a superset of C.
For the development of code, C supports procedural
programming.
C++ is known as hybrid language because C++ supports
both procedural and object oriented programming
paradigms.
Data and functions are separated in C because it is a
procedural programming language.
Data and functions are encapsulated together in form of an
object in C++.
Built-in data types is supported in C. Built-in & user-defined data types is supported in C++.
C is a function driven language. C++ is an object driven language.
scanf() and printf() functions are used for input/output in C. cin and cout are used for input/output in C++.
Virtual and friend functions are not supported by C. Virtual and friend functions are supported by C++.
S.no. On the basis of Procedural Programming Object-oriented programming
1. Definition It is a programming language that is derived from structure programming and
based upon the concept of calling procedures. It follows a step-by-step
approach in order to break down a task into a set of variables and routines via
a sequence of instructions.
Object-oriented programming is a computer programming design philosophy or
methodology that organizes/ models software design around data or objects rather than
functions and logic.
2. Security It is less secure than OOPs. Data hiding is possible in object-oriented programming due to abstraction. So, it is more
secure than procedural programming.
3. Approach It follows a top-down approach. It follows a bottom-up approach.
4. Data movement In procedural programming, data moves freely within the system from one
function to another.
In OOP, objects can move and communicate with each other via member functions.
5. Orientation It is structure/procedure-oriented. It is object-oriented.
6. Access modifiers There are no access modifiers in procedural programming. The access modifiers in OOP are named as private, public, and protected.
7. Inheritance Procedural programming does not have the concept of inheritance. There is a feature of inheritance in object-oriented programming.
8. Code reusability There is no code reusability present in procedural programming. It offers code reusability by using the feature of inheritance.
9. Overloading Overloading is not possible in procedural programming. In OOP, there is a concept of function overloading and operator overloading.
10. Importance It gives importance to functions over data. It gives importance to data over functions.
11. Virtual class In procedural programming, there are no virtual classes. In OOP, there is an appearance of virtual classes in inheritance.
12. Complex problems It is not appropriate for complex problems. It is appropriate for complex problems.
13. Data hiding There is not any proper way for data hiding. There is a possibility of data hiding.
14. Program division In Procedural programming, a program is divided into small programs that are
referred to as functions.
In OOP, a program is divided into small parts that are referred to as objects.
15. Examples Examples of Procedural programming include C, Fortran, Pascal, and VB. The examples of object-oriented programming are -
.NET, C#, Python, Java, VB.NET, and C++.
What is Syntax?
Syntax refers to the rules and
regulations for writing statements in
a programming language. They can
also be viewed as the grammatical
rules defining the structure of a
programming language.
The C++ language also has its syntax
for the functionalities it provides.
Different statements have different
syntax specifying their usage but C++
programs also have basic syntax
rules that are followed throughout
all the programs.
Basic Syntax of C++ Programming
1. Header File
• The header files contain the definition of the functions and macros we are
using in our program. They are defined on the top of the C++ program.
2. Namespace
• A namespace in C++ is used to provide a scope or a region where we define
identifiers. It is used to avoid name conflicts between two identifiers as only
unique names can be used as identifiers.
3. Main Function
• Functions are basic building blocks of a C++ program that contains the
instructions for performing some specific task. Apart from the instructions
present in its body, a function definition also contains information about its
return type and parameters.
4. Blocks
• Blocks are the group of statements that are enclosed within { } braces. They
define the scope of the identifiers and are generally used to enclose the
body of functions and control statements.
5. Semicolons
• As you may have noticed by now, each statement in the above code is
followed by a ( ; ) semicolon symbol. It is used to terminate each line of the
statement of the program. When the compiler sees this semicolon, it
terminates the operation of that line and moves to the next line.
6. Identifiers
• We use identifiers for the naming of variables, functions, and other user-
defined data types. An identifier may consist of uppercase and lowercase
alphabetical characters, underscore, and digits. The first letter must be an
underscore or an alphabet.
7. Keywords
• In the C++ programming language, there are some reserved words that are
used for some special meaning in the C++ program. It can’t be used for
identifiers.
// C++ program to demonstrate the basic
syntax
// Header File Library
#include <iostream>
// Standard Namespace
using namespace std;
// Main Function
int main()
{
// Body of the Function
// Declaration of Variable
int num1 = 24;
int num2 = 34;
int result = num1 + num2;
// Output
cout << result << endl;
// Return Statement
return 0;
}
Object-Oriented Programming in C++
1. Class
• A class is a template of an object.
2. Data Members & Member Functions
• The attributes or data in the class are
defined by the data members & the
functions that work on these data
members are called the member
functions.
3. Object
• The object is an instance of a class. The
class itself is just a template that is not
allocated any memory. To use the data and
methods defined in the class, we have to
create an object of that class.
Keywords and Identifiers
Keywords Identifiers
Keywords are predefined/reserved words
identifiers are the values used to define different
programming items like a variable, integers,
structures, and unions.
Keywords always start in lowercase
identifiers can start with an uppercase letter as well as
a lowercase letter.
It defines the type of entity. It classifies the name of the entity.
A keyword contains only alphabetical characters,
an identifier can consist of alphabetical characters,
digits, and underscores.
It should be lowercase. It can be both upper and lowercase.
No special symbols or punctuations are used in
keywords and identifiers.
No special symbols or punctuations are used in
keywords and identifiers. The only underscore can be
used in an identifier.
Keywords: int, char, while, do. Identifiers: a_a, aa, aa1.
Variables
Variables in C++ is a name given to a memory
location. It is the basic unit of storage in a
program.
• The value stored in a variable can be changed
during program execution.
• A variable is only a name given to a memory
location, all the operations done on the
variable effects that memory location.
• In C++, all the variables must be declared
before use.
• In general, the scope is defined as the extent
up to which something can be worked with. In
programming also the scope of a variable is
defined as the extent of the program code
within which the variable can be accessed or
declared or worked with. There are mainly two
types of variable scopes:
1. Local Variables
2. Global Variables
C++ Programming with examples for B.Tech
C++ Programming with examples for B.Tech
C++ Programming with examples for B.Tech
S.No. Loop Type and Description
1.
while loop– First checks the
condition, then executes the body.
2.
for loop– firstly initializes, then,
condition check, execute body,
update.
3.
do-while
loop– firstly, execute the body then
condition check
C++ Loops
Functions
• A function is a set of statements that takes input, does some specific computation, and
produces output. The idea is to put some commonly or repeatedly done tasks together to make
a function so that instead of writing the same code again and again for different inputs, we can
call this function.
• In simple terms, a function is a block of code that runs only when it is called.
OOPs
• There are some basic concepts
that act as the building blocks of
OOPs i.e.
• Class
• Objects
• Encapsulation
• Abstraction
• Polymorphism
• Inheritance
• Dynamic Binding
• Message Passing
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.
Example: Consider the Class of Cars. There may be many
cars with different names and brands but all of them will
share some common properties like all of them will have
4 wheels, Speed Limit, Mileage range, etc. So here, the
Car is the class, and wheels, speed limits, and mileage are
their properties.
Object
• An Object is an identifiable entity with
some characteristics and behavior.
• 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.
• Each object contains data and code to
manipulate the data.
• Objects can interact without having to
know details of each other’s data or code,
it is sufficient to know the type of
message accepted and the type of
response returned by the objects.
// C++ Program to show the syntax/working of Objects as a
// part of Object Oriented PProgramming
#include <iostream>
using namespace std;
class person {
char name[20];
int id;
public:
void getdetails() {}
};
int main()
{
person p1; // p1 is a object
return 0;
}
Encapsulation
• Encapsulation is defined as wrapping
up data and information under a
single unit.---CLASS
• In Object-Oriented Programming,
Encapsulation is defined as binding
together the data and the functions
that manipulate them.
• Encapsulation also leads to data
abstraction or data hiding. Using
encapsulation also hides the data.
• Data is only accessible from the
class.
#include <iostream>
using namespace std;
class Circle
{
int r;
public:
void set(int x)
{
r=x;
}
int get()
{
return r;
}
};
int main()
{
Circle q1;
q1.set(3);
cout<<q1.get()<<endl;
return 0;
}
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.
• Ex. Pow(x,y)
Abstraction using Classes: We can implement
Abstraction in C++ using classes. The 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 the outside world and
which is not.
Abstraction in Header files: One more type of
abstraction in C++ can be header files. For example,
consider the pow() method present in math.h header
file. Whenever we need to calculate the power of a
number, we simply call the function pow() present in
the math.h header file
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.
• Super Class: The class whose properties are inherited
by a sub-class is called Base Class or Superclass.
• 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.
Inheritance
• Class Car
• Tires
• Capacity
• Class Truck
• Tires
• Capacity
• Class Bus
• Tires
• Capacity
• Reusabilty
Inheritance
• Class Car • Class Truck • Class Bus
• Class Vehicle
• Tires
• Capacity
Access Specifiers & Mode of Inheritance
• Public
• Private
• Protected
Access Specifiers & Mode of Inheritance
1. Public: All the class members declared under
the public specifier will be available to everyone.
The data members and member functions
declared as public can be accessed by other
classes and functions too. The public members of
a class can be accessed from anywhere in the
program using the direct member access operator
(.) with the object of that class.
// C++ program to demonstrate public
// access modifier
#include<iostream>
using namespace std;
// class definition
class Circle
{
public:
double radius;
double compute_area()
{
return 3.14*radius*radius;
}
};
// main function
int main()
{
Circle obj;
// accessing public datamember outside
class
obj.radius = 5.5;
cout << "Radius is: " << obj.radius <<
"n";
cout << "Area is: " << obj.compute_area();
return 0;
}
Access Specifiers & Mode of Inheritance
2. Private: The class members declared as private can be accessed only by the member functions inside the class. They are not allowed to be accessed directly by
any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of the class.
// C++ program to demonstrate private
// access modifier
#include<iostream>
using namespace std;
class Circle
{
// private data member
private:
double radius;
// public member function
public:
double compute_area()
{ // member function can access private
// data member radius
return 3.14*radius*radius;
}
};
// main function
int main()
{
// creating object of the class
Circle obj;
// trying to access private data member
// directly outside the class
obj.radius = 1.5;
cout << "Area is:" << obj.compute_area();
return 0;
}
In function 'int main()’:
11:16: error: 'double Circle::radius' is
private double radius;
31:9: error: within this context obj.radius
= 1.5;
// C++ program to demonstrate private
// access modifier
#include<iostream>
using namespace std;
class Circle
{
// private data member
private:
double radius;
// public member function
public:
void compute_area(double r)
{ // member function can access private
// data member radius
radius = r;
double area = 3.14*radius*radius;
cout << "Radius is: " << radius << endl;
cout << "Area is: " << area;
}
};
// main function
int main()
{
// creating object of the class
Circle obj;
// trying to access private data member
// directly outside the class
obj.compute_area(1.5);
return 0;
}
Radius is: 1.5
Area is: 7.065
We are not allowed to access the
private data members of a class
directly from outside the class. Yet
an access to obj.radius is
attempted, but radius being a
private data member, we obtained
the above compilation error.
However, we can access the
private data members of a class
indirectly using the public
member functions of the class.
Access Specifiers & Mode of Inheritance
3. Protected: The protected access modifier is similar to the private access
modifier in the sense that it can’t be accessed outside of its class unless
with the help of a friend class. The difference is that the class members
declared as Protected can be accessed by any subclass (derived class) of
that class as well.
• Note: This access through inheritance can alter the access modifier of
the elements of base class in derived class depending on the mode of
Inheritance.
// C++ program to demonstrate
// protected access modifier
#include <bits/stdc++.h>
using namespace std;
// base class
class Parent
{
// protected data members
protected:
int id_protected;
};
// sub class or derived class from public base class
class Child : public Parent
{
public:
void setId(int id)
{
// Child class is able to access the inherited
// protected data members of base class
id_protected = id;
}
void displayId()
{
cout << "id_protected is: " << id_protected << endl;
}
};
// main function
int main() {
Child obj1;
// member function of the derived class can
// access the protected data members of the base
class
obj1.setId(81);
obj1.displayId();
return 0;
}
id_protected is: 81
Types of Inheritance
Class A
Class B
Single Inheritance
Class A
Class B
Multi-level Inheritance
Class C
Class A Class B
Class C
Multiple Inheritance
Class A
Class B Class C
Hierarchical Inheritance
Types of Inheritance
Class D
Class E
Class A
Class B Class C
Hybrid
Inheritance
Class F Class G
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 characteristics. A man at the
same time is a father, a husband, and an
employee. So the same person possesses
different behavior in different situations.
This is called polymorphism.
The behavior depends upon the types of data used in
the operation. C++ supports operator overloading and
function overloading.
• Operator Overloading: The process of making an
operator exhibit different behaviors in different
instances is known as operator overloading.
• Function Overloading: Function overloading is
using a single function name to perform different
types of tasks. Polymorphism is extensively used in
implementing inheritance.
Polymorphism
• Compile-Time Polymorphism
• This type of polymorphism is achieved
by function overloading or operator
overloading.
• Runtime Polymorphism
• This type of polymorphism is achieved
by Function Overriding. Late binding and
dynamic polymorphism are other names
for runtime polymorphism. The function
call is resolved at runtime in runtime
polymorphism. In contrast, with compile
time polymorphism, the compiler
determines which function call to bind
to the object after deducing it at
runtime.
Polymorphism
• Function Overloading
• When there are multiple functions
with the same name but different
parameters, then the functions are
said to be overloaded, hence this is
known as Function Overloading.
Functions can be overloaded by
changing the number of arguments
or/and changing the type of
arguments. In simple terms, it is a
feature of object-oriented
programming providing many
functions that have the same name
but distinct parameters when
numerous tasks are listed under one
function name. There are certain
Rules of Function Overloading that
should be followed while
overloading a function.
// function overloading or
// Compile-time Polymorphism
#include <iostream>
using namespace std;
class ABC {
public:
// Function with 1 int parameter
void func(int x)
{
cout << "value of x is " << x << endl;
}
// Function with same name but
// 1 double parameter
void func(double x)
{
cout << "value of x is " << x << endl;
}
// Function with same name and
// 2 int parameters
void func(int x, int y)
{
cout << "value of x and y is " << x << ", " << y
<< endl;
}
};
}
// Driver code
int main()
{
ABC obj1;
// Function being called depends
// on the parameters passed
// func() is called with int value
obj1.func(7);
// func() is called with double value
obj1.func(9.132);
// func() is called with 2 int values
obj1.func(85, 64);
return 0;
}
Output
value of x is 7
value of x is 9.132
value of x and y is 85, 64
Polymorphism
• Operator Overloading
• C++ has the ability to provide
the operators with a special
meaning for a data type, this
ability is known as operator
overloading. For example, we
can make use of the addition
operator (+) for string class to
concatenate two strings. We
know that the task of this
operator is to add two
operands. So a single operator
‘+’, when placed between
integer operands, adds them
and when placed between
string operands, concatenates
them.
// Operator Overloading or
// Compile-Time Polymorphism
#include <iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
// This is automatically called
// when '+' is used with between
// two Complex objects
Complex operator+(Complex const& obj)
{
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() {
cout << real << " + i" << imag << endl;
}
};
// Driver code
int main()
{
Complex c1(10, 5), c2(2, 4);
// An example call to "operator+"
Complex c3 = c1 + c2;
c3.print();
}
Output12 + i9
Dynamic Binding
• In dynamic binding, the code to be executed in
response to the function call is decided at
runtime. C++ has virtual functions to support
this. Because dynamic binding is flexible, it
avoids the drawbacks of static binding, which
connected the function call and definition at
build time.
// C++ Program to Demonstrate the Concept of Dynamic binding
// with the help of virtual function
#include <iostream>
using namespace std;
class ABC {
public:
void call_Function() // function that call print
{
print();
}
void print() // the display function
{
cout << "Printing the Base class Content" << endl;
}
};
class ABC2 : public ABC // ABC2 inherit a publicly
{
public:
void print() // ABC2's display
{
cout << "Printing the Derived class Content"
<< endl;
}
};
int main()
{
ABC A1; // Creating ABC's object
A1.call_Function(); // Calling call_Function
ABC2 A2; // creating ABC2 object
A2.call_Function(); // calling call_Function
// for ABC2 object
return 0;
}
Output
Printing the Base class Content
Printing the Base class Content
Message Passing
• Objects communicate with one another by sending and receiving
information. A message for an object is a request for the execution of a
procedure and therefore will invoke a function in the receiving object
that generates the desired results. Message passing involves specifying
the name of the object, the name of the function, and the information to
be sent.
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.
• Non member function which can access private members of class.

More Related Content

PPTX
Under water welding PPT
PPTX
Overview of friction stir welding
PPTX
Under water welding
PPTX
gas welding
PPTX
concept of Actuators
PDF
Metal Joining Processes
PPTX
electric resistance welding
PPTX
Presentation on Automation in Welding
Under water welding PPT
Overview of friction stir welding
Under water welding
gas welding
concept of Actuators
Metal Joining Processes
electric resistance welding
Presentation on Automation in Welding

What's hot (20)

PPTX
Welding Processes
PDF
Welding.pdf .
PPT
Brazing
PPTX
UNDER WATER WELDING
PPTX
Underwater welding
PPTX
plasma arc and laser beam machining
PPTX
Advance Welding Process
PPTX
Friction stir-welding
PPTX
Visual inspection and optical aids for visual inspection application
PPTX
SpinArc® Welding Process Technology - Overview
PPTX
Friction welding
PPTX
UNDERWATER WELDING
PPTX
UNDER WATER WELDING
DOCX
THREAD CUTTING AND KNURLING ON LATHE
PPTX
Underwater welding
PPTX
Gas welding
PPT
DIFFERENT TOOLS IN CNC MACHINES
PDF
Two marks welding
PDF
Bearing Handling & Maintenance
Welding Processes
Welding.pdf .
Brazing
UNDER WATER WELDING
Underwater welding
plasma arc and laser beam machining
Advance Welding Process
Friction stir-welding
Visual inspection and optical aids for visual inspection application
SpinArc® Welding Process Technology - Overview
Friction welding
UNDERWATER WELDING
UNDER WATER WELDING
THREAD CUTTING AND KNURLING ON LATHE
Underwater welding
Gas welding
DIFFERENT TOOLS IN CNC MACHINES
Two marks welding
Bearing Handling & Maintenance
Ad

Similar to C++ Programming with examples for B.Tech (20)

PPTX
OOP CHAPTER object oreinted programming using c++
PDF
OOPS_Unit_1
PPTX
Unit - I Intro. to OOP Concepts and Control Structure -OOP and CG (2024 Patte...
PPTX
c++.pptxwjwjsijsnsksomammaoansnksooskskk
PPT
Unit 1- Basic concept of object-oriented-programming.ppt
PDF
UNIT1- OBJECT ORIENTED PROGRAMMING IN JAVA- AIML IT-SPPU
PDF
1 puc programming using c++
PDF
Object Oriented Programming Lecture Notes
PDF
Object Oriented Programming With C 2140705 Darshan All Unit Darshan Institute...
PPT
Share Unit 1- Basic concept of object-oriented-programming.ppt
PPTX
Object oriented programming. (1).pptx
PPTX
OOP-1.pptx
PPTX
2.3 Classes, Inheritance, Virtual Base Classes.pptx
DOC
Question bank unit i
PPTX
SE-IT JAVA LAB OOP CONCEPT
PPTX
object oriented programming language in c++
PPT
PDF
C++ [ principles of object oriented programming ]
PPT
Bca 2nd sem u-1 iintroduction
PPT
Mca 2 sem u-1 iintroduction
OOP CHAPTER object oreinted programming using c++
OOPS_Unit_1
Unit - I Intro. to OOP Concepts and Control Structure -OOP and CG (2024 Patte...
c++.pptxwjwjsijsnsksomammaoansnksooskskk
Unit 1- Basic concept of object-oriented-programming.ppt
UNIT1- OBJECT ORIENTED PROGRAMMING IN JAVA- AIML IT-SPPU
1 puc programming using c++
Object Oriented Programming Lecture Notes
Object Oriented Programming With C 2140705 Darshan All Unit Darshan Institute...
Share Unit 1- Basic concept of object-oriented-programming.ppt
Object oriented programming. (1).pptx
OOP-1.pptx
2.3 Classes, Inheritance, Virtual Base Classes.pptx
Question bank unit i
SE-IT JAVA LAB OOP CONCEPT
object oriented programming language in c++
C++ [ principles of object oriented programming ]
Bca 2nd sem u-1 iintroduction
Mca 2 sem u-1 iintroduction
Ad

More from ashutoshgupta1102 (6)

PPT
C++ PROGRAMMING OBJECT ORIENTED PROGRAMMING
PPTX
DC DC Converter.pptx
PPTX
POWER ELECTRONICS
PDF
Firing Circuit
PPTX
Plcc and satelite communcation
PPTX
EMERGENCY RESPONSE PLAN
C++ PROGRAMMING OBJECT ORIENTED PROGRAMMING
DC DC Converter.pptx
POWER ELECTRONICS
Firing Circuit
Plcc and satelite communcation
EMERGENCY RESPONSE PLAN

Recently uploaded (20)

PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
additive manufacturing of ss316l using mig welding
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
DOCX
573137875-Attendance-Management-System-original
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPT
Project quality management in manufacturing
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Geodesy 1.pptx...............................................
PDF
Well-logging-methods_new................
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Sustainable Sites - Green Building Construction
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPT
Mechanical Engineering MATERIALS Selection
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
additive manufacturing of ss316l using mig welding
Operating System & Kernel Study Guide-1 - converted.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
573137875-Attendance-Management-System-original
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Project quality management in manufacturing
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Lesson 3_Tessellation.pptx finite Mathematics
Geodesy 1.pptx...............................................
Well-logging-methods_new................
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Sustainable Sites - Green Building Construction
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Mechanical Engineering MATERIALS Selection
Model Code of Practice - Construction Work - 21102022 .pdf

C++ Programming with examples for B.Tech

  • 1. VIth SEMESTER OCSE-306T C++ Subject Teacher: Dr. Ashutosh Gupta, Department of Instrumentation & Control Engineering, BVCOE New Delhi
  • 2. Content • UNIT 1: • Review of C, Difference between C and C++, Procedure Oriented and Object Oriented Approach. • Basic Concepts: Objects, classes, Principals like Abstraction, Encapsulation, Inheritance and Polymorphism. • Dynamic Binding, Message Passing. Characteristics of Object-Oriented Languages. • Abstract data types, Object & classes, attributes, methods, C++ class declaration, Local Class and Global Class, State identity and behaviour of an object, Local Object and Global Object, Scope resolution operator, Friend Functions, Inline functions, • Constructors and destructors, instantiation of objects, Types of Constructors, Static Class Data, Array of Objects, Constant member functions and Objects, Memory management Operators. • UNIT 2: • Inheritance, Types of Inheritance, access modes public, private & protected, Abstract Classes, Ambiguity resolution using scope resolution operator and Virtual base class, Aggregation, composition vs classification hierarchies, Overriding inheritance methods, Constructors in derived classes, Nesting of Classes. • UNIT 3: • Polymorphism, Type of Polymorphism Compile time and runtime, Function Overloading, Operator Overloading (Unary and Binary) Polymorphism by parameter, Pointer to objects, this pointer, Virtual Functions, pure virtual functions. • UNIT 4: • Manipulating strings, Streams and files handling, formatted and Unformatted Input output. Exception handling, Generic Programming function template, class Template Standard Template Library: Standard Template Library, Overview of Standard Template Library, Containers, Algorithms, Iterators, Other STL Elements, The Container Classes, General Theory of Operation, Vectors. ASHUTOSH Dept. of Electrical Engineering
  • 3. UNIT 1: • Review of C, Difference between C and C++, Procedure Oriented and Object Oriented Approach. • Basic Concepts: Objects, classes, Principals like Abstraction, Encapsulation, Inheritance and Polymorphism. • Dynamic Binding, Message Passing. Characteristics of Object-Oriented Languages. • Abstract data types, Object & classes, attributes, methods, C++ class declaration, Local Class and Global Class, State identity and behaviour of an object, Local Object and Global Object, Scope resolution operator, Friend Functions, Inline functions, • Constructors and destructors, instantiation of objects, Types of Constructors, Static Class Data, Array of Objects, Constant member functions and Objects, Memory management Operators. ASHUTOSH Dept. of Electrical Engineering
  • 4. Difference between C and C++ C C++ C was developed by Dennis Ritchie between the year 1969 and 1973 at AT&T Bell Labs. C++ was developed by Bjarne Stroustrup in 1979. C does no support polymorphism, encapsulation, and inheritance which means that C does not support object oriented programming. C++ supports polymorphism, encapsulation, and inheritance because it is an object oriented programming language. C is (mostly) a subset of C++. C++ is (mostly) a superset of C. For the development of code, C supports procedural programming. C++ is known as hybrid language because C++ supports both procedural and object oriented programming paradigms. Data and functions are separated in C because it is a procedural programming language. Data and functions are encapsulated together in form of an object in C++. Built-in data types is supported in C. Built-in & user-defined data types is supported in C++. C is a function driven language. C++ is an object driven language. scanf() and printf() functions are used for input/output in C. cin and cout are used for input/output in C++. Virtual and friend functions are not supported by C. Virtual and friend functions are supported by C++.
  • 5. S.no. On the basis of Procedural Programming Object-oriented programming 1. Definition It is a programming language that is derived from structure programming and based upon the concept of calling procedures. It follows a step-by-step approach in order to break down a task into a set of variables and routines via a sequence of instructions. Object-oriented programming is a computer programming design philosophy or methodology that organizes/ models software design around data or objects rather than functions and logic. 2. Security It is less secure than OOPs. Data hiding is possible in object-oriented programming due to abstraction. So, it is more secure than procedural programming. 3. Approach It follows a top-down approach. It follows a bottom-up approach. 4. Data movement In procedural programming, data moves freely within the system from one function to another. In OOP, objects can move and communicate with each other via member functions. 5. Orientation It is structure/procedure-oriented. It is object-oriented. 6. Access modifiers There are no access modifiers in procedural programming. The access modifiers in OOP are named as private, public, and protected. 7. Inheritance Procedural programming does not have the concept of inheritance. There is a feature of inheritance in object-oriented programming. 8. Code reusability There is no code reusability present in procedural programming. It offers code reusability by using the feature of inheritance. 9. Overloading Overloading is not possible in procedural programming. In OOP, there is a concept of function overloading and operator overloading. 10. Importance It gives importance to functions over data. It gives importance to data over functions. 11. Virtual class In procedural programming, there are no virtual classes. In OOP, there is an appearance of virtual classes in inheritance. 12. Complex problems It is not appropriate for complex problems. It is appropriate for complex problems. 13. Data hiding There is not any proper way for data hiding. There is a possibility of data hiding. 14. Program division In Procedural programming, a program is divided into small programs that are referred to as functions. In OOP, a program is divided into small parts that are referred to as objects. 15. Examples Examples of Procedural programming include C, Fortran, Pascal, and VB. The examples of object-oriented programming are - .NET, C#, Python, Java, VB.NET, and C++.
  • 6. What is Syntax? Syntax refers to the rules and regulations for writing statements in a programming language. They can also be viewed as the grammatical rules defining the structure of a programming language. The C++ language also has its syntax for the functionalities it provides. Different statements have different syntax specifying their usage but C++ programs also have basic syntax rules that are followed throughout all the programs.
  • 7. Basic Syntax of C++ Programming 1. Header File • The header files contain the definition of the functions and macros we are using in our program. They are defined on the top of the C++ program. 2. Namespace • A namespace in C++ is used to provide a scope or a region where we define identifiers. It is used to avoid name conflicts between two identifiers as only unique names can be used as identifiers. 3. Main Function • Functions are basic building blocks of a C++ program that contains the instructions for performing some specific task. Apart from the instructions present in its body, a function definition also contains information about its return type and parameters. 4. Blocks • Blocks are the group of statements that are enclosed within { } braces. They define the scope of the identifiers and are generally used to enclose the body of functions and control statements. 5. Semicolons • As you may have noticed by now, each statement in the above code is followed by a ( ; ) semicolon symbol. It is used to terminate each line of the statement of the program. When the compiler sees this semicolon, it terminates the operation of that line and moves to the next line. 6. Identifiers • We use identifiers for the naming of variables, functions, and other user- defined data types. An identifier may consist of uppercase and lowercase alphabetical characters, underscore, and digits. The first letter must be an underscore or an alphabet. 7. Keywords • In the C++ programming language, there are some reserved words that are used for some special meaning in the C++ program. It can’t be used for identifiers. // C++ program to demonstrate the basic syntax // Header File Library #include <iostream> // Standard Namespace using namespace std; // Main Function int main() { // Body of the Function // Declaration of Variable int num1 = 24; int num2 = 34; int result = num1 + num2; // Output cout << result << endl; // Return Statement return 0; }
  • 8. Object-Oriented Programming in C++ 1. Class • A class is a template of an object. 2. Data Members & Member Functions • The attributes or data in the class are defined by the data members & the functions that work on these data members are called the member functions. 3. Object • The object is an instance of a class. The class itself is just a template that is not allocated any memory. To use the data and methods defined in the class, we have to create an object of that class.
  • 9. Keywords and Identifiers Keywords Identifiers Keywords are predefined/reserved words identifiers are the values used to define different programming items like a variable, integers, structures, and unions. Keywords always start in lowercase identifiers can start with an uppercase letter as well as a lowercase letter. It defines the type of entity. It classifies the name of the entity. A keyword contains only alphabetical characters, an identifier can consist of alphabetical characters, digits, and underscores. It should be lowercase. It can be both upper and lowercase. No special symbols or punctuations are used in keywords and identifiers. No special symbols or punctuations are used in keywords and identifiers. The only underscore can be used in an identifier. Keywords: int, char, while, do. Identifiers: a_a, aa, aa1.
  • 10. Variables Variables in C++ is a name given to a memory location. It is the basic unit of storage in a program. • The value stored in a variable can be changed during program execution. • A variable is only a name given to a memory location, all the operations done on the variable effects that memory location. • In C++, all the variables must be declared before use. • In general, the scope is defined as the extent up to which something can be worked with. In programming also the scope of a variable is defined as the extent of the program code within which the variable can be accessed or declared or worked with. There are mainly two types of variable scopes: 1. Local Variables 2. Global Variables
  • 14. S.No. Loop Type and Description 1. while loop– First checks the condition, then executes the body. 2. for loop– firstly initializes, then, condition check, execute body, update. 3. do-while loop– firstly, execute the body then condition check C++ Loops
  • 15. Functions • A function is a set of statements that takes input, does some specific computation, and produces output. The idea is to put some commonly or repeatedly done tasks together to make a function so that instead of writing the same code again and again for different inputs, we can call this function. • In simple terms, a function is a block of code that runs only when it is called.
  • 16. OOPs • There are some basic concepts that act as the building blocks of OOPs i.e. • Class • Objects • Encapsulation • Abstraction • Polymorphism • Inheritance • Dynamic Binding • Message Passing
  • 17. 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. Example: Consider the Class of Cars. There may be many cars with different names and brands but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range, etc. So here, the Car is the class, and wheels, speed limits, and mileage are their properties.
  • 18. Object • An Object is an identifiable entity with some characteristics and behavior. • 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. • Each object contains data and code to manipulate the data. • Objects can interact without having to know details of each other’s data or code, it is sufficient to know the type of message accepted and the type of response returned by the objects. // C++ Program to show the syntax/working of Objects as a // part of Object Oriented PProgramming #include <iostream> using namespace std; class person { char name[20]; int id; public: void getdetails() {} }; int main() { person p1; // p1 is a object return 0; }
  • 19. Encapsulation • Encapsulation is defined as wrapping up data and information under a single unit.---CLASS • In Object-Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulate them. • Encapsulation also leads to data abstraction or data hiding. Using encapsulation also hides the data. • Data is only accessible from the class. #include <iostream> using namespace std; class Circle { int r; public: void set(int x) { r=x; } int get() { return r; } }; int main() { Circle q1; q1.set(3); cout<<q1.get()<<endl; return 0; }
  • 20. 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. • Ex. Pow(x,y) Abstraction using Classes: We can implement Abstraction in C++ using classes. The 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 the outside world and which is not. Abstraction in Header files: One more type of abstraction in C++ can be header files. For example, consider the pow() method present in math.h header file. Whenever we need to calculate the power of a number, we simply call the function pow() present in the math.h header file
  • 21. 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. • Super Class: The class whose properties are inherited by a sub-class is called Base Class or Superclass. • 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.
  • 22. Inheritance • Class Car • Tires • Capacity • Class Truck • Tires • Capacity • Class Bus • Tires • Capacity • Reusabilty
  • 23. Inheritance • Class Car • Class Truck • Class Bus • Class Vehicle • Tires • Capacity
  • 24. Access Specifiers & Mode of Inheritance • Public • Private • Protected
  • 25. Access Specifiers & Mode of Inheritance 1. Public: All the class members declared under the public specifier will be available to everyone. The data members and member functions declared as public can be accessed by other classes and functions too. The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class. // C++ program to demonstrate public // access modifier #include<iostream> using namespace std; // class definition class Circle { public: double radius; double compute_area() { return 3.14*radius*radius; } }; // main function int main() { Circle obj; // accessing public datamember outside class obj.radius = 5.5; cout << "Radius is: " << obj.radius << "n"; cout << "Area is: " << obj.compute_area(); return 0; }
  • 26. Access Specifiers & Mode of Inheritance 2. Private: The class members declared as private can be accessed only by the member functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of the class. // C++ program to demonstrate private // access modifier #include<iostream> using namespace std; class Circle { // private data member private: double radius; // public member function public: double compute_area() { // member function can access private // data member radius return 3.14*radius*radius; } }; // main function int main() { // creating object of the class Circle obj; // trying to access private data member // directly outside the class obj.radius = 1.5; cout << "Area is:" << obj.compute_area(); return 0; } In function 'int main()’: 11:16: error: 'double Circle::radius' is private double radius; 31:9: error: within this context obj.radius = 1.5; // C++ program to demonstrate private // access modifier #include<iostream> using namespace std; class Circle { // private data member private: double radius; // public member function public: void compute_area(double r) { // member function can access private // data member radius radius = r; double area = 3.14*radius*radius; cout << "Radius is: " << radius << endl; cout << "Area is: " << area; } }; // main function int main() { // creating object of the class Circle obj; // trying to access private data member // directly outside the class obj.compute_area(1.5); return 0; } Radius is: 1.5 Area is: 7.065 We are not allowed to access the private data members of a class directly from outside the class. Yet an access to obj.radius is attempted, but radius being a private data member, we obtained the above compilation error. However, we can access the private data members of a class indirectly using the public member functions of the class.
  • 27. Access Specifiers & Mode of Inheritance 3. Protected: The protected access modifier is similar to the private access modifier in the sense that it can’t be accessed outside of its class unless with the help of a friend class. The difference is that the class members declared as Protected can be accessed by any subclass (derived class) of that class as well. • Note: This access through inheritance can alter the access modifier of the elements of base class in derived class depending on the mode of Inheritance. // C++ program to demonstrate // protected access modifier #include <bits/stdc++.h> using namespace std; // base class class Parent { // protected data members protected: int id_protected; }; // sub class or derived class from public base class class Child : public Parent { public: void setId(int id) { // Child class is able to access the inherited // protected data members of base class id_protected = id; } void displayId() { cout << "id_protected is: " << id_protected << endl; } }; // main function int main() { Child obj1; // member function of the derived class can // access the protected data members of the base class obj1.setId(81); obj1.displayId(); return 0; } id_protected is: 81
  • 28. Types of Inheritance Class A Class B Single Inheritance Class A Class B Multi-level Inheritance Class C Class A Class B Class C Multiple Inheritance Class A Class B Class C Hierarchical Inheritance
  • 29. Types of Inheritance Class D Class E Class A Class B Class C Hybrid Inheritance Class F Class G
  • 30. 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 characteristics. A man at the same time is a father, a husband, and an employee. So the same person possesses different behavior in different situations. This is called polymorphism. The behavior depends upon the types of data used in the operation. C++ supports operator overloading and function overloading. • Operator Overloading: The process of making an operator exhibit different behaviors in different instances is known as operator overloading. • Function Overloading: Function overloading is using a single function name to perform different types of tasks. Polymorphism is extensively used in implementing inheritance.
  • 31. Polymorphism • Compile-Time Polymorphism • This type of polymorphism is achieved by function overloading or operator overloading. • Runtime Polymorphism • This type of polymorphism is achieved by Function Overriding. Late binding and dynamic polymorphism are other names for runtime polymorphism. The function call is resolved at runtime in runtime polymorphism. In contrast, with compile time polymorphism, the compiler determines which function call to bind to the object after deducing it at runtime.
  • 32. Polymorphism • Function Overloading • When there are multiple functions with the same name but different parameters, then the functions are said to be overloaded, hence this is known as Function Overloading. Functions can be overloaded by changing the number of arguments or/and changing the type of arguments. In simple terms, it is a feature of object-oriented programming providing many functions that have the same name but distinct parameters when numerous tasks are listed under one function name. There are certain Rules of Function Overloading that should be followed while overloading a function. // function overloading or // Compile-time Polymorphism #include <iostream> using namespace std; class ABC { public: // Function with 1 int parameter void func(int x) { cout << "value of x is " << x << endl; } // Function with same name but // 1 double parameter void func(double x) { cout << "value of x is " << x << endl; } // Function with same name and // 2 int parameters void func(int x, int y) { cout << "value of x and y is " << x << ", " << y << endl; } }; } // Driver code int main() { ABC obj1; // Function being called depends // on the parameters passed // func() is called with int value obj1.func(7); // func() is called with double value obj1.func(9.132); // func() is called with 2 int values obj1.func(85, 64); return 0; } Output value of x is 7 value of x is 9.132 value of x and y is 85, 64
  • 33. Polymorphism • Operator Overloading • C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can make use of the addition operator (+) for string class to concatenate two strings. We know that the task of this operator is to add two operands. So a single operator ‘+’, when placed between integer operands, adds them and when placed between string operands, concatenates them. // Operator Overloading or // Compile-Time Polymorphism #include <iostream> using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i = 0) { real = r; imag = i; } // This is automatically called // when '+' is used with between // two Complex objects Complex operator+(Complex const& obj) { Complex res; res.real = real + obj.real; res.imag = imag + obj.imag; return res; } void print() { cout << real << " + i" << imag << endl; } }; // Driver code int main() { Complex c1(10, 5), c2(2, 4); // An example call to "operator+" Complex c3 = c1 + c2; c3.print(); } Output12 + i9
  • 34. Dynamic Binding • In dynamic binding, the code to be executed in response to the function call is decided at runtime. C++ has virtual functions to support this. Because dynamic binding is flexible, it avoids the drawbacks of static binding, which connected the function call and definition at build time. // C++ Program to Demonstrate the Concept of Dynamic binding // with the help of virtual function #include <iostream> using namespace std; class ABC { public: void call_Function() // function that call print { print(); } void print() // the display function { cout << "Printing the Base class Content" << endl; } }; class ABC2 : public ABC // ABC2 inherit a publicly { public: void print() // ABC2's display { cout << "Printing the Derived class Content" << endl; } }; int main() { ABC A1; // Creating ABC's object A1.call_Function(); // Calling call_Function ABC2 A2; // creating ABC2 object A2.call_Function(); // calling call_Function // for ABC2 object return 0; } Output Printing the Base class Content Printing the Base class Content
  • 35. Message Passing • Objects communicate with one another by sending and receiving information. A message for an object is a request for the execution of a procedure and therefore will invoke a function in the receiving object that generates the desired results. Message passing involves specifying the name of the object, the name of the function, and the information to be sent.
  • 36. 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. • Non member function which can access private members of class.