Unit-01
Fundamentals of Object Oriented
Programming
- Charushila D. Patil
Ass. Professor, Computer
GCOERC, Nashik
Differentiate between Procedure Oriented Programming & Object
Oriented Programming languages.
S.N POP OOP
1 Programs are divided into functions. Programs are divided into objects.
2 It follows top-to bottom approach. It follows bottom to top approach.
3 Data is not secure. It does not Data is secure. It provide access
provide access specifiers like private, specifiers like private, public or
public or protected. protected.
4 Reusability of code is not possible Reusability of code is possible using
using Inheritance. Inheritance.
5 Addition of new code, modification Addition of new code, modification
,testing of code is complicated. ,testing of code is easier due to modular
programming.
State Object Oriented Programming languages.
C++
SmallTalk
Simula
Java
Eiffel
Pascal
Ada
Introduction of C++
C++ is an object-oriented programming language.
It was developed in 1980’s.
The developers and strong supporter of C, wanted to combine the
best of both the languages and create a more powerful language that
could support object-oriented programming features and still retain
the power and elegance of C. The result was C++.
Therefore, C++ is an extension of C
C+ + is a superset of C. Almost all c programs are also C++ programs.
However, there are a few minor differences that will prevent a c
program to run under C++ complier.
The most important facilities that C++ adds on to C care classes,
inheritance, function overloading and operator overloading.
These features enable creating of abstract data types, inherit
properties from existing data types and support polymorphism,
thereby making C++ a truly object-oriented language.
Application of C++
• C++ is a versatile language for handling very large
programs.
• development of editors, compilers, databases,
communication systems and any complex real life
applications systems is possible .
• C++ allow us to create hierarchy related objects, we can
build special object-oriented libraries which can be used
later by many programmers.
• C++ programs are easily maintainable and expandable.
• When a new feature needs to be implemented, it is very
easy to add to the existing structure of an object.
• C++ is able to map the real-world problem properly.
gives the language the ability to get closed to the
machine-level details
Explain structure of C program.
Documentation section
Link Section
Global Declaration Section
Class declaration
{
member function definition
};
member function definition
main( ) function section
{ declaration part;
Executable part;
}
Documentation section:
The documentation section consists of a set of comment lines giving the name of the program,
the author and other details, which the programmer would like to use later.
Link section :
The link section provides instructions to the compiler to link functions from the system library
such as using the #include directive.
Global declaration section:
There are some variables that are used in more than one function. Such variables are called
global variables and are declared in the global declaration section that is outside of all the
functions.
Class Declaration :
The class declaration part declares all the variables ,functions used
in the executable part .Member function body can be written inside or outside class.
'main' function
main( ) function is the entry point of any C++ program. It is the point at which execution of
program is started.
Here object of class is created.
Members of class(variable ,functions) can be accessed here using object only.
Subprogram section:
If the program is a multi-function program then the subprogram
section contains all the user-defined functions
that are called in the main 0 function. User-defined functions are
generally placed immediately after the main () function, although
they may appear in any order.
Header files:
A header file is a file with extension .h which contains C function
declarations and macro definitions to be shared between several source
files.
Include Syntax
Both the user and the system header files are included using the
preprocessing directive #include.
Simple C++ Program
// Printing A String
#include<iostream.h>
Using namespace std;
int main()
{
cout<<” c++ is better than c \n”;
return 0;
}
Write Simple C++ Program to read two numbers from user and
display addition.
#include<iostream.h>
#include<conio.h>
void main() //writing main() function
{ //start of main() function
int a,b,c;
cout<<“Enter two numbers”;
//display statement
cin>>a>>b;
c=a+b;
cout<<“Addition is: “<<c;
getch(); //return to program
} //end of main() function
• The double slash comment is basically a single
line comment.
• Multiline comments can be written as follows:
// This is an example of
// C++ program to illustrate
// some of its features
multiline comments:
/* This is an example of
C++ program to illustrate
some of its features */
Write input/ output statement in c++ with syntax & Example.
Input statement
cin - It is a statement used to read data from user with “>>”(Extraction) operator or get
operator. Object Extruction operator Variable
Syntax-. cin>>variable name; cin >> no1
Eg: int n;
cin>>n;
Output statement Keyboard
cout- It is a statement used to display output or messageFig:
toInput
user using Extraction operator
on screen. It uses “<<“
(Insertion) operator/put to operator.
Syntax – cout<<"message”;
or
cout<<"message”<<variable name;
Eg- int n;
cout<<“Enter number:”;
cin>>n; Screen
cout<<“Number is: “<<n;
cout << “Welcome”
Object Insertion operator Variable
Fig: Input using Insertion operator
Output operator
• Cout<<”C++ is better than C.”;
• Causes the string in quotation marks to be
displayed on the screen.
• Two new C++ features:
cout <<
The iostream File
• The #include directive instructs the compiler to include the contents
of the file enclosed within angular brackets into the source file.
• The header file iostream.h should be included at the beginning of all
programs that use input/output statements.
• Namespace:
• new concept introduced by the ANSI C++
• This defines a scope for the identifiers that are used in a program.
• For using the identifier defined in the namespace scope we must
include the using directive, like
• Using namespace std;
• Here, std is the namespace where ANSI C++ standard class libraries are
defined. All ANSI C++ programs must include this directive. This will
bring all the identifiers defined in std to the current global scope.
Using and namespace are the new keyword of C++.
Return Type of main()
• main () returns an integer value to the operating
system.
• Therefore, every main () in C++ should end with a
return (0) statement; otherwise a warning an error
might occur.
• main () returns an integer type for main () is
explicitly specified as int.
• Note :
The default return type for all function in C++
is int.
AVERAGE OF TWO NUMBERS
#include<iostream.h> // include header file
Using namespace std;
Int main()
{
Float number1, number2,sum, average;
Cin >> number1; // Read Numbers
Cin >> number2; // from keyboard
Sum = number1 + number2;
Average = sum/2;
Cout << ”Sum = “ << sum << “\n”;
Cout << “Average = “ << average << “\n”;
Return 0;
} //end of example
The output would be: Enter two numbers: 6.5
7.5
Sum = 14
Average = 7
Variables :
number1, number2, sum and average.
They are declared as type float.
float number1, number2, sum, average;
All variable must be declared before they are used
in the program.
Input Operator :
cin >> number1
The number keyed in is placed in the variable
number1.
The identifier cin (pronounced ‘C in’) is a
predefined object in C++ that corresponds to the
standard input stream
• The operator >> is known as extraction or get
from operator.
• It extracts (or takes) the value from the
keyboard and assigns it to the variable on its
right.
• Like <<, the operator >> can also be
overloaded.
C++ program with Class
#include<iostream.h> // include header file
using namespace std;
class person
{
char name[30];
int age;
public:
void getdata(void);
void display(void);
};
void person :: getdata(void)
{
cout << “Enter name: “;
cin >> name;
cout << “Enter age: “;
cin >> age;
}
Void person : : display(void)
{
cout << “\nNameame: “ << name;
cout << “\nAge: “ << age;
}
Int main()
{
person p;
[Link]();
[Link]();
Return 0;
} //end of example
Structures in C++
• We often come around situations where we need to
store a group of data whether of similar data types
or non-similar data types. We have seen Arrays in
C++ which are used to store set of data of similar
data types at contiguous memory locations.
• Unlike Arrays, Structures in C++ are user defined
data types which are used to store group of items
of non-similar data types.
• What is a structure?
• A structure is a user-defined data type in C/C++. A
structure creates a data type that can be used to
group items of possibly different types into a single
type.
How to create a structure?
The ‘struct’ keyword is used to create a structure. The
general syntax to create a structure is as shown
below:
struct structureName
{ member1;
member2;
member3; . . .
memberN; };
eg. struct student
{ int rollno;
char name[20];
float percentage;
}
• Structures in C++ can contain two types of
members:
• Data Member: These members are normal C++
variables. We can create a structure with
variables of different data types in C++.
• Member Functions: These members are
normal C++ functions. Along with variables, we
can also include functions inside a structure
declaration.
// Data Members
int roll;
int age;
int marks;
// Member Functions
void printDetails()
{
cout<<"Roll = "<<roll<<"\n";
cout<<"Age = "<<age<<"\n";
cout<<"Marks = "<<marks;
}
How to declare structure variables?
• A structure variable can either be declared with
structure declaration or as a separate declaration like
basic types.
// A variable declaration with structure declaration.
struct Point
{
int x, y;
} p1; // The variable p1 is declared with 'Point'
// A variable declaration like basic data types
struct Point
{
int x, y;
};
int main()
{
struct Point p1; // The variable p1 is declared like a normal variable
}
How to initialize structure members?
Structure members cannot be initialized with
declaration. For example the following C program fails
in compilation.
Bu
struct Point
{
int x = 0; // COMPILER ERROR: cannot initialize
members here
int y = 0; // COMPILER ERROR: cannot initialize
members here
};
Note : The reason for above error is simple, when a datatype is
declared, no memory is allocated for it.
Memory is allocated only when variables are created.
Structure members can be initialized using curly braces ‘{}’. For
example, following is a valid initialization.
struct Point
{
int x, y;
};
int main()
{
// A valid initialization. member x gets value 0 and y
// gets value 1. The order of declaration is followed.
struct Point p1 = {0, 1};
}
How to access structure elements?
Structure members are accessed using dot (.) operator.
#include <iostream>
using namespace std;
struct Point {
int x, y;
};
int main()
{
struct Point p1 = { 0, 1 };
// Accessing members of point p1
p1.x = 20;
cout << "x = " << p1.x << ", y = " << p1.y;
return 0;
}
Output : x = 20, y = 1
Programs
on word files
Enumerations
• Enum is a user defined data type where we specify a set of values for a
variable and the variable can only take one out of a small set of possible
values. We use enum keyword to define a Enumeration.
• enum direction {East, West, North, South}dir;
• Here Enumeration name is direction which can only take one of the four
specified values, the dir at the end of the declaration is an enum variable.
• #include<iostream>
• using namespace std;
• enum direction {East, West, North, South}dir;
• int main()
• { dir = West; by default the values are in
• cout<<dir; increasing order starting
• return 0; from 0, which means East is
• } 0, West is 1, North is 2 and
South is 3.
o/p :1
• enum direction {East, West, North, South};
• int main()
• { direction dir;
• dir = South;
• cout<<dir;
• return 0;
• }
• What will be output ???
3
How to change default values of Enum
• #include <iostream>
• using namespace std;
• enum direction {East=11, West=22, North=33,
South=44};
• int main()
• { direction dir;
• dir = South;
• cout<<dir;
• return 0; }
• o/p : 44
Why use enum in C++
• Enums are used only when we expect the variable
should have one of the possible set of values.
• E.g. dir variable holds the direction.
• Since there are four directions, dir variable can
take any one of the four values,
• Summery :
An enumeration is a user-defined data type that
consists of integral constants. To define an
enumeration, keyword enum is used.
enum season { spring, summer, autumn, winter };
Name of enumeration values
• By Default
• Spring=0, summer=1, autumn=2, winter =3
• You can change the default value of an enum element
during declaration
• enum season
• { spring = 0,
• summer = 4,
• autumn = 8,
• winter = 12 };
• Refer file1
•
Advantages of enum
• t can be used in switch case.
• It improves type safety.
• It can have fields, constructors and methods.
• It can implement many interfaces but cannot
extend any class.
• It can be traversed.