CPP - Module 1 - Part A
CPP - Module 1 - Part A
Module I
Kavya Kishore
Asst.Professor, Dept.of
BCA
St. Teresas College
Introduction to C++
C++ was developed by Bjarne Stroustrup,
as an extension to the C language.
C++ gives programmers a high level of
control over system resources and memory.
C++ is an object-oriented programming
language which gives a clear structure to
programs and allows code to be reused,
lowering development costs.
C++ is portable.
Evolution of Programming Methodology
Procedure Oriented Programming
Object Oriented Programming
Comparison of Procedure Oriented and Object Oriented Programming
POP (OOP)
1. Emphasis is on doing things 1. Emphasis is on data rather than procedure,
not on data, means it is function means object driven
driven
2. Main focus is on the function 2. Main focus is on the data that is being
and procedures that operate on operated
data
3. Top Down approach in 3. Bottom Up approach in program design
program design
4. Large programs are divided 4. Large programs are divided into classes
into smaller programs known as and objects
functions
5. Most of the functions share 5. Data is tied together with function in the
global data
data structure
6. Data moves openly in the 6. Data is hidden and cannot be accessed by
system from one function to external functions
another function
7. Adding of data and function is 7. Adding of data and function is easy
difficult
8. We cannot declare namespace 8. We can use name space directly, Ex: using
directly
namespace std;
9. Concepts like inheritance, 9. Concepts like inheritance, polymorphism, data
polymorphism, data encapsulation, abstraction, access specifiers are
encapsulation, abstraction, access available and can be used easily
specifiers are not available.
10. Examples: C, Fortran, Pascal, 10. Examples: C++, Java, C#, etc…
etc…
Merits and Demerits of OOP
Basic Concepts of OOPs
Object
Class
Data Abstraction
Encapsulation
Inheritance
Polymorphism
Dynamic Binding
Message Passing
Object
An object is an instance of a class.
An object means anything from real world like as
person, computer, bench etc...
An object is a component of a program that knows
how to interact with other pieces of the program.
For example, If Automobile is a class then car is
an object.
Object
Class
A class is a template that specifies the attributes
and behavior of things or objects.
A class is a blueprint or prototype from which
objects are created.
A class is a collection of objects, that contain
both data and functions.
Data Abstraction
It refers to the act of representing essential
features without including background details
and explanations.
Provides data security
Encapsulation
The wrapping up of data and functions
into a single unit (called class) is known
as encapsulation.
class Student
{
private :
char name[20];
int rollno;
float marks;
public:
void getdetails();
void putdetails();
};
int main()
{
Student s;
s.getdetails();
s.putdetails();
return 0;
}
Encapsulation
Inheritance
It is the process by which objects of one class
acquires the properties and behavior of another
class.
The existing class is called the base class and
new class is called the derived class.
Types of Inheritance
Polymorphism
The Greek word Polymorphism means the ability
to take more than one form.
Polymorphism refers to the ability of a C++
function or object to perform in different ways,
depending on how the function or object is used.
Polymorphism
Types of polymorphism
Compile time (Static) polymorphism (or early
binding): Polymorphism during and compilation.
Function overloading and operator overloading are
examples.
Run time (Dynamic) polymorphism (or late binding):
Polymorphism during run-time. It uses pointers. Virtual
functions are examples.
Function Overloading: Functions with the same name,
but different signatures can act differently. Eg: The
function prototypes int area (int, int); and int area(int);
shows that area() is an overloaded function.
Operator overloading: It is the process of giving new
meaning to an existing C++ operator.
Static Binding/Late Binding
If linking between method call and method
implementation is resolved at compile time
then we call it static binding or if it is resolved
at run time then it is dynamic binding.
Message Passing
A program contains set of object that communicates
with each other.
Creating classes that define objects and their behavior.
Creating objects from class definition
Establishing communication among objects.
Message passing involves the object name, function
name and the information to be sent.
Structure of a C++ program
Sample C++ program
Structure of a C++ program (Contd..)
Pre-processors: These are the compiler
directive statements which give instruction to
the compiler to process the information
provided before actual compilation starts.
These lines always start with a # (hash)
symbol.
Header files: Files available along with
compiler and they are kept in the standard
library. The header files contain the
information about functions, objects and
predefined derived data types.
Structure of a C++ program (Contd..)
using namespace statement: It tells the compiler about a
namespace where it should search for the elements used in
the program. In C++, std is an abbreviation of 'standard'
and it is the standard namespace in which cout, cin and a
lot of other things are defined. So, when we want to use
them in a program, we need to follow the format std::cout
and std::cin. This kind of explicit referencing can be
avoided with the statement using namespace std; in the
program.
40
41
Primitive data types are the basic data types
( int, char, float, double).
42
int
These are the whole numbers, both positive
and negative.
The keyword used to define integers is int
An example of an integer value is 32.
An example of declaring an integer variable
called sum is,
int sum;
sum = 20;
43
float
44
double
45
char
These are single characters.
letter is
char letter;
letter='A';
Note the assignment of the character A to the
variable letter is done by enclosing the value in single
quotes.
46
boolean
Value of x will be 7
47
Data type qualifiers
48
Size qualifiers
Size qualifiers alters the size of a basic type. There
Two types
1. Signed
•can store both positive and negative values
2. unsigned
•can store only positive values
short int a=50000;
50
User defined type declaration
A user can define an identifier that
represents an existing data type.
52
Example:
typedef int salary;
typedef float average;
53
Enumerated data type
The second type of user defined data type is
enumerated data type.
Consists of integral constants.
Keyword used is enum
54
Example:
enum day {Monday, Tuesday, …. Sunday};
enum rainbow{Violet, Indigo, Blue, Green,
Yellow, Orange, Red};
By default Monday is 0,Tuesday is 1 and so on…
• Binary operator
+-*/%
• Ternary operator (conditional operator ?)
condition? true_value: false_value;
Eg. result=(A>100 ? 10:20);
58
Types of Operators
Arithmetic operators
Assignment operators
Relational operators
Logical operators
Bit wise operators
Conditional operators (ternary operators)
Increment/decrement operators
59
60
Arithmetic operators
61
Relational operators
62
Logical operators
63
Logical operators
64
Assignment operator
65
66
Conditional operator
Syntax:
68
69
++i means increment i then use it (Prefix notation)
notation)
70
Bitwise operators
& BitwiseAND
| BitwiseOR
~ BitwiseNOT
^ Bitwise XOR
<< LeftShift
>> RightShift
71
72
Size of Operator
Size of operator is a unary operator, which gives
75
Statements
Two types :
Simple statements
Eg: a=b+c;
Compound statements
temp=a;
a=b;
b=temp; }
76
Declaration Statements
77
Input statements
cin - Input statements
Standard input stream
Variable cin is predefined to denote an input
stream from the standard input device (the
keyboard)
The extraction operator >> is called “get
from”
78
Syntax: cin >> Variable1 >> Variable2 . . . ;
79
Output statements
80
Syntax: cout << Expression << Expression . . . ;
81
Selection Statements
Simple if
if-else
if else if ladder
Nested if
switch
82
Simple if
if (condition)
{
statement block;
}
83
Simple if - Example
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<”\n Enter the age”;
cin>> age;
if (age>=18)
{
cout<<”You are eligible to vote”;
}
return 0;
} 84
if - else
if (condition)
{
statement block 1;
}
else
{
statement block 2;
}
85
if else if ladder
if (condition 1)
statement block 1;
else if (condition 2)
statement block 2;
else if (condition 3)
statement block 3;
...............
else
statement block n;
86
switch
switch(expression)
{
case constant_1 : statement block 1;
break;
case constant_2 : statement block 2;
break;
case constant_3 : statement block 3;
break;
:
:
case constant_n-1 : statement block n-1;
break;
default : statement block n;
87
}