0% found this document useful (0 votes)
21 views37 pages

Unit-1 Overview of C++ Programming: Prepared By: Dr. Aayushi Chaudhari, Prof. Nishat Shaikh

The document provides an overview of C++ programming, including its history, key features, and role in modern software development. It covers fundamental data types, challenges related to data type portability, and the principles of Object-Oriented Programming (OOP) such as encapsulation, inheritance, and polymorphism. Additionally, it compares procedural programming with OOP and highlights the significance of C++ in various applications.

Uploaded by

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

Unit-1 Overview of C++ Programming: Prepared By: Dr. Aayushi Chaudhari, Prof. Nishat Shaikh

The document provides an overview of C++ programming, including its history, key features, and role in modern software development. It covers fundamental data types, challenges related to data type portability, and the principles of Object-Oriented Programming (OOP) such as encapsulation, inheritance, and polymorphism. Additionally, it compares procedural programming with OOP and highlights the significance of C++ in various applications.

Uploaded by

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

Unit-1

OVERVIEW OF C++
PROGRAMMING
Prepared By:
Dr. Aayushi Chaudhari,
Prof. Nishat Shaikh

February 28, 2025 | U & P U. Patel Department of Computer Engineering 1


Contents
Introduction to C++ Programming Language:
Brief history of C++, Key features of C++: efficiency, performance, and versatility, Role of C++ in
modern software development
Fundamental Types and Portability:
Understanding fundamental data types in C++, Challenges related to data type portability
Introduction to Object-Oriented Programming (OOP):
Core principles of OOP: Abstraction, Encapsulation, Inheritance, Polymorphism, Differences between
Object-Oriented and Procedural programming, The significance of OOP in software development.
Standard Library and Namespaces:
Introduction to the C++ Standard Library, Overview of namespaces in C++, Importance of using
namespaces, structure of C++ program, data member, member function, initializing object with
constructor
February 28, 2025| U & P U. Patel Department of Computer Engineering
Introduction to C++
• C++ is a general-purpose programming language that was developed as an enhancement of
the C language to include object-oriented paradigm.
• It is an imperative and a compiled language.
• C++ is a high-level, general-purpose programming language designed for system and
application programming.
• It was developed by Bjarne Stroustrup at Bell Labs in 1985 as an extension of the C
programming language.
• One of the key features of C++ is its ability to support low-level, system-level
programming, making it suitable for developing operating systems, device drivers, and
other system software.
• At the same time, C++ also provides a rich set of libraries and features for high-level
application programming, making it a popular choice for developing desktop applications,
video games, and other complex applications.
February 28, 2025| U & P U. Patel Department of Computer Engineering
Key Features of C++
• Object-Oriented Programming (OOP): C++ supports OOP principles, making it easier to design, organise,
and manage code. It allows developers to create classes, objects, and inherit properties and behaviours.
• Classes and Objects: C++ enables the creation of classes and objects, facilitating the modelling of real-world
entities. This enhances code organisation and reusability.
• Templates: C++ templates allow developers to write generic code that can work with any data type, making it
easier to write reusable and flexible code.
• Standard Template Library (STL): The STL provides a wide range of containers and algorithms for
working with data, making it easier to write efficient and effective code. It is a collection of template classes
and functions that provide essential data structures (like vectors and lists) and algorithms (like sorting and
searching). It simplifies complex programming tasks.
• Exception Handling: C++ provides robust exception handling capabilities, making it easier to write code that
can handle errors and unexpected situations.
• Performance: C++ offers high performance and low-level memory control, making it a preferred choice for
system-level programming and resource-intensive applications.
February 28, 2025| U & P U. Patel Department of Computer Engineering
Role of C++ in modern software development
Gaming
GUI based applications
Adobe Photoshop and Illustrator
Operating system
Database softwares
Browsers
Animation tools
Compilers
Banking applications
Embedded systems
February 28, 2025| U & P U. Patel Department of Computer Engineering
Procedure Oriented Programming
C Programming was Procedure Oriented Programming, well known as structured
programming language, was popular in 1980s.

It helped programmers to write moderately complex programs.

However, when the program grows larger, the structured approach got failed in achieving
desired results, in terms of bug-free, easy to maintain and reusable programs.

February 28, 2025| U & P U. Patel Department of Computer Engineering


Why Object Oriented Programming?
It is an approach to organize and develop program in such a manner, which would eliminate
pitfalls of conventional programming methods, by incorporating the best structured
programming with several powerful new concepts.

So, we can consider it as a new way of organizing and developing programs and it has nothing
to do with language.

All languages do not support object oriented programming concepts easily.

February 28, 2025| U & P U. Patel Department of Computer Engineering


C++ Datatypes

February 28, 2025| U & P U. Patel Department of Computer Engineering


C++ fundamental Datatypes
Whenever a variable is defined in C++, the compiler allocates some memory for that variable
based on the data type with which it is declared as every data type requires a different
amount of memory.
Data Type Meaning Size (in Bytes)

int Integer 2 or 4

float Floating-point 4

double Double Floating-point 8

char Character 1

wchar_t Wide Character 2

bool Boolean 1

void Empty 0

February 28, 2025| U & P U. Patel Department of Computer Engineering


C++ fundamental Datatypes
 Int
The int keyword is used to indicate integers.
Its size is usually 4 bytes. Meaning, it can store values from -2147483648 to 2147483647.
Eg. int salary= 20000;

 Float and double


float and double are used to store floating-point numbers (decimals and exponentials).
The size of float is 4 bytes and the size of double is 8 bytes. Hence, double has two times the
precision of float
E.g. float km = 250.20;
Double distance = 45E12 i.e. 45*10^12
February 28, 2025| U & P U. Patel Department of Computer Engineering
C++ fundamental Datatypes
 Char
Keyword char is used for characters.
Its size is 1 byte.
Characters in C++ are enclosed inside single quotes ' '.
E.g. : char Alpha = ‘A’;

 Wchar_t
Wide character wchar_t is similar to the char data type, except its size is 2 bytes instead of 1.
It is used to represent characters that require more memory to represent them than a single char.
E.g.: w_char Alpha = L’ 日’ ; //Used for Japanese languages.
Notice the letter L before the quotation marks.
February 28, 2025| U & P U. Patel Department of Computer Engineering
C++ fundamental Datatypes
 bool
The bool data type has one of two possible values: true or false.
Booleans are used in conditional statements and loops.
E.g.: bool cond = false;

 void
The void keyword indicates an absence of data. It means "nothing" or "no value".
We will use void when we learn about functions and pointers.
Note: We cannot declare variables of the void type.

February 28, 2025| U & P U. Patel Department of Computer Engineering


Challenges in datatype portabilities
Data type portability in C++ refers to the ability of code using specific data types to behave consistently
across different platforms, compilers, and architectures. Challenges arise because the exact size,
representation, and alignment of data types can vary.
Following are the challenges:
1. Platform-Dependent Sizes:
Problem:
Fundamental data types like int, long, float, etc., do not have fixed sizes.
For example: On a 32-bit system, int is often 2 bytes.
On a 64-bit system, long may differ between platforms (e.g., 8 bytes on Linux but 4 bytes on Windows).
2. Standard Library Differences
Problem: Some library implementations or extensions may not be available or behave differently across
platforms.
February 28, 2025| U & P U. Patel Department of Computer Engineering
Cont..
3. Character Encoding
Problem: Different platforms may use different character encodings (e.g., ASCII vs. UTF-16).
4. Compiler-Specific Behavior
Problem: Compiler extensions or undefined behavior might work differently (or fail) when
the code is compiled with a different toolchain.
5. Differences in sizeof
Problem: The sizeof operator's results depend on the data type size, which is not consistent
across platforms.
6. Third-Party Libraries
Problem: Dependencies on third-party libraries can introduce portability issues if the libraries
are not available or optimized for all platforms.
February 28, 2025| U & P U. Patel Department of Computer Engineering
Introduction to Object-Oriented Programming (OOP):
• 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.

February 28, 2025| U & P U. Patel Department of Computer Engineering


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


Data members => data variables and
member functions => functions

Functions are used to manipulate these variables and together these data members and member
functions define the properties and behavior of the objects in a Class.

Example: Class of Cars .


There may be many cars with different names and brand but all of them will share some common
properties like all of them will have 4 wheels, Speed Limit, Mileage range etc will act as data members.

Applying brakes, increasing speed may lead to member function of that class.
February 28, 2025| U & P U. Patel Department of Computer Engineering
Class
A class can be thought of as a template used to create a set of objects.
A class is a static definition; a piece of code written in a programming language.
One or more objects described by the class are instantiated at runtime.

The objects are called instances of the class.


Each instance will have its own distinct set of attributes.
Every instance of the same class will have the same set of attributes;

every object has the same attributes but,


each instance will have its own distinct values for those attributes.
February 28, 2025| U & P U. Patel Department of Computer Engineering
Data Members and Member Functions
Data members
The variables which are declared in any class by using any fundamental data types (like int,
char, float etc) or derived data type (like class, structure, pointer etc.) are known as Data
Members.

Member Functions
And the functions which are declared in class are known as Member functions.

February 28, 2025| U & P U. Patel Department of Computer Engineering


What do you mean by an object?
• Objects are the runtime entities, existing in the real world. They can be represented as person, a place, a bank
account.
• Real-world objects have attributes and behaviors.
Examples:
Dog =>
Attributes: breed, color, hungry, tired, etc.
Behaviors: eating, sleeping, etc.
Bank Account =>
Attributes: account number, owner, balance
Behaviors: withdraw, deposit
Note: When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is
allocated.
One class can have multiple objects, which can interact with each other
February 28, 2025| U & P U. Patel Department of Computer Engineering
POP VS OOP
Procedural Oriented Programming Object Oriented Programming

Divided Into In POP, program is divided into small parts called functions. In OOP, program is divided into parts called objects.

In POP, Importance is not given to data but to functions as well In OOP, Importance is given to the data rather than procedures or functions
Importance as sequence of actions to be done. because it works as a real world.
Approach POP follows Top Down approach. OOP follows Bottom Up approach.
Access Specifiers POP does not have any access specifier. OOP has access specifiers named public, private, protected, etc.
In POP, Data can move freely from function to function in the In OOP, objects can move and communicate with each other through
Data Moving system. member functions.
Expansion To add new data and function in POP is not so easy. OOP provides an easy way to add new data and function.

In POP, Most function uses Global data for sharing that can be In OOP, data cannot move easily from function to function. It can be kept
Data Access accessed freely from function to function in the system. public or private so we can control the access of data.

POP does not have any proper way for hiding data so it is less
Data Hiding secure.
OOP provides Data Hiding so provides more security.

In OOP, overloading is possible in the form of Function Overloading and


Overloading In POP, Overloading is not possible.
Operator Overloading.
Examples Example of POP are: C, VB, FORTRAN, and Pascal. Example of OOP are: C++, JAVA, VB.NET, C#.NET.

February 28, 2025| U & P U. Patel Department of Computer Engineering


What is Encapsulation?
The wrapping up of data and functions into a single
unit is known as encapsulation. Visible Methods

Hidden
When classes are defined, programmers can specify State
Variables
that certain methods or state variables remain and
hidden inside the class. Methods

Visible Variables
These variables and methods are accessible from
within the class, but not accessible outside it.

Class
The insulation of the data from direct access by the Definition
program is called data hiding or information hiding.
February 28, 2025| U & P U. Patel Department of Computer Engineering
What is abstraction?
Abstraction refers to the act of representing essential features without including the
background details or explanations.

Classes use the concept of abstraction and are defined as a list of abstract attributes such as
size, weight, and functions to operate on these attributes. They encapsulate all the essential
properties of the objects that are to be created.

Consider a real-life example of a man driving a car. The man only knows that pressing the
accelerators will increase the speed of the car or applying brakes will stop the car but he does
not know about how on pressing accelerator the speed is actually 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.
February 28, 2025| U & P U. Patel Department of Computer Engineering
What is inheritance?
Inheritance is the process by which objects of one class acquire the properties of objects of another
class.
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 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.
February 28, 2025| U & P U. Patel Department of Computer Engineering
Inheritance Example

February 28, 2025| U & P U. Patel Department of Computer Engineering


Polymorphism
Polymorphism is one of the essential features of an object- oriented language. It means ability
to take more than one form.

An operation may exhibit different behaviours in different instances. The behavior depends
upon the type of data used in the operations.

Example: Addition operation, with integer datatype, it will generate sum, but if operands are
strings, it will generate third string by concatenation.

The process of making an operator to exhibit different behaviors in different instances is


known as operator overloading.

February 28, 2025| U & P U. Patel Department of Computer Engineering


Polymorphism cont..
Single function name can be used to handle different Shape
number and different types of arguments.
Draw ()

This is something similar to a particular word having


several different meaning depending on the context.
So, using a single function name to perform different
types of tasks is known as function overloading.
Triangle Square
Circle Object
Object Object
Polymorphism is extensively used in implementing Draw (circle)
Draw Draw
inheritance. (triangle) (square)

February 28, 2025| U & P U. Patel Department of Computer Engineering


Dynamic Binding
Dynamic binding refers to linking a procedure call to code that will execute only once.

The code associated with the procedure is not known until the program is executed, which is
also known as late binding.

Dynamic binding happens when all information needed for a function call cannot be
determined at compile-time.

Dynamic binding can be achieved using the virtual functions.


Advantage of using dynamic binding is that it is flexible since a single function can handle
different type of objects at runtime.
February 28, 2025| U & P U. Patel Department of Computer Engineering
Significance of using OOP in software development
Increases productivity: OOP allows programmers to construct new programs quickly by using
reusable code and multiple libraries.
Improves maintainability: OOP's modularity and reusability reduce redundancy and make code
easier to maintain.
Simplifies troubleshooting: OOP's self-contained nature makes it easier to identify the source of a
problem in the code.
Models complex systems: OOP allows developers to create intuitive models of complex systems by
representing real-world entities as objects with properties and behaviors.
Saves time: OOP's polymorphism allows a single function to adapt to the class it's in, saving time and
making it easy to add new vehicle types.
Enhances flexibility: OOP's inheritance and polymorphism provide flexibility for your codebase.
Reduces development costs: OOP's maintenance and reusability benefits reduce development costs.
February 28, 2025| U & P U. Patel Department of Computer Engineering
Standard Library and Namespaces:
• In C++, a namespace is a collection of related names or identifiers (functions, class,
variables) which helps to separate these identifiers from similar identifiers in other
namespaces or the global namespace.
• The identifiers of the C++ standard library are defined in a namespace called std.
• In order to use any identifier belonging to the standard library, we need to specify that it
belongs to the std namespace. One way to do this is by using the scope resolution operator ::

• For e.g. std::cout << "Hello World!";

• Here, we have used the code std:: before cout. This tells the C++ compiler that the cout
object we are using belongs to the std namespace.

February 28, 2025| U & P U. Patel Department of Computer Engineering


C++ std Identifiers
All the standard library identifiers provided by the standard header files like <iostream>,
<string>, <vector>, etc. are declared in the std namespace.

For example, identifiers cin and cout are defined inside the standard header file <iostream> of
the namespace std.
The first way we access identifiers in the std namespace is by directly qualifying the identifier
with the prefix std::. Here,

std is the C++ standard library namespace


:: is the scope resolution operator

February 28, 2025| U & P U. Patel Department of Computer Engineering


Introduction to the C++ Standard Library
The C++ Standard Library is a collection of functions, containers, and other tools that can be
used in C++ programs:
Containers: Generic containers for storing data
Functions: Functions for manipulating containers, performing common tasks, and more
Streams: Generic streams for input and output, including interactive streams
Support: Support for some language features
The C++ Standard Template Library (STL) is a set of template classes and functions that
provides the implementation of common data structures and algorithms such as lists,
stacks, arrays, sorting, searching, etc.

February 28, 2025| U & P U. Patel Department of Computer Engineering


Components of STL
The components of STL are the features provided by Standard Template Library (STL) in C++
that can be classified into 4 types:
Containers
Algorithms
Iterators
Functors

February 28, 2025| U & P U. Patel Department of Computer Engineering


Overview of namespaces in C++,
Namespace provide the space where we can define or declare identifier i.e. variable, method, classes.
Using namespace, you can define the space or context in which identifiers are defined i.e. variable,
method, classes. In essence, a namespace defines a scope.
Importance of using namespaces :
Example, you might be writing some code that has a function called xyz() and there is another library
available which is also having same function xyz(). Now the compiler has no way of knowing
which version of xyz() function you are referring to within your code.
A namespace is designed to overcome this difficulty and is used as additional information to
differentiate similar functions, classes, variables etc. with the same name available in different
libraries.
The best example of namespace scope is the C++ standard library (std) where all the classes, methods
and templates are declared. Hence while writing a C++ program we usually include the directive
using namespace std;
February 28, 2025| U & P U. Patel Department of Computer Engineering
structure of C++ program
The structure of the program written in C++ language is as follows:
Documentation Section:
This section comes first and is used to document the logic of the
program that the programmer going to code.
It can be also used to write for purpose of the program.
Linking Section:
The linking section contains two parts:
Header Files:
Standard headers are specified in a program through
the preprocessor directive #include.
February 28, 2025| U & P U. Patel Department of Computer Engineering
Cont..
Namespaces:
A namespace permits grouping of various entities like classes, objects, functions, and various C++ tokens, etc. under a single name.
Namespaces can be accessed in multiple ways:
using namespace std;
using std :: cout;
Definition Section:
It is used to declare some constants and assign them some value.
Global Declaration Section:
Here, the variables and the class definitions which are going to be used in the program are declared to make them global.
Function Declaration Section:
It contains all the functions which our main functions need.
Usually, this section contains the User-defined functions.
Main Function:
The main function tells the compiler where to start the execution of the program. The execution of the program starts with the main
function.
February 28, 2025| U & P U. Patel Department of Computer Engineering
Example of C++ program
#include <iostream>

int main() {
std::cout << "Hello World!";
return 0;
}

Output

Hello World!
February 28, 2025| U & P U. Patel Department of Computer Engineering
Thank You.

February 28, 2025| U & P U. Patel Department of Computer Engineering

You might also like