C++ 2
C++ 2
Report About:-
C++
1
Introduction to C++
2
Benefits of C++ over
C Language
3
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
defines its properties and its functions. Like
Human Being, having body parts, and performing
various actions.
Inheritance
5
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
Polymorphism
1. Objects
2. Classes
3. Abstraction
4. Encapsulation
5. Inheritance
7
6. Overloading
7. Exception Handling
Objects
Class
8
Abstraction
Encapsulation
9
Inheritance
Polymorphism
10
Exception Handling
11
First C++ program
#include <iostream.h>
using namespace std;
int main()
{
cout << "Hello this is C++";
}
12
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 difference is
that you do not need to mention format specifiers
like,
%d for int etc, in cout & cin.
comment
cout<<"single line"; // This is single line
/*this is
a multiple line
comment */
13
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
Datatypes and
Modifiers in C++
Let's start with Datatypes. They are used to define
type of variables and contents used. Data types
define the way you use storage in the programs
you write. Data types can be of two types:
1. Built-in Datatypes
2. User-defined or Abstract Datatypes
15
User defined 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-defined datatypes were
implemented as structures.
16
Modifiers in C++
In C++, special words(called modifiers) can be
used to modify the meaning of the predefined
built-in data types and expand them to a much
larger set. There are four datatype modifiers in C+
+, they are:
1. long
2. short
3. signed
4. unsigned
The above mentioned modifiers 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 modifiers,
• 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 floating point numbers is :
float < double < long double
• long float is not a legal type and there are no
short floating point numbers.
• Signed types includes both positive and
negative numbers and is the default type.
17
• 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
• 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
// calling the function with name 'sum'
sum (a, b);
}
Declaring, Defining
and Calling a
Function
int main()
{
int a =
10; int b
int c = sum (a, b); //calling the
function
cout << c;
}
20
//defining the function
int sum (int x, int y)
21
{
return (x + y);
}
Calling a Function
22
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
23
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
24
void calc(int *p);
int main()
{
int x = 10;
calc(&x); // passing address of x as
argument
printf("%d", x);
}
Output : 20
25