0% found this document useful (0 votes)
67 views116 pages

Oop Unit 1

Uploaded by

Arjun Bhosale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views116 pages

Oop Unit 1

Uploaded by

Arjun Bhosale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 116

UNIT-I

Principles of Object
Oriented Programming
Introduction
• C++ is an object oriented programming language
• It is developed by Bjarne Stroustrup at AT&T Bell
Laboratories in Murry Hill, New Jersey, USA in the
early 1980's.
• C++ is an extension of C with major addition of the
Class feature.
• Class was a major addition to the original C language.

• Stroustrup initially called the new language


'C with Classes'

• Later in 1983, the name was changed to C++.

• The Most important facilities that C++ adds to C are


Classes, Inheritance, Function Overloading and
Operator Overloading.
Difference between procedure
oriented language(C) and object
oriented language(C++)
The major difference between C and C++ is that C
is a procedural programming language and does
not support classes and objects, while C++ is a
combination of both procedural and object
oriented programming language; therefore C++
can be called a hybrid language.
(1) C was developed by Dennis Ritchie between
1969 and 1973 at AT&T Bell Labs.

C++ was developed by Bjarne Stroustrup in 1979


with C++'s predecessor "C with Classes".

(1) When compared to C++, C is a subset of C++.

C++ is a superset of C. C++ can run most of C


code while C cannot run C++ code.
(3)
C supports procedural programming paradigm for
code development.

C++ supports both procedural and object oriented


programming paradigms.

(4)
C does not support object oriented programming;
therefore it has no support for polymorphism,
encapsulation, and inheritance.

Being an object oriented programming language


C++ supports polymorphism, encapsulation, and
inheritance.
(5)
In C data and functions are separate and free
entities.

In C++ data and functions are encapsulated


together in form of an object.

(6)
C does not support information hiding.

In C++, Encapsulation hides the data.


(7)
C, being a procedural programming, it is a
function driven language.

While, C++, being an object oriented


programming, it is an object driven language.

(8)
C does not support function and operator
overloading.

C++ supports both function and operator


overloading.
(9)
Inline functions are not supported in C.

In C++ on the other hand, inline functions are


used.

(10)
C uses functions for input/output.
For example scanf and printf.

C++ uses objects for input output.


For example cin and cout.
(11)
C has no support for virtual and friend functions.

C++ supports virtual and friend functions.

(12)
C provides malloc() and calloc() functions for
dynamic memory allocation, and free() for memory
de-allocation.

C++ provides new operator for memory allocation


and delete operator for memory de-allocation.
Basic Concepts of Object Oriented
Programming
1) Objects
2)Classes
3)Data Abstraction
4)Encapsulation
5)Inheritance
6)Polymorphism
7)Dynamic Binding
8)Message Passing
Object
● Objects are basic run-time entities in an object
oriented system
● Objects are instances of a class
● It is user defined data types.
● Practically each and every living and nonliving
thing is an object
● Any entity that has state and behavior is known
as an object.
● For example: chair, pen, table, keyboard, bike
etc.
● Object take up space in memory and have an
associated address.
ex:
class person
{
char name[20];
int id;
public:
void getdetails() { }
};

void main()
{
person p1; //p1 is a object
}
Class
• Collection of objects is called class.
• Class is a blueprint of data and functions or
methods.
• Class does not take any space.
• Class is a user defined data type like structures
and unions in C.
• By default class variables are private.
syntax for class:-
class class_name
{
private:
//data members and member functions declarations
public:
//data members and member functions declarations
protected:
//data members and member functions declarations
};
Data Abstraction
• Data abstraction refers to, providing only
essential information to the outside world and
hiding their background details, i.e., to represent
the needed information in program without
presenting the details.

• For example, a database system hides certain


details of how data is stored and created and
maintained. Similar way, C++ classes provides
different methods to the outside world without
giving internal detail about those methods and
data.
• Consider a real life example of a man driving a
car. The man only knows that pressing the
accelerators will increase the speed of car or
applying brakes will stop the car but he does not
know about how on pressing accelerator the
speed is actually increasing, he does not know
about the inner mechanism of the car or the
implementation of accelerator, brakes etc in the
car. This is what abstraction is.

● Example--filling online form, essential


information is shown to user and its background
details are hidden from user
Encapsulation
• Wrapping up of data and functions into a single
unit(called class) is known as encapsulation.
• The data is not accessible to the outside world
and only those functions which are wrapping in
the class can access it.
• This insulation of the data from direct access by
the program is called data hiding or information
hiding.
Inheritance
• One of the most useful aspects of object-
oriented programming is code reusability.
• As the name suggests Inheritance is the
process of forming a new class from an existing
class
• the existing class called as base class, new
class is called as derived class.
• This is a very important concept of object-
oriented programming since this feature helps to
reduce the code size.
• Inheritance is a way to reuse once written code
again and again. The class which is inherited is
called base calls & the class which inherits is
called derived class.

• So when, a derived class inherits a base class,


the derived class can use all the functions which
are defined in base class, hence making code
reusable.
Polymorphism
• polymorphism means ability to take more than
one form.
• An operation may exhibit different behaviors in
different instances. The behavior depends upon
the types of data used in the operation.
• C++ supports operator overloading and function
overloading.
• Operator overloading is the process of making
an operator to exhibit different behaviors in
different instances is known as operator
overloading.
• Function overloading is using a single function
name to perform different types of tasks.
Dynamic Binding

•In dynamic binding, the code to be executed in


response to function call is decided at runtime.
•C++ provides facility to specify that the compiler
should match function calls with the correct
definition at the run time; this is called dynamic
binding or late binding or run-time binding.
•Dynamic binding is achieved using virtual
functions.
Message Passing
• The process of programming in OOP involves
foll steps.
1) Creating Classes
2) Creating Objects
3) Establishing communication among objects.
• Objects communicate with one another by
sending and receiving information, same as
people pass message to one another.
• Objects communicate with one another by
sending and receiving information to each other.
• A message for an object is a request for
execution of a function that generates the
desired results.
• Message passing involves specifying the name
of the object, the name of the function and the
information to be sent.
Employee.salary(name);
• Here employee is object, salary is message
and name is information
Object oriented programming
languages
• C++
• Smalltalk Simula
• Object Pascal
• Eiffel
• Java
APPLICATIONS OF OOPS
• Real-time system
• Simulation and modeling
• Object-oriented data bases
• Hypertext, Hypermedia, and expertext
• AI and expert systems
• Neural networks and parallel programming
• Decision support and office automation
systems
• CIM/CAM/CAD systems
Structure of C++ program
The structure of C++ program is divided into
four different sections:
(1) Header File Section
(2) Class Declaration section
(3) Member Function definition section
(4) Main function section
(1) Header File Section:

---This section contains various header files.


---You can include various header files in to your
program using this section.

For example:
# include <iostream.h >
---Header file contains declaration and definition
of various built in functions as well as object. In
order to use this built in functions or object we
need to include particular header file in our
program.
(2) Class Declaration Section:

---This section contains declaration of class.


---You can declare class and then declare data
members and member functions inside that class
----You can also inherit one class from another
existing class in this section.
For example:
class Demo
{
int a, b;
public:
void input();
void output(){…….}
};
(3) Member Function Definition Section:
---This section is optional in the structure of C++
program.
---Because you can define member functions inside
the class or outside the class. If all the member
functions are defined inside the class then there is no
need of this section.
---This section is used only when you want to define
member function outside the class.
---This section contains definition of the member
functions that are declared inside the class.
For example:
void Demo:: input ()
{
cout << ―Enter Value of A:‖;
cin >> a;
cout << ―Enter Value of B:‖;
cin >> b;
}
(4) Main Function Section:

----In this section you can create an object of the


class and then using this object you can call various
functions defined inside the class as per your
requirement.
For example:
Void main ()
{
Demo d1;
d1.input ();
d1.output ();
}
Comments

Comments are a vital element of a program that is


used to increase the readability of a program and to
describe its functioning. Comments are not
executable statements and hence, do not increase the
size of a file.

C++ supports two comment styles: single line


comment and multiline comment.
Single line comments are used to define line-by-line
descriptions. Double slash (//) is used to represent
single line comments. To understand the concept of
single line comment, consider this statement.

/ / An example to demonstrate single line


comment It can also be written as
/ / An example to demonstrate
/ / single line comment
---Multiline comments are used to define multiple
lines descriptions and are represented as / * * /.
For example, consider this statement.

/* An example to demonstrate
multiline comment */
Tokens
• A token is the smallest element of a C++
program that is meaningful to the compiler.
• Different kinds of tokens: identifiers, keywords,
strings, constants, operators.
Keyword

It is reserved identifiers and cannot be used as


names for the program variables or other user
defined program elements.
const goto short virtual
continue if signed void
default inline sizeof volatile
delete int static while
do long struct
catch float public typedef
char for register union
class friend return unsigned
asm double new switch
auto else operator template
break enum private this
case extern protected try
Constants
Constants can be defined as the expression with
fixed values.
Data types in C++
Built-in Data Types
1) int
2) float
3) char
4) void
Derived Data types--
C++ also permits four types of derived data types.
As the name suggests, derived data types are
basically derived from the built-in data types.
There are three derived data types. These are:

1) Array
2) Function
3) Pointer
User Defined Data Types--
C++ also permits four types of user defined data
types. As the name suggests, user defined data
types are defined by the programmers during
the coding of software development. There are
four user defined data types. These are:
1) Structure
2) Union
3) Class, and
4) Enumerator
Identifiers

• A symbolic name is generally known as an


identifier.
• valid identifiers are a sequence of one or more
letters, digits or underscore characters (_).
• Neither spaces nor punctuation marks or
symbols can be part of an identifier. Only letters,
digits and single underscore characters are
valid.
The rules for the formation of identifier can be
summarized as;

• An identifier may include of alphabets, digits


and/or underscores. It must not start with a digit.
• C++ is case sensitive, i.e, upper case and
lower case letters are considered totally different
from each other. It may be noted that TOTAL
and total are two different identifier names.
• It should not be a reserved word.
Different types of Operators.
(1) Arithmetic Operators

(2) Relational Operators

(3) Logical Operators

(4) Assignment Operators


(5) Increments and Decrement Operators

(6) Bitwise Operators


Arithmetic Operators
Relational Operators :
Logical Operators
1)Logical AND (&&) :
a > b && x = = 10
The expression to the left is a > b and that on the
right is x == 10 the whole expression is true only
if both expressions are true i.e., if a is greater
than b and x is equal to 10.
2) Logical OR (||) : Example :
a < m || a < n

The expression evaluates to true if any one of


them is true or if both of them are true. It
evaluates to true if a is less than either m or n
and when a is less than both m and n.

3)Logical NOT (!) :For example :


! (x >= y)
The NOT expression evaluates to true only if the
value of x is neither greater than or equal to y.
2) Logical OR (||) : Example :
a < m || a < n

The expression evaluates to true if any one of


them is true or if both of them are true. It
evaluates to true if a is less than either m or n
and when a is less than both m and n.

3)Logical NOT (!) :For example :


! (x >= y)
The NOT expression evaluates to true only if the
value of x is neither greater than or equal to y.
Assignment Operators
Example : x + = 1 is same as x = x + 1
The commonly used shorthand assignment
operators are as follows:
Bitwise operators--

^ (XOR)
| (OR)
& (AND)
~ (complement)
<< (shift left, insertion to stream)
>> (shift right, extraction from stream)
Increment and Decrement Operators :
--C offers two unusual and unique operators ++
and — called increment and decrement
operator respectively.
--The operators can be place either before or after
the operand.
--If the operator is placed before the variable (++I
or –I) it is known as pre incrementing or pre
decrementing.
--If the operator is placed after the variable (I++ or
I–) it is known as post incrementing or post
decrementing.
In the pre increment or pre decrement first the
value of operand is incremented or
decremented then it is assigned.

Example:
x=100;
y=++x;
After the execution the value of y will be 101 and
the value of x will be 100.
In the post increment or post decrement first the
value of operand is assigned to the concern
variable and then the increment or decrement
perform.

Example:
x=100;
Y=x++;
After the execution the value of y will be 100 and
the value of x will be 101.
Scope resolution operator in C++
In C++, scope resolution operator is ::. It is used
for following purposes.
1) To define a function outside a class.
2) To access a class’s static variables.
3) In case of multiple Inheritance.
Memory management operators
• To avoid wastage of memory, you can
dynamically allocate the memory required during
runtime using new and delete operator.
• New operator--The new operator in C++ is
used for dynamic storage allocation. This
operator can be used to create object of any
type.
• Delete operator--The delete operator in C++ is
used for releasing memory space when the
object is no longer needed.
Structure of C++ Program
The structure of C++ program is divided into four
different sections:
(1) Header File Section
(2) Class Declaration section
(3) Member Function definition section
(4) Main function section
(1) Header File Section:

• This section contains various header files.


• You can include various header files in to your
program using this section.

For example:
# include <iostream.h >
(2) Class Declaration Section:
• This section contains declaration of class.
• You can declare class and then declare data
members and member functions inside that
class.
• You can also inherit one class from another
existing class in this section.
For example:
class Demo
{
int a, b;
public:
void input();
void output();
}
(3) Member Function Definition Section:
• Because you can define member functions
inside the class or outside the class. If all the
member functions are defined inside the class
then there is no need of this section.
• This section is used only when you want to
define member function outside the class.
• This section contains definition of the member
functions that are declared inside the class.
For example:
void Demo:: input ()
{
cout << ―Enter Value of A:‖;
cin >> a;
cout << ―Enter Value of B:‖;
cin >> b;
}
(4) Main Function Section:
• In this section you can create an object of the
class and then using this object you can call
various functions defined inside the class as per
your requirement.

For example:
Void main ()
{
Demo d1;
d1.input ();
d1.output ();
}
Input Statement (cin)
• In C++ cin is known as an input statement.
• It is used to input data into the program.
• The general form of the cin is as follow:
cin>>Variable_Name;
Here,
• cin is a predefined object defined in istream.h
file which represents the standard input stream
in C++ which is keyboard .
• >> is known as extraction or get from operator.
Because it gets the value from the keyboard and
assign it to the variable on it’s right.
Variable_Name is the name of the variable to
which you want to assign the entered value.
Example:
cin>>a;

We can also cascade >> operator to enter


multiple values using single cin statement.

Example:
Cin>>a>>b;
Extraction Operator(>>)--
• Include <iostream.h> header file to use cin.
• cin is used for accepting data from the keyboard.
• The operator >> called as extraction operator or get
from operator.
• The extraction operator can be overloaded.
• Extraction operator is similar to the scanf() operation
in C.
• cin is the object of istream class.
• Data flow direction is from input device to variable.
• Due to the use of input statement, the program will
wait till the user type some input and that input is
stored in variable.
• Multiple inputs can be accepted using cin.
Output Statement (cout)
• In C++ cout is known as an output statement.
• It is used to display the content of the variable
or string on the output screen.
• The general form of cout statement is follow:
cout << Variable_Name or ―String‖;
Here,
• cout is a predefined object defined in ostream.h
which represents the standard output stream in
C++ which is monitor.
• << is an overloaded bitwise shift left operator. It
is also known as insertion or put to operator. It
inserts the contents of the variable on its right to
the object on its left.
• Variable_Name or ―String‖ represents the value
to be displayed on the screen.
Example:
cout<<a;

We can also cascade << operator to display


multiple values using single cout statement.

Example:
Cout<<a<<b;
cout<<―Addition=‖<<sum;
Insertion Operator(<<)--

• Include <iostream.h> header file to use cout.


• cout is used for displaying data on the screen.
• The operator << called as insertion operator or put to
operator.
• The Insertion operator can be overloaded.
• Insertion operator is similar to the printf() operation in
C.
• cout is the object of ostream class.
• Data flow direction is from variable to output device.
• Multiple outputs can be displayed using cout.
Header Files in C++
1) conio.h--if we use getch() and clrscr() in C++
program
2) iostream.h-- if you use cin and cout

Syntax--
#include<conio.h>
#include<iostream.h>
#include<math.h>
//Find addition of 4 and 5
#include<iostream.h>
#include<conio.h>
void main(){
int n1,n2,sum;
n1=4;
n2=5;
sum=n1+n2;
cout<<―Addition=‖<<sum;

getch();
}
//WAP to read three integers and find their mean
#include<iostream.h>
#include<conio.h>
void main(){
int a,b,c,mean;
clrscr();
cout<<‖Enter three integer values:‖;
cin>>a>>b>>c;
mean=(a+b+c)/3;
cout<<‖\n Mean of three integers=‖<<mean;
getch();
}
Enter three integer values:2 4 6
Mean of three integers=4
O/p--
Enter three integer values:4
5
2
Mean of three integers=3.66
//wap to read two numbers from the keyboard and
//display the larger value on the screen
#include<iostream.h>
#include<conio.h>
Void main(){
Int n1,n2;
Clrscr();
Cout<<‖\n Enter two numbers‖;
cin>>n1>>n2;
if(n1>n2)
cout<<‖\n The largest number is‖<<n1;
Else
cout<<‖\n The largest number is‖<<n2;
Getch();
}
O/p--
Enter two numbers15 63
The largest number is 63
/*wap to input an integer value from keyboard and
display on the screen ―WELDONE‖ that many
times*/
#include<iostream.h>
#include<conio.h>
Void main(){
Int i,n;
Clrscr();
Cout<<‖\n Enter number‖;
cin>>n;
for(i=0;i<n;i++)
Cout<<‖WELDONE‖;
Getch();
}
O/p--
Enter number4
WELDONE
WELDONE
WELDONE
WELDONE
/* WAP TO CALCULATE SIMPLE INTEREST
USING FORMULA SI=P*N*R/100 where p-
principal amount, n-number of years, r-rate of
interest*/
#include<iostream.h>
#include<conio.h>
Void main(){
Int p,n,r,si;
Clrscr();
Cout<<‖\n Enter valus for p,n,r‖;
cin>>p>>n>>r;
si=(p*n*r)/100;
cout<<‖\n simple interest=‖<<si;
Getch();
}
O/p--
Enter valus for p,n,r 1000 4 9.5
simple interest=380
//Even and Odd Program in C++
#include<iostream.h>
#include<conio.h>
void main(){
int no;
clrscr();
cout<<"Enter any num: ";
cin>>no;
if(no%2==0){
cout<<"Even num";
}
else{
cout<<"Odd num";
}
getch();
}
Output

Enter any num : 5


Odd num
//Factorial program in C++ using for Loop
#include<iostream.h>
#include<conio.h>
void main()
{
int i, no, fact=1;
clrscr();
cout<<"Enter the any no. : ";
cin>>no;
for(i=1;i<=no;i++)
{
fact=fact*i;
}
cout<<"Factorial: "<<fact;
getch();
}
Output

Enter the any no. : 4


Factorial: 24
//Prime number program in C++
#include<iostream.h>
#include<conio.h>
void main(){
int i,no;
clrscr();
cout<<"Enter any num: ";
cin>>no;
if(no==1) {
cout<<"Smallest prime num is 2";
}
for(i=2;i<no;i++) {
if(no%i==0) {
cout<<"Not prime num";
break;
}
}
if(no==i)
{
cout<<"Prime num";
}
getch();
}
Output

Enter any num: 10


Not Prime Num
Introduction to Structure
Definition:
A structure is a collection of variables of different
data types, grouped together under a single
name.
● Each variable within a structure is called a

'member'.
● Name given to the structure is called as a

'structure tag'.
Structure declaration
Struct tag
{
member1;
member2;
.
.
memberN
}
Example
Structure Definition
●Memory is allocated only when
variable/instance for the structure are
created.
●Two ways to create instance of a structure
1. struct tag
{
structure member;
}instance;

eg- struct student{


char name[20];
int rollno;
int marks;
}stud1, stud2;
2. struct tag
{
structure member;
};
struct tag instance;

eg--struct student{
char name[20];
int rollno;
int marks;
};
struct student stud1, stud2;
Structure Initialization
eg--
struct student{
char name[20];
int rollno;
int marks;
}stud1={―Priya‖,10,95};
Accessing Structure Members
● Structure members can be accessed using the
operator . Also called the dot operator.
● eg-- stud1.name;
stud1.rollno=20;
Program 1--Write a program to define a structure 'tender'
having data members tender_no, cost and company_name.
Accept and display data for one variable of this structure.

#include<iostream.h>
#include<conio.h>
struct tender
{
int tender_no;
float cost;
char company_name[20];
};
void main{
struct tender t;
cout<<‖Enter tender number, cost and company name ‖;
cin>>t.tender_no>>t.cost>>t.company_name;
cout<<‖\n tender number‖<<t.tender_no;
cout<<‖\n cost<<t.cost;
cout<<‖\ncompany name<<t.company_name;
getch();
}

You might also like