DATA STRUCTURES AND
OBJECT ORIENTED
PROGRAMMING IN C++
C++
C++ is an Object Oriented Programming
language.
Developed by Bjarne Stroustrup
Created at Bell Labs in the 1980's and called C
with Classes
A superset of C
Adds additional features to improve the language
Adds functions and features to support Object
Oriented Programming (OOP)
1.3
Introduction to C++
Where did C++ come from?
Derived from the C language
C was derived from the B language
B was derived from the BCPL language
Why the ‘++’?
++ is an operator in C++ and results in a cute
pun
C++
Traditionally, C programs use the file extension
.C, and C++ programs use the extension .CPP.
C++ compiler uses the file extension to
determine what type of program it is compiling.
A Sample C++ Program
#include <iostream> //In older C++ #include<iostream.h>
using namespace std;
int main()
{
int i;
cout << "This is output.\n"; // this is a single line comment
/* you can still use C style comments */
// input a number using >>
cout << "Enter a number: ";
cin >> i;
cout << i << " squared is " << i*i << "\n";
return 0;
}
C++ - Program Explained…
iostream file
Namespace
Variables
Output operator ( << - insertion operator)
Input operator ( >> - extraction operator)
Comment
Return type of main()
(<iostream> is to C++, is what stdio.h is to
C.)
Notice one other thing: there is no .h extension to the
name iostream. The reason is that <iostream> is one of
the new-style headers defined by Standard C++.
Newstyle headers do not use the .h extension
Explanation of code
Program statement
cout << “……..”;
cout (see-out) used for output to the monitor
“<<“ inserts “Press…a number.\n” in the data
bound for the monitor
Think of cout as a name for the monitor
“<<“ points to where the data is to end up
‘\n’ causes a new line to be started on the monitor
Explanation of code
Program statement
cin >> x;
cin (see-in) used for input from the keyboard
“>>” extracts data from the keyboard
Think of cin as a name for the keyboard
“>>” points from the keyboard to a variable where the data is stored
Basic concepts of OOPs
Objects
Classes
Data Abstraction and Encapsulation
Inheritance
Polymorphism
Dynamic binding
Message Passing
Object X Object Y
OOPs Data Data
Treats data as critical
element Communication
Functions Functions
Does not allow data to
move freely around the
program
Divides the program into
number of entities called
Object Z
Objects and
Builds the data and
functions that operates
Data
on data around the
objects.
Data of an object can
only be accessed by the
Functions
functions associated
with that object.
Basic concepts of OOPs
Object: Student
Objects : Run time
entitity. Eg. A person, Data:
place or an item. Name
Classes : Date–of-birth
is a collection of Marks
objects of similar type. ………..
Eg. Mango,apple,
orange are members of Functions:
the class fruit. Total
are user defined data Average
type. Display
………..
Data Abstraction and Encapsulation:
Wrapping up of data and functions into a single
unit (called class) is known as encapsulation.
Insulation of data from the direct access by the
program is called data hiding or information
hiding.
The act of representing essential features
without including including the background
details.
Inheritance
Isthe process by which objects of one class
acquire the properties of objects of another
class.
Provides the idea of reusability.
Polymorphism
Ability to take more than one form.
Operator overloading
Function overloading
Dynamic Binding
Binding means linking a function to be executed
in response to the function call.
Also called as late binding.
The code associated with a given function call is
not known until the time of execution of the
function (run time)
Message Passing
Objects communicate with each other as follows:
By creating classes and defining the objects
By creating objects of the type of class
Establishing the communication among the objects.
STRUCTURE OF C++ PROGRAM
Include files
Class declaration
Member functions
definitions
Main function Program
Introducing C++ Classes
In C++, to create an object, you first must
define its general form by using the keyword
class.
A class is similar syntactically to a
structure.