C++ 2
C++ 2
Report About:-
C++
1
Introduction to C++
2
fi
Bene ts of C++ over C
Language
3
fi
ff
fi
Object Oriented
Programming in C++
Object Oriented programming is a programming
style that is associated with the concept of Class,
Objects and various other concepts revolving
around these two, like Inheritance, Polymorphism,
Abstraction, Encapsulation etc.
4
Class
Here we can take Human Being as a class. A
class is a blueprint for any functional entity which
de nes its properties and its functions. Like
Human Being, having body parts, and performing
various actions.
Inheritance
5
fi
Objects
Abstraction
Encapsulation
This concept is a little tricky to explain with our
example. Our Legs are binded to help us walk.
Our hands, help us hold things. This binding of the
properties to functions is called Encapsulation.
6
fi
Polymorphism
1. Objects
2. Classes
3. Abstraction
4. Encapsulation
5. Inheritance
7
ff
fi
ff
fi
fi
6. Overloading
7. Exception Handling
Objects
Class
8
fi
fi
fi
Abstraction
Encapsulation
9
fi
Inheritance
Polymorphism
10
fi
ff
ff
fi
ff
fi
Exception Handling
11
First C++ program
#include <iostream.h>
using namespace std;
int main()
{
cout << "Hello this is C++";
}
12
fi
fi
fi
fi
fi
fi
main(), is the function which holds the executing
part of program its return type is int.
cout <<, is used to print anything on screen,
same as printf in C language. cin and cout are
same as scanf and printf, only di erence is that
you do not need to mention format speci ers like,
%d for int etc, in cout & cin.
/*this is
a multiple line
comment */
13
ff
fi
Creating Classes in C++
Classes name must start with capital letter, and
they contain data Variables and member
functions. This is a mere introduction to classes,
we will discuss classes in detail throughout the C+
+ tutorial.
class Abc
{
int i; //data variable
void display() //Member Function
{
cout << "Inside Member Function";
}
}; // Class ends here
int main()
{
Abc obj; // Creatig Abc class's object
obj.display(); //Calling member function
using class object
}
14
fi
fi
Datatypes and
Modi ers in C++
Let's start with Datatypes. They are used to de ne
type of variables and contents used. Data types
de ne the way you use storage in the programs
you write. Data types can be of two types:
1. Built-in Datatypes
2. User-de ned or Abstract Datatypes
15
fi
fi
fi
fi
fi
User de ned or Abstract data
types
These are the type, that user creates as a class or
a structure. In C++ these are classes where as in
C language user-de ned datatypes were
implemented as structures.
16
fi
fi
fi
fi
Modi ers in C++
In C++, special words(called modi ers) can be
used to modify the meaning of the prede ned
built-in data types and expand them to a much
larger set. There are four datatype modi ers in C+
+, they are:
1. long
2. short
3. signed
4. unsigned
The above mentioned modi ers can be used
along with built in datatypes to make them more
precise and even expand their range.
Below mentioned are some important points you
must know about the modi ers,
• long and short modify the maximum and
minimum values that a data type will hold.
• A plain int must have a minimum size of short.
• Size hierarchy : short int < int < long int
• Size hierarchy for oating point numbers is :
float < double < long double
• long oat is not a legal type and there are no
short oating point numbers.
• Signed types includes both positive and
negative numbers and is the default type.
17
fl
fl
fi
fl
fi
fi
fi
fi
fi
• Unsigned, numbers are always without any
sign, that is always positive.
Functions in C++
Functions are used to provide modularity to a
program. Creating an application using function
makes it easier to understand, edit, check errors
etc.
return-type function-name(parameter1,
parameter2, ...)
{
// function-body
}
18
fi
• return-type: suggests what the function will
return. It can be int, char, some pointer or
even a class object. There can be functions
which does not return anything, they are
mentioned with void.
• Function Name: is the name of the function,
using the function name it is called.
• Parameters: are variables to hold values of
arguments passed while function is called. A
function may or may not contain parameter
list.
int main()
{
int a = 10;
int b = 20;
// calling the function with name 'sum'
sum (a, b);
}
int main()
{
int a = 10;
int b = 20;
int c = sum (a, b); //calling the
function
cout << c;
}
20
fi
fi
{
return (x + y);
}
Calling a Function
21
fi
fi
fi
Call by Value
In this calling technique we pass the values of
arguments which are stored or copied into the
formal parameters of functions. Hence, the
original values are unchanged only the parameters
inside function changes.
int main()
{
int x = 10;
calc(x);
printf("%d", x);
}
void calc(int x)
{
x = x + 10 ;
}
Output : 10
22
But we can change this program to modify the
original x, by making the function calc() return a
value, and storing that value in x.
int main()
{
int x = 10;
x = calc(x);
printf("%d", x);
}
int calc(int x)
{
x = x + 10 ;
return x;
}
Output : 20
Call by Reference
23
void calc(int *p);
int main()
{
int x = 10;
calc(&x); // passing address of x as
argument
printf("%d", x);
}
Output : 20
24
fi