SlideShare a Scribd company logo
306
Object Oriented
Programming & Data
Structure
Chapter 1
Concepts Of OOPS
Concepts of OOP
Conc
epts
Object
Message Passing
Polymorphism
Abstraction
Dynamic Binding
Encapsulation
Inheritance
Class
Object:
 Any Entity that has state and behaviour is known as entity.
 Object is an active entity.(actual work)
 Object is a instance of class.
 When class is defined no memory is allocated but when it is instantiated
memory is allocated.
 Class class_name()
 {
Dataype datamember;
Datatype datafunction(){
//which task we perofrm
}
};
Int main (){
Object Obj;
Obj.function name();
Return 0;
}
Class:
 Class is a collection of objects.
 Class is a passive entity.
 A class is a user defined data type which has data member and data
function.
Class:
 Class is a collection of objects.
 Class is a passive entity.
 A class is a user defined data type which has data member and data
function.
Inheritance:
 When one class access the property of another class is called inheritance.
Polymorphism:
 When one task is performed by different ways are known as polymorphism.
 For example, A person at the same time can have different character like
a father , a husband and employee.
 So the same person can have different behaviour in different situations.
Polymorphism
Compile Time Run Time
Function
Overloading
Operator
Overloading
Virtual Function
Abstraction:
 Hiding internal details and showing functionality.
 For example, phone call we don’t know the internal processing.
 In c++ we can use abstract class and interface to achieve abstraction.
Encapsulation:
 Binding or wrapping code and data together into a single unit is known as
encapsulation.
 For example, Capsule, It is wrapped with different medicine.
Dynamic Binding:
 In dynamic binding, the code to be executed in response to function call is
decided at runtime.
 C++ has virtual function to support this.
Message Passing:
 Objects communicate with one another by sending and receiving
information to each other through message passing.
Object A Object B
Message
Sending Object Receiving Object
1.1 Difference Between Procedural
Programming & OOPS
Procedural Languages Object Oriented Language
The Program is divided into small parts
called functions.
The Program is divided into small parts
called objects.
It follows a top-down approach. It follows a bottom-up approach.
No access specifier in procedural
programming.
Object-oriented programming has
access specifier like
private,public,protected.
There is no concept of data hiding
and inheritance.
It provides concept of data hiding
and inheritance.
Doesn’t have any proper way to
hiding data so it is less secure.
Provides data hiding so it is more
secure.
Overloading is not possible. Overloading is possible.
The function is more important than
the data.
Data is more important than the
function.
It is not based on the real word
example.
It is based on the real word example.
1.2 Various Library(header) Files For C++
 The Standard Function Library (I/O,String and character
handling,Mathematical,ide-character etc)
 The Object Oriented Class Library
• The Standard C++ I/O classes
• The String Class
• The Numeric Class
• The STL Container Classes
• The STL Algorithms
• The STL Function Objects
• The STL Iterator
• Exception Handling Classes ,etc.
1.3 Data Types in C++
 C++ supports following data types:
1. Built-in Data Type
2. Derived Data Type
3. User-defined Data Type
Data Types (continue…)
1)Built-in Data Type:
 It’s also known as primary data type and fundamental data type.
 Built-in data type can be used directly by user to declare variables.
• Integer
• Character
• Boolean
• Floating Point
• Double Floating Point
• Void
• Wide Character
Data Types (continue…)
1.Integer:
 Keyword is used for integer data type is int.
 Integer data type require 4 bytes of memory space.
 Range is from -2147483648 to 2147483647.
 Long,Short,Signed,Unsigned
Example:
int a;
Data Types (continue…)
2.Character:
 Character data type is used for storing character values.
 Keyword is used for character data type is char.
 Character data type require 1 bytes of memory space.
 Range is from -128 to 127.
Example:
char a;
Data Types (continue…)
3.Boolean:
 Boolean data type is used for storing logical values.
 Keyword is used for boolean data type is bool.
 Boolean variable can store either true or false value.
Example:
bool a;
Data Types (continue…)
4.Floating Point:
 Floating Point data type is used for storing single-precision floating-point
value or decimal values.
 Keyword is used for Floating-Point data type is float.
 Float variables require 4 bytes of memory location.
Example:
float a;
Data Types (continue…)
5.Double Floating Point:
 Double Floating Point data type is used for storing double-precision floating-
point value or decimal values.
 Keyword is used for Floating-Point data type is double.
 Double Float variables require 8 bytes of memory location.
Example:
double a;
Data Types (continue…)
5.Double Floating Point:
 #include <iostream>
 using namespace std;
 double findSum(double x, double y)
 {
 return x + y;
 }
 int main()
 {
 double value1 = 10.45, value2 = 6.28;
 cout << "The sum is: " << findSum(value1, value2) << "nn";
 return 0;
 }
Data Types (continue…)
5.Double Floating Point:
 int main()
 {
 double value1 , value2;
Cout<<“Enter 1 value”<<endl;
Cin>>value1;
Cout<<“Enter 2 value”;
Cin>>value2;
 cout << "The sum is: " << findSum(value1, value2) << "nn";
 return 0;
 }
Data Types (continue…)
6.Void:
 Void means without any value.
 Void data type represents a valueless entity.
 A void data type is used for those function which does not return a value.
Example:
void function_name()
{
…
}
Data Types (continue…)
7.Wide Character:
 Wide character data type is also a character data type but this data type has a
size grater than the normal 8-bit data type
 Wide character represents by wchar_t.
 It is generally 2 or 4 bytes long.
Example:
#include<iostream.h>
using namespace std;
int main(){
wchar_t wide_character = L’a’;
cout<<“The wide character is:”<<wide_character<<endl; //97
cout<<“Wide character size: “<<sizeof(wide_character); //2
}
Data Types (continue…)
2) Derived Data Type:
 The data types that are derived from primitive or built-in data types are
referred to as Derived Data Types.
 These can be of four type.
• Function
• Array
• Pointer
• Reference
Data Types (continue…)
1. Function:
 A function is a block of code or program-segment that is define to perform a
specific task.
 A function is generally defined to save the user from writing the same lines of
code again and again for the same input.
 All the line of code are put together inside a single function and this can be
called anywhere required.
 main() is a default function that is defined in every program of c++.
Syntax:
FunctionType FunctionName(parameters)
Data Types (continue…)
Example:
#include<iostream.h>
int max(int a,int b) //find maximum
{
if(a > b)
return a;
else
return b;
}
int main()
{
int x=10,y=20;
int m = max(x,y);
cout << “m is” << m;
return 0;
}
Data Types (continue…)
2. Array:
 An array is a collection of items stored at continuous memory locations.
 The idea of array represent many instances in one variable.
 Array length = 5
 First Index = 0
 Last Index = 4
40 63 22 97 10
0 1 2 3 4
Array
Indices
Data Types (continue…)
2. Array:
Syntax :
DataType ArrayName[size_of_array];
Example:
Int main()
{
int array[3];
array[0] = 13;
array[1] = 20;
array[2] = 2;
cout<<array[0]<<“ “<<array[1]<<“ “<<array[2];
return 0;
}
Data Types (continue…)
3. Pointers:
 Pointer are symbolic representation of addresses.
 They enable programs to simulate call-by-reference as well as to create and
manipulate dynamic data structure.
 Use * before the variable name.
Syntax:
datatype *var_name;
Data Types (continue…)
3. Pointers:
Example:
Int main()
{
int var = 10;
int* ptr; //declare pointer variable
ptr = &var; //data type of ptr and var must be same
// assign addess of a variable to a pointer
cout<<“Value Of ptr = “<<ptr<<“n”;
cout<<“Value Of Var = “<<var<<“n”;
cout<<“Value of *ptr = “<<*ptr<<“n”;
}
Data Types (continue…)
4. Reference:
 When a variable is declared as reference,it becomes an alternative name for an existing
variable.
 A variable can be declared as reference by putting ‘&’ in declaration.
Example:
int main()
{
int x =10;
int& ref = x; //ref is reference of x.
ref = 20; //value of x is now chaged to 20
cout<<“x = “<<x<<endl; //20
x=30; //calue of x is now changed to 30
cout<<“ref = “<<ref<<endl; //30
return 0;
}
3.User Defined Data Type:
 1. class:
 A class represent a group of similar objects.
 To represent classes in c++, it offers a user defined data type called class.
 Once the class is defined in it objects belonging to that class can be easily
be created.
 To define a class you describe what sort of information it can represent and
what sort of action can perform with that data .
3.User Defined Data Type:
 1. class:
Class department{
public:
int emp_no;
char name[30];
char deptname[30];
public:
void add(int emp_no,string name,string deptname);
void delete();
void display(){ cout<<emp_no<<name<<deptname };
};
Int main(){
department sales,HR,accounts;
sales.add(1,”abc”,”computer science”);
sales.display();
}
3.User Defined Data Type:
#include<iostream.h>
#include<string.h>
Class MyClass{
public:
int num;
char mystring[30];
};
Int main(){
MyClass obj;
obj.num=10;
obj.mystring=“Hello World…”;
cout<<obj.num<<endl;
cout<<obj.mystring;
return 0;
}
3.User Defined Data Type:
 2. Structure :
 A structure is a collection of variables of different data types referenced under
one name.
 For every datamember it will occupy different memory location.
Struct student{
int roll_no;
char name[30];
float marks;
};
Main(){
student s1,s2,s3;
}
3.User Defined Data Type:
#include<iostream.h>
#include<string.h>
Int main(){
struct{
string brand;
string model;
int year;
}c1,c2;
c1.brand=“BMW”;
c1.model=“X5”;
c1.year=1999;
c2.brand=“Ford”;
c2.model=“Mustang”;
c2.year=1969;
cout<<c1.brand<<c1.model<<c1.year;
cout<<c2.brand<<c2.model<<c2.year;
return 0;
}
3.User Defined Data Type:
 3.Union:
 Union is a memory location that is shared by two or more different variables,
generally of different types at different times.
 Defining a union is similar to defining a structure.
 Element uses same memory location.(anyone)
Union share{
int a; //2
char ch; //1
}
Union share c; //make variable
3.User Defined Data Type:
#include <iostream.h>
union GFG {
int Geek1;
char Geek2;
float Geek3;
};
int main()
{
union GFG G1;
G1.Geek1 = 34;
cout << "The first value at " << "the allocated memory : " << G1.Geek1 << endl;
G1.Geek2 = 34;
cout << "The next value stored "
<< "after removing the "
<< "previous value : " << G1.Geek2 << endl;
G1.Geek3 = 34.34;
cout << "The Final value value "
<< "at the same allocated "
<< "memory space : " << G1.Geek3 << endl;
return 0;
}
3.User Defined Data Type:
 4.Enumeration:
 An alternative method for naming integer constants is often more
convenient than const.
 This can be achieved by enumeration by using keyword enum.
Const int start =0;
Const int pause =1;
Const int go=2;
Or
Enum status{start=10,pause=20,go=30};
3.User Defined Data Type:
#include <iostream.h>
enum detail { a1
a2
a3};
detail d1= a1;
detail d2= a2;
detail d3= a3;
int main()
{
cout << "The numerical value "<< "assigned to a1: "<< d1<< endl;
cout << "The numerical value "<< "assigned to a2: "<< d2<< endl;
cout << "The numerical value "<< "assigned to a3: "<< d3<< endl;
return 0;
}
3.User Defined Data Type:
#include <iostream.h>
enum detail {
doble pi=3.14;
}
Function sum(){
sum=pi+x;
}
Int main()
{
detail d1;
cin>>10;
cout<<“The sum is: “<<sum();
return 0;
}
1.4 Concept Of String
 Strings are used for storing a text.
 A string variable contains a collection of character surrounded by double
quotes or single quotes.
Example:
string str = “Hello”;
 To use strings, must include an additional header file in the source code, the
<string> library:
#include<string>
string str = “Hello”;
1.4.1 Character Array
 C++ programming, the collection of characters is stored in the form of arrays.
 Strings are array of type char terminated with null character, that is , 0(ASCII value
of null character is 0).
To define a String:
char str[] = “C++”;
• Here, str is a string and it holds 4 character.
• C++ has 3 character ,the null character 0 is added to the end of the string
automatically.
 Alternative way to define string
 char str[4] = “C++”;
 char str[] = {‘C’,’+’,’+’,’0’};
 char str[4] = {‘C’,’+’,’+’,’0’};
1.4.1 Character Array (continue…)
 A character array is a derived data type in c++ that used to store a collection
of characters or strings.
 A char data type take 1 byte of memory, so a character array has memory of
the number of elements in the array. (1*number_of_element_in_array).
 Each character in character array has an index that shows the position of the
character in the string.
 The null character 0 is used to find the end of characters in the array and is
always stored in the index after the last character or in the last index.
0 1 2 3 4 5
H e l l o 0
1.4.1 Character Array (continue…)
 Example:
int main()
{
char str[30];
cout<<“Enter a string: “; //C++
cin>>str;
cout<<“You entered: “<<str<<endl;
cout<<“Enter another string: “; //Hello Word (terminating character)
cin>>str;
cout<<“You entered: “<<str<<endl;
return 0;
}
1.4.2 Pointer To Character Array
 An array of pointer to strings is an array of character pointers that holds the address of the first
character of a string or we can say the base address of a string.
 In an array of pointers, the manipulation of strings is comparatively easier than in case of 2d
array.We can easily change the position of strings by using pointers.
Example:
char *names[5] = {“John”,
“Ravi”,
“Hetvi”,
“Om”,
“Mansi”};
 We declared an array of pointer name as ‘names’ of size 5.
 In this case we have done initialization at the time of declaration, so we do not need to mention
the size of array of a pointer.
 We can also declared array without any size.
 It is not guaranteed that all the string literals will be stored in the contiguous memory location, but
the characters of string literal are stored in contiguous memory location.
1.4.2 Pointer To Character Array (continue…)
Example:
int main()
{
char *names[5] = {“John”,”Om”,”Ravi”,”Hetvi”,”Mansi”};
for(int i=0;i<5;i++)
{
cout<<names[i]<<endl;
}
return 0;
}
In this code, we have declared an array of char pointer holding 5 string literals,
and the first character of each string is holding the base address of the string.
1.4.3 Use Of String.h & It’s Important Function
 String.h is a header library for string.
 This library has several common functions for dealing with strings stored in
arrays of characters.
 The string.h header file to be included before using any string function.
 To define we can write,
#include<string.h>
So many functions are used.
strcpy()
strlen()
strcmp()
strcat()
strupr()
strlwr()
1.4.3 Functions Of String.h Header File
1. strcpy():
 Strcpy() is a standard library function in C/C++ and is ued to copy one string
to another.
 The stcpy() function takes two arguments :
• taget - Pointer to the destination array where the content is to be
copied.
• source – string which will be copied.
 It copies the character string pointed by the source to the memory location
pointed by the target.
 The null terminating character is also copied.
Syntax:
char *strcpy(char *dest, const char *src);
1.4.3 Functions Of String.h Header File
1. strcpy():
Example:
#include<string.h>
#include<iostream.h>
using namespace std;
int main()
{
char s[] = “Hello World”;
char *s1[];//large enough to store content of src
cout<<“copied String: “<<strcpy(s1,s);
return 0;
}
1.4.3 Functions Of String.h Header File
2. strlen():
 The strlen() function calculates the length of a given string.
 The strlen() function is defined in string.h header file.
 It doesn’t count null character ‘0’.
Syntax:
strlen(const char *str);
• Str – It represents the string variable whose length we have to find.
1.4.3 Functions Of String.h Header File
2. strlen():
Example:
#include<string.h>
#include<iostream.h>
int main()
{
char str[] = “Hello”;
cout<<“Length of string is = “<<strlen(str);
return 0;
}
1.4.3 Functions Of String.h Header File
3. strcmp():
This function takes two strings as arguments and compare these two strings.
Synatx:
strcmp(const char *str1,const char *str2);
Example:
int main(){
char s1[] = “Hello”;
char s2[] = “Hello”;
int returnvalue = strcmp(s1,s2);
if(returnvalue==0){
cout<<“Strings are equal”;
}
else{
cout<<“Strings are unequal”;
}
cout<<“n Value returned by strcmp() is: “<<returnvalue;
return 0;
}
1.4.3 Functions Of String.h Header File
4. strcat():
 This function will append a copy of the source string to the end of destination
string.
 The strcat() function takes two arguments:
1. dest
2. src
Syntax:
strcat(dest,src);
1.4.3 Functions Of String.h Header File
4. strcat():
Example:
int main()
{
char dest[30] = “Hello”;
char src[30] = “World”;
strcat(dest,src);
cout<<dest;
return 0;
}
1.4.3 Functions Of String.h Header File
5. strrev():
 This function is used to reverse the string.
Syntax:
char *strrev(char *str);
 Str – The given string which is needed to be reversed.
1.4.3 Functions Of String.h Header File
5. strrev():
Example:
int main()
{
char str[30] = “hello”;
cout<<“Entered string is: “<<str;
cout<<“After reversing string is: “<<strrev(str);
return 0;
}
1.5 Concepts Of Class & Objective
Class:
 Class in C++ is the building block that leads to object-oriented programming.
 It is a user-defined data type,which holds its own data members and member
functions,which can be accessed and used by creating an instance of that
class.
 A C++ class is like a blueprint for an object.
 For example consider the class of cars.There may be many cars with different
names and bunds but all of them will share some common properties like all
of them will have 4 wheels,speed limit,etc.
 So here, car is the class and wheels, speed limits are their properties.
 An Object is an instance of a class.When a class is defined,no memory is
allocated but when it is instantiated memory is allocated.
1.5 Concepts Of Class & Objective
Class:
 A class is defined in c++ using the keyword class followed by the name of
class.
 The body of the class is defined inside the curly brackets and terminated by a
semicolon at the end.
class ClassName
{
Access specifier; //can be private,public or protected
Data members; //variables to be used
Member Functions(){} //methods to access data members
}; //class name ends with a semicolon
1.5 Concepts Of Class & Objective
Declaring Objects:
 when a class is defined,only the specification for the object is
defined;no memory or storage is allocated.
Syntax:
ClassName ObjectName;
 If we want to access member function using object which we can create.
Object.datamember;
Object.memberfunction();
1.5 Concepts Of Class & Objective
Class student()
{
public:
int rno;
string name;
double marks;
void display()
{
cout<<name<<rno<<marks;
}
};
Int main()
{
student s1;
s1.rno=1;
s1.name=“abc”;
s1.marks=50.67;
s1.display();
return 0;
}

More Related Content

Similar to OOPS (object oriented programming) unit 1 (20)

PPTX
OODP Unit 1 OOPs classes and objects
Shanmuganathan C
 
PPTX
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 
PPTX
Structured Languages
Mufaddal Nullwala
 
PPTX
BASIC CONCEPTS OF C++ CLASS 12
Dev Chauhan
 
PPT
C++ data types
pratikborsadiya
 
PPTX
Introduction to c++
NIDA HUSSAIN
 
PPTX
Chapter 2.datatypes and operators
Jasleen Kaur (Chandigarh University)
 
PPTX
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTRE
jatin batra
 
PPTX
Unit 1
donny101
 
PPTX
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
DeepasCSE
 
PPTX
Data types
Nokesh Prabhakar
 
PPTX
Chapter1.pptx
WondimuBantihun1
 
PPTX
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
crazysamarth927
 
PPT
5-Lec - Datatypes.ppt
AqeelAbbas94
 
PPTX
C++ & Data Structure - Unit - first.pptx
KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY
 
PPTX
Introduction to C++ Programming
Preeti Kashyap
 
PPTX
Chp 3 C++ for newbies, learn fast and earn fast
nhbinaaa112
 
PPT
programming week 2.ppt
FatimaZafar68
 
OODP Unit 1 OOPs classes and objects
Shanmuganathan C
 
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 
Structured Languages
Mufaddal Nullwala
 
BASIC CONCEPTS OF C++ CLASS 12
Dev Chauhan
 
C++ data types
pratikborsadiya
 
Introduction to c++
NIDA HUSSAIN
 
Chapter 2.datatypes and operators
Jasleen Kaur (Chandigarh University)
 
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTRE
jatin batra
 
Unit 1
donny101
 
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
DeepasCSE
 
Data types
Nokesh Prabhakar
 
Chapter1.pptx
WondimuBantihun1
 
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
crazysamarth927
 
5-Lec - Datatypes.ppt
AqeelAbbas94
 
C++ & Data Structure - Unit - first.pptx
KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY
 
Introduction to C++ Programming
Preeti Kashyap
 
Chp 3 C++ for newbies, learn fast and earn fast
nhbinaaa112
 
programming week 2.ppt
FatimaZafar68
 

Recently uploaded (20)

PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
PPT
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Amity University, Patna
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PPTX
Presentation 2.pptx AI-powered home security systems Secure-by-design IoT fr...
SoundaryaBC2
 
PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Amity University, Patna
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Presentation 2.pptx AI-powered home security systems Secure-by-design IoT fr...
SoundaryaBC2
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Ad

OOPS (object oriented programming) unit 1

  • 3. Concepts of OOP Conc epts Object Message Passing Polymorphism Abstraction Dynamic Binding Encapsulation Inheritance Class
  • 4. Object:  Any Entity that has state and behaviour is known as entity.  Object is an active entity.(actual work)  Object is a instance of class.  When class is defined no memory is allocated but when it is instantiated memory is allocated.
  • 5.  Class class_name()  { Dataype datamember; Datatype datafunction(){ //which task we perofrm } }; Int main (){ Object Obj; Obj.function name(); Return 0; }
  • 6. Class:  Class is a collection of objects.  Class is a passive entity.  A class is a user defined data type which has data member and data function.
  • 7. Class:  Class is a collection of objects.  Class is a passive entity.  A class is a user defined data type which has data member and data function.
  • 8. Inheritance:  When one class access the property of another class is called inheritance.
  • 9. Polymorphism:  When one task is performed by different ways are known as polymorphism.  For example, A person at the same time can have different character like a father , a husband and employee.  So the same person can have different behaviour in different situations. Polymorphism Compile Time Run Time Function Overloading Operator Overloading Virtual Function
  • 10. Abstraction:  Hiding internal details and showing functionality.  For example, phone call we don’t know the internal processing.  In c++ we can use abstract class and interface to achieve abstraction.
  • 11. Encapsulation:  Binding or wrapping code and data together into a single unit is known as encapsulation.  For example, Capsule, It is wrapped with different medicine.
  • 12. Dynamic Binding:  In dynamic binding, the code to be executed in response to function call is decided at runtime.  C++ has virtual function to support this.
  • 13. Message Passing:  Objects communicate with one another by sending and receiving information to each other through message passing. Object A Object B Message Sending Object Receiving Object
  • 14. 1.1 Difference Between Procedural Programming & OOPS Procedural Languages Object Oriented Language The Program is divided into small parts called functions. The Program is divided into small parts called objects. It follows a top-down approach. It follows a bottom-up approach. No access specifier in procedural programming. Object-oriented programming has access specifier like private,public,protected. There is no concept of data hiding and inheritance. It provides concept of data hiding and inheritance. Doesn’t have any proper way to hiding data so it is less secure. Provides data hiding so it is more secure. Overloading is not possible. Overloading is possible. The function is more important than the data. Data is more important than the function. It is not based on the real word example. It is based on the real word example.
  • 15. 1.2 Various Library(header) Files For C++  The Standard Function Library (I/O,String and character handling,Mathematical,ide-character etc)  The Object Oriented Class Library • The Standard C++ I/O classes • The String Class • The Numeric Class • The STL Container Classes • The STL Algorithms • The STL Function Objects • The STL Iterator • Exception Handling Classes ,etc.
  • 16. 1.3 Data Types in C++  C++ supports following data types: 1. Built-in Data Type 2. Derived Data Type 3. User-defined Data Type
  • 17. Data Types (continue…) 1)Built-in Data Type:  It’s also known as primary data type and fundamental data type.  Built-in data type can be used directly by user to declare variables. • Integer • Character • Boolean • Floating Point • Double Floating Point • Void • Wide Character
  • 18. Data Types (continue…) 1.Integer:  Keyword is used for integer data type is int.  Integer data type require 4 bytes of memory space.  Range is from -2147483648 to 2147483647.  Long,Short,Signed,Unsigned Example: int a;
  • 19. Data Types (continue…) 2.Character:  Character data type is used for storing character values.  Keyword is used for character data type is char.  Character data type require 1 bytes of memory space.  Range is from -128 to 127. Example: char a;
  • 20. Data Types (continue…) 3.Boolean:  Boolean data type is used for storing logical values.  Keyword is used for boolean data type is bool.  Boolean variable can store either true or false value. Example: bool a;
  • 21. Data Types (continue…) 4.Floating Point:  Floating Point data type is used for storing single-precision floating-point value or decimal values.  Keyword is used for Floating-Point data type is float.  Float variables require 4 bytes of memory location. Example: float a;
  • 22. Data Types (continue…) 5.Double Floating Point:  Double Floating Point data type is used for storing double-precision floating- point value or decimal values.  Keyword is used for Floating-Point data type is double.  Double Float variables require 8 bytes of memory location. Example: double a;
  • 23. Data Types (continue…) 5.Double Floating Point:  #include <iostream>  using namespace std;  double findSum(double x, double y)  {  return x + y;  }  int main()  {  double value1 = 10.45, value2 = 6.28;  cout << "The sum is: " << findSum(value1, value2) << "nn";  return 0;  }
  • 24. Data Types (continue…) 5.Double Floating Point:  int main()  {  double value1 , value2; Cout<<“Enter 1 value”<<endl; Cin>>value1; Cout<<“Enter 2 value”; Cin>>value2;  cout << "The sum is: " << findSum(value1, value2) << "nn";  return 0;  }
  • 25. Data Types (continue…) 6.Void:  Void means without any value.  Void data type represents a valueless entity.  A void data type is used for those function which does not return a value. Example: void function_name() { … }
  • 26. Data Types (continue…) 7.Wide Character:  Wide character data type is also a character data type but this data type has a size grater than the normal 8-bit data type  Wide character represents by wchar_t.  It is generally 2 or 4 bytes long. Example: #include<iostream.h> using namespace std; int main(){ wchar_t wide_character = L’a’; cout<<“The wide character is:”<<wide_character<<endl; //97 cout<<“Wide character size: “<<sizeof(wide_character); //2 }
  • 27. Data Types (continue…) 2) Derived Data Type:  The data types that are derived from primitive or built-in data types are referred to as Derived Data Types.  These can be of four type. • Function • Array • Pointer • Reference
  • 28. Data Types (continue…) 1. Function:  A function is a block of code or program-segment that is define to perform a specific task.  A function is generally defined to save the user from writing the same lines of code again and again for the same input.  All the line of code are put together inside a single function and this can be called anywhere required.  main() is a default function that is defined in every program of c++. Syntax: FunctionType FunctionName(parameters)
  • 29. Data Types (continue…) Example: #include<iostream.h> int max(int a,int b) //find maximum { if(a > b) return a; else return b; } int main() { int x=10,y=20; int m = max(x,y); cout << “m is” << m; return 0; }
  • 30. Data Types (continue…) 2. Array:  An array is a collection of items stored at continuous memory locations.  The idea of array represent many instances in one variable.  Array length = 5  First Index = 0  Last Index = 4 40 63 22 97 10 0 1 2 3 4 Array Indices
  • 31. Data Types (continue…) 2. Array: Syntax : DataType ArrayName[size_of_array]; Example: Int main() { int array[3]; array[0] = 13; array[1] = 20; array[2] = 2; cout<<array[0]<<“ “<<array[1]<<“ “<<array[2]; return 0; }
  • 32. Data Types (continue…) 3. Pointers:  Pointer are symbolic representation of addresses.  They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structure.  Use * before the variable name. Syntax: datatype *var_name;
  • 33. Data Types (continue…) 3. Pointers: Example: Int main() { int var = 10; int* ptr; //declare pointer variable ptr = &var; //data type of ptr and var must be same // assign addess of a variable to a pointer cout<<“Value Of ptr = “<<ptr<<“n”; cout<<“Value Of Var = “<<var<<“n”; cout<<“Value of *ptr = “<<*ptr<<“n”; }
  • 34. Data Types (continue…) 4. Reference:  When a variable is declared as reference,it becomes an alternative name for an existing variable.  A variable can be declared as reference by putting ‘&’ in declaration. Example: int main() { int x =10; int& ref = x; //ref is reference of x. ref = 20; //value of x is now chaged to 20 cout<<“x = “<<x<<endl; //20 x=30; //calue of x is now changed to 30 cout<<“ref = “<<ref<<endl; //30 return 0; }
  • 35. 3.User Defined Data Type:  1. class:  A class represent a group of similar objects.  To represent classes in c++, it offers a user defined data type called class.  Once the class is defined in it objects belonging to that class can be easily be created.  To define a class you describe what sort of information it can represent and what sort of action can perform with that data .
  • 36. 3.User Defined Data Type:  1. class: Class department{ public: int emp_no; char name[30]; char deptname[30]; public: void add(int emp_no,string name,string deptname); void delete(); void display(){ cout<<emp_no<<name<<deptname }; }; Int main(){ department sales,HR,accounts; sales.add(1,”abc”,”computer science”); sales.display(); }
  • 37. 3.User Defined Data Type: #include<iostream.h> #include<string.h> Class MyClass{ public: int num; char mystring[30]; }; Int main(){ MyClass obj; obj.num=10; obj.mystring=“Hello World…”; cout<<obj.num<<endl; cout<<obj.mystring; return 0; }
  • 38. 3.User Defined Data Type:  2. Structure :  A structure is a collection of variables of different data types referenced under one name.  For every datamember it will occupy different memory location. Struct student{ int roll_no; char name[30]; float marks; }; Main(){ student s1,s2,s3; }
  • 39. 3.User Defined Data Type: #include<iostream.h> #include<string.h> Int main(){ struct{ string brand; string model; int year; }c1,c2; c1.brand=“BMW”; c1.model=“X5”; c1.year=1999; c2.brand=“Ford”; c2.model=“Mustang”; c2.year=1969; cout<<c1.brand<<c1.model<<c1.year; cout<<c2.brand<<c2.model<<c2.year; return 0; }
  • 40. 3.User Defined Data Type:  3.Union:  Union is a memory location that is shared by two or more different variables, generally of different types at different times.  Defining a union is similar to defining a structure.  Element uses same memory location.(anyone) Union share{ int a; //2 char ch; //1 } Union share c; //make variable
  • 41. 3.User Defined Data Type: #include <iostream.h> union GFG { int Geek1; char Geek2; float Geek3; }; int main() { union GFG G1; G1.Geek1 = 34; cout << "The first value at " << "the allocated memory : " << G1.Geek1 << endl; G1.Geek2 = 34; cout << "The next value stored " << "after removing the " << "previous value : " << G1.Geek2 << endl; G1.Geek3 = 34.34; cout << "The Final value value " << "at the same allocated " << "memory space : " << G1.Geek3 << endl; return 0; }
  • 42. 3.User Defined Data Type:  4.Enumeration:  An alternative method for naming integer constants is often more convenient than const.  This can be achieved by enumeration by using keyword enum. Const int start =0; Const int pause =1; Const int go=2; Or Enum status{start=10,pause=20,go=30};
  • 43. 3.User Defined Data Type: #include <iostream.h> enum detail { a1 a2 a3}; detail d1= a1; detail d2= a2; detail d3= a3; int main() { cout << "The numerical value "<< "assigned to a1: "<< d1<< endl; cout << "The numerical value "<< "assigned to a2: "<< d2<< endl; cout << "The numerical value "<< "assigned to a3: "<< d3<< endl; return 0; }
  • 44. 3.User Defined Data Type: #include <iostream.h> enum detail { doble pi=3.14; } Function sum(){ sum=pi+x; } Int main() { detail d1; cin>>10; cout<<“The sum is: “<<sum(); return 0; }
  • 45. 1.4 Concept Of String  Strings are used for storing a text.  A string variable contains a collection of character surrounded by double quotes or single quotes. Example: string str = “Hello”;  To use strings, must include an additional header file in the source code, the <string> library: #include<string> string str = “Hello”;
  • 46. 1.4.1 Character Array  C++ programming, the collection of characters is stored in the form of arrays.  Strings are array of type char terminated with null character, that is , 0(ASCII value of null character is 0). To define a String: char str[] = “C++”; • Here, str is a string and it holds 4 character. • C++ has 3 character ,the null character 0 is added to the end of the string automatically.  Alternative way to define string  char str[4] = “C++”;  char str[] = {‘C’,’+’,’+’,’0’};  char str[4] = {‘C’,’+’,’+’,’0’};
  • 47. 1.4.1 Character Array (continue…)  A character array is a derived data type in c++ that used to store a collection of characters or strings.  A char data type take 1 byte of memory, so a character array has memory of the number of elements in the array. (1*number_of_element_in_array).  Each character in character array has an index that shows the position of the character in the string.  The null character 0 is used to find the end of characters in the array and is always stored in the index after the last character or in the last index. 0 1 2 3 4 5 H e l l o 0
  • 48. 1.4.1 Character Array (continue…)  Example: int main() { char str[30]; cout<<“Enter a string: “; //C++ cin>>str; cout<<“You entered: “<<str<<endl; cout<<“Enter another string: “; //Hello Word (terminating character) cin>>str; cout<<“You entered: “<<str<<endl; return 0; }
  • 49. 1.4.2 Pointer To Character Array  An array of pointer to strings is an array of character pointers that holds the address of the first character of a string or we can say the base address of a string.  In an array of pointers, the manipulation of strings is comparatively easier than in case of 2d array.We can easily change the position of strings by using pointers. Example: char *names[5] = {“John”, “Ravi”, “Hetvi”, “Om”, “Mansi”};  We declared an array of pointer name as ‘names’ of size 5.  In this case we have done initialization at the time of declaration, so we do not need to mention the size of array of a pointer.  We can also declared array without any size.  It is not guaranteed that all the string literals will be stored in the contiguous memory location, but the characters of string literal are stored in contiguous memory location.
  • 50. 1.4.2 Pointer To Character Array (continue…) Example: int main() { char *names[5] = {“John”,”Om”,”Ravi”,”Hetvi”,”Mansi”}; for(int i=0;i<5;i++) { cout<<names[i]<<endl; } return 0; } In this code, we have declared an array of char pointer holding 5 string literals, and the first character of each string is holding the base address of the string.
  • 51. 1.4.3 Use Of String.h & It’s Important Function  String.h is a header library for string.  This library has several common functions for dealing with strings stored in arrays of characters.  The string.h header file to be included before using any string function.  To define we can write, #include<string.h> So many functions are used. strcpy() strlen() strcmp() strcat() strupr() strlwr()
  • 52. 1.4.3 Functions Of String.h Header File 1. strcpy():  Strcpy() is a standard library function in C/C++ and is ued to copy one string to another.  The stcpy() function takes two arguments : • taget - Pointer to the destination array where the content is to be copied. • source – string which will be copied.  It copies the character string pointed by the source to the memory location pointed by the target.  The null terminating character is also copied. Syntax: char *strcpy(char *dest, const char *src);
  • 53. 1.4.3 Functions Of String.h Header File 1. strcpy(): Example: #include<string.h> #include<iostream.h> using namespace std; int main() { char s[] = “Hello World”; char *s1[];//large enough to store content of src cout<<“copied String: “<<strcpy(s1,s); return 0; }
  • 54. 1.4.3 Functions Of String.h Header File 2. strlen():  The strlen() function calculates the length of a given string.  The strlen() function is defined in string.h header file.  It doesn’t count null character ‘0’. Syntax: strlen(const char *str); • Str – It represents the string variable whose length we have to find.
  • 55. 1.4.3 Functions Of String.h Header File 2. strlen(): Example: #include<string.h> #include<iostream.h> int main() { char str[] = “Hello”; cout<<“Length of string is = “<<strlen(str); return 0; }
  • 56. 1.4.3 Functions Of String.h Header File 3. strcmp(): This function takes two strings as arguments and compare these two strings. Synatx: strcmp(const char *str1,const char *str2); Example: int main(){ char s1[] = “Hello”; char s2[] = “Hello”; int returnvalue = strcmp(s1,s2); if(returnvalue==0){ cout<<“Strings are equal”; } else{ cout<<“Strings are unequal”; } cout<<“n Value returned by strcmp() is: “<<returnvalue; return 0; }
  • 57. 1.4.3 Functions Of String.h Header File 4. strcat():  This function will append a copy of the source string to the end of destination string.  The strcat() function takes two arguments: 1. dest 2. src Syntax: strcat(dest,src);
  • 58. 1.4.3 Functions Of String.h Header File 4. strcat(): Example: int main() { char dest[30] = “Hello”; char src[30] = “World”; strcat(dest,src); cout<<dest; return 0; }
  • 59. 1.4.3 Functions Of String.h Header File 5. strrev():  This function is used to reverse the string. Syntax: char *strrev(char *str);  Str – The given string which is needed to be reversed.
  • 60. 1.4.3 Functions Of String.h Header File 5. strrev(): Example: int main() { char str[30] = “hello”; cout<<“Entered string is: “<<str; cout<<“After reversing string is: “<<strrev(str); return 0; }
  • 61. 1.5 Concepts Of Class & Objective Class:  Class in C++ is the building block that leads to object-oriented programming.  It is a user-defined data type,which holds its own data members and member functions,which can be accessed and used by creating an instance of that class.  A C++ class is like a blueprint for an object.  For example consider the class of cars.There may be many cars with different names and bunds but all of them will share some common properties like all of them will have 4 wheels,speed limit,etc.  So here, car is the class and wheels, speed limits are their properties.  An Object is an instance of a class.When a class is defined,no memory is allocated but when it is instantiated memory is allocated.
  • 62. 1.5 Concepts Of Class & Objective Class:  A class is defined in c++ using the keyword class followed by the name of class.  The body of the class is defined inside the curly brackets and terminated by a semicolon at the end. class ClassName { Access specifier; //can be private,public or protected Data members; //variables to be used Member Functions(){} //methods to access data members }; //class name ends with a semicolon
  • 63. 1.5 Concepts Of Class & Objective Declaring Objects:  when a class is defined,only the specification for the object is defined;no memory or storage is allocated. Syntax: ClassName ObjectName;  If we want to access member function using object which we can create. Object.datamember; Object.memberfunction();
  • 64. 1.5 Concepts Of Class & Objective Class student() { public: int rno; string name; double marks; void display() { cout<<name<<rno<<marks; } }; Int main() { student s1; s1.rno=1; s1.name=“abc”; s1.marks=50.67; s1.display(); return 0; }