0% found this document useful (0 votes)
27 views48 pages

C++ Unit-1

C++ notes

Uploaded by

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

C++ Unit-1

C++ notes

Uploaded by

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

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

SCHOOL OF ENGINEERING & TECHNOLOGY


• .

Programming in C++
CSL312
By:
Rohit Maheshwari
Assistant Professor
CSE Dept.
Procedure oriented programming V/S Object
oriented programming

The high-level programming languages are broadly categorized in to two categories:

(i) Procedure oriented programming(POP) language.


(ii) Object oriented programming(OOP) language.

Procedure Oriented Programming Language

• In the procedure oriented approach, the problem is viewed as sequence of things to be done such as
reading , calculation and printing.
Characteristics of procedure oriented programming:

1. Emphasis is on doing things(algorithm)


2. Large programs are divided into smaller programs known as functions.
3. Most of the functions share global data
4. Data move openly around the system from function to function
5. Function transforms data from one form to another.
6. Employs top-down approach in program design
Object Oriented Programing

“Object oriented programming as an approach that provides a way of modularizing

programs by creating partitioned memory area for both data and functions that can be used

as templates for creating copies of such modules on demand”.


Features of the Object Oriented programming

1. Emphasis is on data rather than procedure.


2. programs are divided into what are known as objects.
3. Data structures are designed such that they characterize the objects.
4. Functions that operate on the data of an object are tied together in the data structure.
5. Data is hidden and can’t be accessed by external functions.
6. Objects may communicate with each other through functions.
7. New data and functions can be easily added.
8. Follows bottom-up approach in program design
Object-Oriented Programming (OOPs) Concepts:

C++ supports the object-oriented programming, the four major pillar of object-oriented programming
(OOPs) used in C++ are:

1. Objects
2. Classes
3. Data abstraction and encapsulation
4. Inheritance
5. Polymorphism
6. Dynamic binding
7. Message passing
Object means a real word entity such as pen, chair,
table etc. Object-Oriented Programming is a
methodology or paradigm to design a program
using classes and objects.

Object

Any entity that has state and behavior is known as an object.


For example: chair, pen, table, keyboard, bike etc. It can be
physical and logical.

Class

Collection of objects is called class. It is a logical entity.


Abstraction
• Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know
the internal processing.
• In C++, we use abstract class and interface to achieve abstraction.

Encapsulation
Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it
is wrapped with different medicines.

Inheritance
When one object acquires all the properties and behaviors of parent object i.e. known as inheritance. It provides
code reusability. It is used to achieve runtime polymorphism.
Polymorphism
• When one task is performed by different ways i.e. known as polymorphism. For example: to convince the
customer differently, to draw something e.g. shape or rectangle etc.
• In C++, we use Function overloading and Function overriding to achieve polymorphism.

Dynamic Binding
Binding refers to the linking of a procedure call to the code to the executed in response to the call. Dynamic
binding means the code associated with a given procedure call is not known until the time of the call at run-time.

Message Passing
An object oriented program consists of a set of objects that communicate with each other.
Advantage of OOPs over Procedure-oriented programming language
1. OOPs makes development and maintenance easier where as in Procedure-oriented programming
language it is not easy to manage if code grows as project size grows.
2. OOPs provide data hiding whereas in Procedure-oriented programming language a global data can be
accessed from anywhere.
3. OOPs provide ability to simulate real-world event much more effectively. We can provide the
solution of real word problem if we are using the Object-Oriented Programming language.
What is C++?

• C++ is a general purpose, case-sensitive, free-form programming language that supports object-oriented, procedural
and generic programming.
• C++ is a middle-level language, as it encapsulates both high and low level language features.

• C++ is a special-purpose programming language developed by Bjarne Stroustrup at Bell Labs circa 1980.
• C++ language is very similar to C language, and it is so compatible with C that it can run 99% of C programs without
changing any source of code though C++ is an object-oriented programming language, so it is safer and well-
structured programming language than C.

• C++ programming is "relative" (called a superset) of C, it means any valid C program is also a valid C++ program.
C++ Features

1.Simple
2.Machine Independent or Portable
3.Mid-level programming language
4.Structured programming language
5.Rich Library
6.Memory Management
7.Fast Speed
8.Recursion
9.Extensible
10.Object Oriented
11.Compiler based
Structure Of A Program :

// my first program in C++

#include <iostream>
using namespace std;

int main ()
{
cout << "Hello World!";
return 0;
}
Output:-Hello World!
C++ Comments

C++ introduces a new comment symbol //(double slash). Comments start with a double slash symbol and
terminate at the end of line.

The double slash comment is basically a single line comment. Multi line comments can be written as follows:

// this is an example of
// c++ program
// thank you
The c comment symbols /* ….*/ are still valid and more suitable for multi line comments.

/* this is an example of c++ program */


I/O Library Header Files
Header File Function and Description

It is used to define the cout, cin objects, which correspond to standard output stream,
<iostream>
standard input stream and standard error stream, respectively.

It is used to declare services useful for performing formatted I/O, such as setprecision and
<iomanip>
setw.
<fstream> It is used to declare services for user-controlled file processing.

• C++ I/O operation is using the stream concept. Stream is the sequence of bytes or flow of data. It makes the
performance fast.
• If bytes flow from main memory to device like printer, display screen, or a network connection, etc, this is called as
output operation.
• If bytes flow from device like printer, display screen, or a network connection, etc to main memory, this is called as
input operation.
Output Operator:
• The statement cout <<”Hello, world” displayed the string with in quotes on the screen.
• The identifier cout can be used to display individual characters, strings and even numbers.
• It is a predefined object that corresponds to the standard output stream.
• Stream just refers to a flow of data and the standard Output stream normally flows to the screen display.
• The cout object, whose properties are defined in iostream.h represents that stream.
• The insertion operator << also called the ‘put to’ operator directs the information on its right to the object on its left.

Return Statement:
• In C++ main ( ) returns an integer type value to the operating system.
• Therefore every main ( ) in C++ should end with a return (0) statement, otherwise a warning or an error might occur.
Input Operator:
• The statement : cin>> number 1; is an input statement and causes the program to wait for the user to type in a number.
• The identifier cin is a predefined object in C++ that corresponds to the standard input stream. Here this stream
represents the key board.
• The operator >> is known as get from operator. It extracts value from the keyboard and assigns it to the
variable on its right.

Cascading Of I/O Operator:


cout<<”sum=”<<sum<<”\n”;
cout<<”sum=”<<sum<<”\n”<<”average=”<<average<<”\n”;
cin>>number1>>number2;
#include <iostream>
• Lines beginning with a hash sign (#) are directives for the preprocessor.

using namespace std;


• All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the
name std.
• So in order to access its functionality we declare with this expression that we will be using these entities.
• This line is very frequent in C++ programs that use the standard library.
return 0;
• The return statement causes the main function to finish. return may be followed by a return code.
• A return code of 0 for the main function is generally interpreted as the program worked as expected without any
errors during its execution.
C++ Variable

• A variable is a name of memory location. It is used to store data. Its value can be changed and it can be reused
many times.
• It is a way to represent memory location through symbol so that it can be easily identified.

Rules for defining variables


• A variable can have alphabets, digits and underscore.
• A variable name can start with alphabet and underscore only. It can't start with digit.
• No white space is allowed within variable name.
• A variable name must not be any reserved word or keyword e.g. char, float etc.
C++ Data Types
• A data type specifies the type of data that a variable can
store such as integer, floating, character etc.
Tokens in C++

• Tokens in C++ is the most important element to be used in creating a program in C.


• We can define the token as the smallest individual element in C.
C++ Keywords

A keyword is a reserved word. You cannot use it as a variable name, constant name etc.

A list of 32 Keywords in C++ Language


A list of 30 Keywords in C++ Language which are not available in C language
C++ Operators
An operator is simply a symbol that is used to perform operations. There can be many types of operations like
arithmetic, logical, bitwise etc.
There are following types of operators to perform different types of operations in C language.
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operator
• Unary operator
• Ternary or Conditional Operator
Precedence of Operators in C++
C++ Identifiers
• C++ identifiers in a program are used to refer to the name of the variables, functions, arrays, or other user-defined
data types created by the programmer.
• They are the basic requirement of any language. Every language has its own rules for naming the identifiers.
In short, we can say that the C++ identifiers represent the essential elements in a program which are given below:
• Constants
• Variables
• Functions
• Labels
• Defined data types
Some naming rules are common in both C and C++. They are as follows:
• Only alphabetic characters, digits, and underscores are allowed.

• The identifier name cannot start with a digit, i.e., the first letter should be alphabetical.
• After the first letter, we can use letters, digits, or underscores.
• In C++, uppercase and lowercase letters are distinct. Therefore, we can say that C++ identifiers are
case-sensitive.
• A declared keyword cannot be used as a variable name.
C++ Expression

• C++ expression consists of operators, constants, and variables which


are arranged according to the rules of the language.
• It can also contain function calls which return values.
• An expression can consist of one or more operands, zero or more
operators to compute a value.
• Every expression produces some value which is assigned to the
variable with the help of an assignment operator.
C++ Structure

• C++ Structure is a collection of different data types. It is similar to the class that holds different types of data.
• Unlike class, structs in C++ are value type than reference type.
• It is useful if you have data that is not intended to be modified after creation of struct.

A structure is declared by preceding the struct keyword.


Structure Example
Structure v/s Class
C++ Enumeration
• Enum in C++ is a data type that contains fixed set of constants.
• It can be used for days of the week, directions etc.
• The C++ enum constants are static and final implicitly.
• C++ enums can be thought of as classes that have fixed set of constants.
Example

Output:

Day: 5
C++ Functions
• The function in C++ language is also known as procedure or subroutine in other programming languages.
• To perform any task, we can create function. A function can be called many times. It provides modularity and code
reusability.

Advantage of functions in C
There are many advantages of functions.
1) Code Reusability
• By creating functions in C++, you can call it many times. So we don't need to write the same code again and again.
2) Code optimization
• It makes the code optimized, we don't need to write much code.
Types of Functions
There are two types of functions in C programming:
1. Library Functions: are the functions which are declared in
the C++ header files such as ceil(x), cos(x), exp(x), etc.
2. User-defined functions: are the functions which are created
by the C++ programmer, so that he/she can use it many times. It
reduces complexity of a big program and optimizes the code.
Declaration of a function
C++ Function Example
Call by value and call by reference in C++
Call by value in C++

• In call by value, original value is not modified.


• In call by value, value being passed to the function is
locally stored by the function parameter in stack memory
location.
• If you change the value of function parameter, it is changed
for the current function only.
• It will not change the value of variable inside the caller
method such as main().
Call by reference in C++

In call by reference, original value is modified


because we pass reference (address).
INLINE FUNCTION:

• To eliminate the cost of calls to small functions C++ proposes a new feature called inline function.
• An inline function is a function that is expanded inline when it is invoked .
• That is the compiler replaces the function call with the corresponding function code.

The inline functions are defined as follows:-

inline function-header
{
function body;
}
Example:
#include<iostream.h> main( )
#include<stdio.h> {
inline float mul(float x, float y) float a=12.345;
{ float b=9.82;
return(x*y); cout<<mul(a,b)<<endl;
} cout<<div (a,b)<<endl;
inline double div(double p.double q) }
{ output:-
return(p/q); 121.227898
} 1.257128
FUNCTION OVERLOADING:

Overloading refers to the use of the same thing for different purposes . C++ also permits overloading functions.

This means that we can use the same function name to creates functions that perform a variety of different tasks. This

is known as function polymorphism in OOPs.


Example: int volume( int s)
#include<iostream.h>
{
int volume(int);
return (s*s*s); //cube
double volume( double , int ); }
double volume(long int ,int ,int); double volume( double r, int h)

main( ) {
return(3.1416*r*r*h); //cylinder
{
}
cout<<volume(10)<<endl; output:-
long volume (long int 1, int b, int h)
cout<<volume(3.5,5)<<endl; 1000
{
cout<<volume(10,11,12)<<endl; 192.423
return(1*b*h); //cylinder
} } 1320
C++ Recursion

• When function is called


within the same function,
it is known as recursion
in C++.
• The function which calls
the same function, is
known as recursive
function.
C++ Storage Classes

You might also like