Oop Unit 1
Oop Unit 1
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.
(4)
C does not support object oriented programming;
therefore it has no support for polymorphism,
encapsulation, and inheritance.
(6)
C does not support information hiding.
(8)
C does not support function and operator
overloading.
(10)
C uses functions for input/output.
For example scanf and printf.
(12)
C provides malloc() and calloc() functions for
dynamic memory allocation, and free() for memory
de-allocation.
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:
# 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:
/* 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
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
^ (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:
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;
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;
Example:
Cout<<a<<b;
cout<<―Addition=‖<<sum;
Insertion Operator(<<)--
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
'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;
};
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();
}