AN OVERVIEW OF
C++
1
OOP (Object Oriented Programming)
OOP stands for Object-Oriented Programming. Procedural
programming is about writing procedures or functions that perform
operations on the data, while object-oriented programming is about
creating objects that contain both data and functions. OOP provides a
clear structure for the programs .
What is OOP?
OOP is a powerful way to approach the
task of programming.
OOP encourages developers to decompose
a problem into its constituent parts.
Each component becomes a self-contained
object that contains its own instructions
and data that relate to that object.
So,
complexity is reduced and the
programmer can manage larger programs.
4
INTRODUCTION
C++is the C programmer’s answer to Object-
Oriented Programming (OOP).
C++ is an enhanced version of the C language.
C++adds support for OOP without sacrificing
any of C’s power, elegance, or flexibility.
Bothobject-oriented and non-object-oriented
programs can be developed using C++.
6
OOP basic concepts
Class and object
Encapsulation/ data hiding
Polymorphism
Inheritance
7
Class and Object
A class is a blueprint that defines the
variables and the methods (or function)
common to all objects of a certain kind. It
determines what an object of that type
will look like. A class is a logical entity.
An object declaration creates a physical
entity of that (class) type. That is, an
object occupies memory space, but a type
definition does not.
8
Classes contain data and functions.
9
Classes: A First Look
• General syntax -
class class-name
{
// private functions and variables
public:
// public functions and variables
} object-list (optional);
10
Encapsulation/ data hiding
Innormal terms Encapsulation is defined
as wrapping up of data and information
under a single unit. In Object Oriented
Programming, Encapsulation is defined as
binding together the data and the
functions that manipulates them.
11
Polymorphism
Polymorphism is a feature of OOPs that allows
the object to behave differently in different
conditions.
In C++ we have two types of polymorphism:
• 1) Compile time Polymorphism – This type
of is resolved during the compilation process.
This is also known as static (or early)
binding. Ex: Function overloading.
• 2) Runtime Polymorphism – Whenever an
object is bound with the functionality at run
time, this is known as runtime
polymorphism. This is also known as
dynamic (or late) binding. Ex: Function
Overriding. 12
Introducing Function Overloading
• Provides the mechanism by which C++ achieves
one type of polymorphism (called compile-time
polymorphism).
• Two or more functions can share the same name
as long as either
The type of their arguments differs, OR
The number of their arguments differs, OR
Both of the above
13
Introducing Function Overloading
(cont.)
• The compiler will automatically select the correct
version.
• The return type alone is not a sufficient difference
to allow function overloading.
14
Example
int add(int n, int m);
float add(float n, float m);
void main(){
cout<<“Summation of 10 and 20 is :”<<add(10,
20)<<endl;
cout<<“Summation of 10.5 and 20.5 is :”<<add(10.5,
20.5)<<endl;
}
int add(int n, int m){
return(n+m);
}
float add(float n, float m){
return(n+m);
}
15
Inheritance
Inheritance is a mechanism of reusing
and extending existing classes without
modifying them, thus producing
hierarchical relationships between them.
Inheritance is a process in which one
object acquires all the properties and
behaviors of its parent object
automatically. In such way, you can reuse,
extend or modify the attributes and
behaviors which are defined in other class.
16
Type of Inheritance
1. Single inheritance.
2. Multiple inheritance.
3. Hierarchical inheritance.
4. Multilevel inheritance.
5. Hybrid inheritance.
17
C++ Program Structure
18
C++ Program Structure
A modern-style C++ program that uses the new-
style headers and a namespace -
#include <iostream>
using namespace std;
int main()
{
/* program code */
return 0;
19
}
The New C++ Headers
The new-style headers do not specify filenames.
They simply specify standard identifiers that might
be mapped to files by the compiler, but they need
not be.
<iostream>
<vector>
<string>, not related with <string.h>
<cmath>, C++ version of <math.h>
<cstring>, C++ version of <string.h>
Programmer defined header files should end in “.h”.
20
C++ Console I/O (Output)
cout << “Hello World!”;
printf(“Hello World!”);
cout << iCount; /* int iCount */
printf(“%d”, iCount);
cout << 100.99;
printf(“%f”, 100.99);
cout << “\n”, or cout << ‘\n’, or cout << endl
printf(“\n”)
In general, cout << expression 21
C++ Console I/O (Input)
cin >> strName; /* char strName[16] */
scanf(“%s”, strName);
cin >> iCount; /* int iCount */
scanf(“%d”, &iCount);
cin >> fValue; /* float fValue */
scanf(“%f”, &fValue);
cin >> day >> month >> year;
cin >> day;
cin >> month;
cin >> year
In general, cin >> variable; 22
finding the area of a circle using class and
object in C++.
(self study)
• Difference between POP and
OOP
• Difference between C and C++