Module 1 Section 3 PPT v4 Part 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Introduction to C++

Presented by:
Dr. Vinod Jain
Assistant Professor
Dept. Of CE&A
Syllabus
• 3.1 What is C++
• 3.2 A Simple C++ Program
• 3.3 Structure of C++ Program
• 3.4 Dynamic Initialization of Variables
• 3.5 Reference Variables
• 3.6 Scope Resolution Operators
• 3.7 Manipulators

<Subject Code> <Name of Subject> 2


What is C++
1. C++ is general-purpose object-oriented programming (OOP)
language developed by Bjarne Stroustrup.
2. Originally, C++ was called “C with classes,” as it had all the
properties of the C language with the addition of user-
defined data types called “classes.” It was renamed C++ in
1983.
3. C++ is considered an intermediate-level language, as it
includes both high and low-level language features

<Subject Code> <Name of Subject> 3


Why Use C++

1. C++ is one of the world's most popular programming


languages.
2. C++ can be found in today's operating systems, Graphical
User Interfaces, and embedded systems.
3. C++ is an object-oriented programming language which gives
a clear structure to programs and allows code to be reused,
lowering development costs.
4. As C++ is close to C# and Java, it makes it easy for
programmers to switch to C++ or vice versa

<Subject Code> <Name of Subject> 4


Features of C++
• Simple: It is a simple language as it use the syntax of C
language.
• Machine Independent but Platform Dependent: A C++
executable is not platform-independent (compiled programs
on Linux won’t run on Windows), however they are machine
independent.
• Mid-level language: It is a mid-level language as we can do
both systems-programming (drivers, kernels, networking etc.)
and build large-scale user applications (Media Players,
Photoshop, Game Engines etc.)
• Rich library support: Has a rich library support (Both standard
~ built-in data structures, algorithms etc.) as well 3rd party
libraries (e.g. Boost libraries) for fast and rapid development.
<Subject Code> <Name of Subject> 5
Features of C++ …
• Speed of execution: C++ programs excel in execution speed.
Since, it is a compiled language.
• Pointer and direct Memory-Access: C++ provides pointer
support which aids users to directly manipulate storage
address. This helps in doing low-level programming.
• Object-Oriented: Object-Oriented support helps C++ to make
maintainable and extensible programs. i.e. Large-scale
applications can be built. Procedural code becomes difficult to
maintain as code-size grows.
• Compiled Language: C++ is a compiled language, contributing
to its speed.

<Subject Code> <Name of Subject> 6


Applications of C++
• C++ finds varied usage in applications such as:
1. Operating Systems & Systems Programming. e.g. Linux-based
OS (Ubuntu etc.)
2. Browsers (Chrome & Firefox)
3. Graphics & Game engines (Photoshop, Blender, Unreal-
Engine)
4. Database Engines (MySQL, MongoDB, Redis etc.)
5. Cloud/Distributed Systems
6. Window application (Using Visual C++)
7. Client-Server application
8. Device drivers
9. Embedded firmware etc.
<Subject Code> <Name of Subject> 7
Difference b/w C and C++

1. Global and local variables are used in C, public and private


keywords are used in C++.
2. C is a structured programming language. C++ is an object
oriented programming language.
3. In C, different variables and functions are used. In C++, data
and objects are used.
4. Bitwise operators are used in C. In C++ bitwise operators are
not used.

<Subject Code> <Name of Subject> 8


A Simple C++ Program
#include <iostream>
using namespace std;
int main() {
cout << "Hello World"; // prints Hello World
return 0;
}
Output
Hello World
<Subject Code> <Name of Subject> 9
Structure of C++ Program

<Subject Code> <Name of Subject> 10


Structure of C++ Program
• Section 1 : Header File Declaration
Section
1. Header files used in the program
are listed here.
2. Header File provides Prototype
declaration for different library
functions.
3. We can also include user define
header file.
4. Basically all preprocessor
directives are written in this
section.
<Subject Code> <Name of Subject> 11
Structure of C++ Program
• Section 2 : Global Declaration
Section
1. Global Variables are declared
here.
2. Global Declaration may
include –
• Declaring Structure
• Declaring Class
• Declaring Variable

<Subject Code> <Name of Subject> 12


Structure of C++ Program
• Section 3 : Class Declaration
Section
1. Actually this section can be
considered as sub section for
the global declaration
section.
2. Class declaration and all
methods of that class are
defined here.

<Subject Code> <Name of Subject> 13


Structure of C++ Program
• Section 4 : Main Function
1. Each and every C++ program
always starts with main function.
2. This is entry point for all the
function. Each and every method
is called indirectly through main.
3. We can create class objects in the
main.
4. Operating system call this function
automatically.

<Subject Code> <Name of Subject> 14


Structure of C++ Program
• Section 1 : Header File Declaration
Section
1. Header files used in the program
are listed here.
2. Header File provides Prototype
declaration for different library
functions.
3. We can also include user define
header file.
4. Basically all preprocessor
directives are written in this
section.
<Subject Code> <Name of Subject> 15
Structure of C++ Program
• Section 5 : Method
Definition Section
• This is optional section .
Generally this method was
used in C Programming.

<Subject Code> <Name of Subject> 16


Dynamic Initialization of
Variables
• In C, a variable must be initialized using a constant expression.
• C++, however permits initialization of variables at run time
known as dynamic initialization.
• Example: - int n = strlen(str);
• float area = 3.14*r*r;
• Thus, both the declaration and initialization of a variable can
be done simultaneously at the place where variable is used
for the first time.

<Subject Code> <Name of Subject> 17


Reference Variables
• A reference variable provides an alternative name for a
previously defined variable.
• A reference declaration consists of a base type, an ampersand
(&) symbol and a reference variable name equated to a
previously defined variable name.
• Syntax: - Type & reference var. name = var. name.
• Example: - int total;
• total = 10;
• int & sum = total;
• cout<<sum;

<Subject Code> <Name of Subject> 18


Reference Variables

<Subject Code> <Name of Subject> 19


// WAP to demonstrate reference
// variable
#include<iostream>
using namespace std; Output
int main() Sum = 10
{ Sum = 15
int total=10;
int & sum=total;
cout<<"\n Sum = "<<sum;

total=total+5;
cout<<"\n Sum = "<<sum;
return 0;
}

<Subject Code> <Name of Subject> 20


Reference Variables
• Write a function that will swap value of two reference
variables passed as arguments.

void swap(int &a,int &b)


{
int temp =a;
a=b;
b=temp;
}

<Subject Code> <Name of Subject> 21


Scope Resolution Operator
• The scope resolution operator ( :: ) is used to resolve the
issues of scope. It is mainly used for
1. To access a global variable when there is a local variable with
same name.
2. To define functions outside of a class

<Subject Code> <Name of Subject> 22


Scope Resolution Operator
• To access a global variable when there is a local variable with
same name:
• We can access global variable using scope resolution when
there is a conflict between global and local variable name i.e.
when both the variables have same name.
• Here is a C++ program to show that we can access a global
variable using scope resolution operator :: when there is a
local variable with same name.

<Subject Code> <Name of Subject> 23


<Subject Code> <Name of Subject> 24
// Scope resolution operator ::
#include<iostream>
using namespace std;
int x=5; // Global x
• Output
• Value of global x is 5
int main()
• Value of local x is 10
{
int x = 10; // Local x
cout << "Value of global x is " << ::x;
cout << "\nValue of local x is " << x;
return 0;
}

<Subject Code> <Name of Subject> 25


Scope Resolution Operator
• To access a global variable when there is a local variable with
same name:
• We can declare functions inside a class and can define the
functions outside the class. Outside the class we have to use
scope resolution operator :: to define that this function belongs
to which class.
• Here is a C++ program to show that scope resolution operator
:: is used to define a function outside a class

<Subject Code> <Name of Subject> 26


<Subject Code> <Name of Subject> 27
// Scope resolution operator ::
#include<iostream>
using namespace std; int main()
class A
{
{
public: A a;
// Only declaration a.fun();
void fun();
}; return 0;
// Definition outside class using :: }
void A::fun()
{
Output
cout << "fun() called"; fun() called
}

<Subject Code> <Name of Subject> 28


Scope Resolution Operator

Some other uses of scope resolution operator are covered


later on in the syllabus.
3) To access a class’s static variables.
4) In case of multiple Inheritance
5) For namespace Eg. std::cout << "Hello";
6) Refer to a class inside another class

Reference
https://www.geeksforgeeks.org/scope-resolution-operator-in-c/

<Subject Code> <Name of Subject> 29


Questions

<Subject Code> <Name of Subject> 30


Thank you

<BCAC0007> <Object Oriented Programming> 31

You might also like