Chapter 2

You might also like

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

Chapter 2:Introduction to C++

Sudeep Shakya
Associate Prof. / Head
Department of Computer Engineering
Kathmandu Engineering College
KEC,Kalimati
The need or importance of C++(Imp)
❖C programming language cannot handle complexity
❖Data that are globally declared can be accessed by the
functions other than the function declared for.
❖In order to solve these problems,Bjarne Stroustrup
developed C++ in 1980s.C++ is nothing but C with classes.
C++ adds object oriented features in C meaning that C++
has all the features and benefit of C.
❖C++ is an Object-Oriented Programming Language and
includes features like classes,inheritance,polymorphism,data
abstraction and encapsulation.
❖C++ has a rich function library
❖C++ allows exception handling, function overloading and
others which are not possible in C.
❖C++ is powerful and efficient language having wide range
of applications from GUI applications to 3D graphics for
games etc.
Features of C++(Imp)
C++ consists of common features of object oriented
programming language as well as some typical features of
C.Following are the some important features of C++.
1)Flexible declarations:
In C++ variables can be declared and initialized at the time of
need where as in c the variable should be declared at the
beginning of the function definition.
2)Function Overloading:
In C++ we can define more than one functions with the same name and
it helps to remember the name of the functions which are logically same
just by remembering the single name.
3)Reference variable:
• C++ supports one more type of variable called reference variable in
addition to the value variable and pointer variables in C.Value
variables are used to hold some numeric values and pointer variables
are used to hold address of some other value variables. Reference
variable behaves similar to both a value variable and a pointer
variable. In a program code it is used similar to that of a value
variable but has an action of pointer variable. Thus it provides an
alias(alternative name) of the variable that is previously defined. It
helps the function for returning multiple values from the function.
• Syntax: data_type &reference name=variable name;
Reference variable should be immediately initialized after its
declaration with already defined variable.
• Let us consider the following two programs
1)Program without use of reference variable
#include<iostream>
using namespace std;
int main( )
{
int b=10;
int a=b; a b
a++; 10 10
cout<< “a=“<<a<<endl;
cout<<“b=“<<b; Output:
return 0; a=11
} b=10
• Program with the use of reference variable.
#include<iostream>
using namespace std;
int main( )
{
int b=10;
int &a=b;
a++; 10
cout<<“a=“<<a<<endl;
cout<<“b=“<<b; a b
Output
return 0; a=11
} b=11
In above program a is an reference variable to a previously
defined variable b that means a and b denotes same variable
and no separate memory is allocated for variable a and b that
means change in one variable is affected upon the other.
4)New and delete operator:
In C++ memory can be allocated dynamically by using new
operator and for deallocating i.e. for making memory free
delete operator is used. Where as in c function like
calloc(),malloc() are used for DMA(Dynamic Memory
Allocation) and function free() is used for deallocating the
memory.
5)Class( Explained before)
6)Inheritance(Explained before)
7)Generic Programming:
In c programming we need to define different code for the
same algorithm but with different data type. But C++ show the
generic programming with the help of template where we can
create single function and single class that can work for
multiple data types.
8)STL(Standard Template Library):
In C++ the data structures which are needed in programming
are already defined with different functions associated with
them in a library which is known as standard template library.
It implements template to show generic programming.
• Differences between C and C++(imp)
C C++
1)C follows the procedural 1)C++ is a multi-paradigm
programming paradigm i.e. importance language(procedural as well as object
is given to the steps or procedure of the oriented) i.e. C++ focuses on the data
program rather than the process.
2)C uses top-down approach in 2)C++ uses the bottom up approach in
programming. programming.
3)Data are not secured 3)Data are secured
4)Functions are the building blocks of 4)Objects are the building block of C++
a C program so it is function driven. program so it is object driven.
5)C does not support function 5)C++ supports function overloading.
overloading
6)Structures cannot contain functions 6)In C++ functions can be used inside a
in C class
7)The namespace feature is absent in C 7)C++ allows the namespace feature

8)C does not allow the use of reference 8)C++ allows the use of reference
variables. variables

You might also like