SlideShare ist ein Scribd-Unternehmen logo
1 von 46
Presented By,
Thooyavan V
INTRODUCTION TO OOPS
FEATURES OF OBJECT ORIENTED PROGRAMMING
• Objects
• Classes
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism
• Overloading
• Exception Handling
• Constructor & Destructor
OBJECT ORIENTED PROGRAMMING
OBJECTS
 Objects arebasic unit of OOP.
 They areinstance of a class.
 Consists of various data membersand member functions.
 These data typesand memberfunctions arebundled together asa unit
is called objects.
CLASSES
 It is similar to Structure in C.
 Class is userdefined data type.
 It holds owndata membersand member functions.
 Class can beaccessed and usedonly byinstance of that class. It is
basically blueprint for object.
// Header Files
#include <iostream.h>
#include<conio.h>
// Class Declaration
class person
{
//Access - Specifier
public:
// Variable Declaration
string name;
int number;
};
//Main Function
int main()
{
// Object Creation For Class
person obj;
//Get Input Values For Object Variables
cout<<"Enter the Name :";
cin>>obj.name;
cout<<"Enter the Number :";
cin>>obj.number;
//Show the Output
cout << obj.name << ": " << obj.number <<
endl;
getch();
return 0;
}
ACCESS SPECIFIERS
Private: Private member defines that the members or methods
can be accessed within the same class only.
Public: Public member defines that the variable or methods
can be accessed at any where within
the project.
Protected: Protected member can be accessed to the class
which is inherited by other class.
 By default, all members and function of a class is private i.e if
no access specifier is specified.
SYNTAX OF DECLARING ACCESS MODIFIERS
IN C++
class
{
private:
// private members and function
public:
// public members and function
protected:
// protected members and function
};
ABSTRACTION
 A model complex of a system that includes only the details essential
to perspective of the viewerof the system.
 An Abstraction is a model of a complex system that includes only
the essential details.
 Abstractions are the fundamental way that we manage complexity.
Helps to managecomplexity of alarge system.
 Support our quality goals of modifiability and reusability.
#include<iostream.h>
#include<conio.h>
class sum
{
// hidden data from outside
world
private:
int a,b,c;
public:
void add()
{
cout<<"Enter any two numbers: ";
cin>>a>>b; c=a+b;
cout<<"Sum: "<<c;
}
};
void main()
{
sum s;
s.add();
getch();
}
ENCAPSULATION
 It can besaid Data binding.
 Encapsulation is the method of combining the data and
functions inside a class. This hides the data from being accessed
from outside a class directly, only through the functions inside the
class is able to access the information.
#include <iostream.h> class Add
{
private:
int x,y,r;
public:
int Addition(int x, int y)
{
r= x+y;
return r;
}
void show( )
{ cout << "The sum is::" << r << "n";}
}s;
void main()
{
Add s;
s.Addition(10, 4);
s.show();
}
OUTPUT
The sum is:: 14
integer values "x,y,r" of the class "Add" can be
accessed only through the function "Addition".
These integer values are encapsulated inside the
class "Add".
ENCAPSULATION
INHERITANCE
 Code reusability.
 Process of forming newclassfrom anexisting class. Inheritedclassis called
Base class.
 classwhich inherits is called Derived class.
PROS
Helps to reducecode size.
TYPESOFINHERITANCEIN C++
1. Single inheritance.
2. Multilevel inheritance.
3. Hierarchical inheritance.
4. Multiple inheritance.
5. Hybrid inheritance.
SINGLE INHERITANCE
#include<iostream>
using namespace std;
class single_base
{
protected:
int a,b;
public:
void get()
{
cout<<"Enter a & b";
cin>>a>>b;
} };
class single_derived :
public single_base
{
int c;
public:
void output()
{
c=a+b;
cout<<"Sum:"<<c;
}
};
int main()
{
single_derived obj;
obj.get();
obj.output();
return 0;
}
OUTPUT
MULTILEVEL INHERITANCE
#include <iostream>
using namespace std;
class A
{
public:
void display()
{ cout<<"Base class content."; }
};
class B : public A
{
};
class C : public B
{
};
int main()
{
C obj;
obj.display();
return 0;
}
HIERARCHICAL INHERITANCE
#include<iostream>
using namespace std;
class father
// Base class derivation
{
int age;
char name [20];
public:
void get()
{
cout << "nEnter father's
name:";
cin >> name;
cout << "Enter father's age:";
cin >> age;
}
void show()
{
cout << "nnFather's name is
" << name;
cout << "nFather's age is "
<< age;
}
};
class son : public father
// First derived class derived
from father class
{
int age;
char name [20];
public:
void get()
{
father :: get();
cout << "Enter son's name:";
cin >> name;
cout << "Enter son's age:";
cin >> age;
}
void show()
{
father::show();
cout << "nSon's name is " <<
name;
cout << "nSon's age is " <<
age;
}
};
class daughter : public father
// Second derived class derived
from the father class
{
int age;
char name [20];
public:
void get()
{
father :: get();
cout << "Enter daughter's
name:";
cin >> name;
cout << "Enter daughter's
age:";
cin >> age;
}
void show()
{
father::show();
cout << "nDaughter's name is "
<< name;
cout << "nDaughter's age is "
<< age;
}
};
int main ()
{
son s1;
daughter d1;
s1.get();
d1.get();
s1.show();
d1.show();
return 0;
}
OUTPUT
MULTIPLE INHERITANCE
#include<iostream>
using namespace std;
class base1
{ public:
int a;
void firstdata()
{
cout<<"Enter a:";
cin>>a;
}
};
class base2
{ public:
int b;
void seconddata()
{
cout<<"Enter b:";
cin>>b;
}
};
class deriverd : public
base1,public base2
{ public:
int c;
void addition()
{
c=a+b;
cout<<"Output"<<c;
}
};
int main()
{
deriverd obj;
obj.firstdata();
obj.seconddata();
obj.addition();
return 0;
}
output
HYBRID INHERITANCE
#include<iostream.h>
#include<conio.h>
class arithmetic
{
protected:
int num1, num2;
public:
void getdata()
{
cout<<"For Addition:";
cout<<"n Enter the first number: ";
cin>>num1;
cout<<"n Enter the second
number: ";
cin>>num2;
}
};
class plus: public arithmetic
{
protected:
int sum;
public:
void add()
{
sum=num1+num2;
}
};
class minus
{
protected:
int n1,n2,diff;
public:
void sub()
{
cout<<"n For Subtraction:";
cout<<"n Enter the first number: ";
cin>>n1;
cout<<"n Enter the second
number: ";
cin>>n2;
diff=n1-n2;
}
};
class result:public plus, public
minus
{
public:
void display()
{
cout<<"n Sum of "<<num1<<" and
"<<num2<<"= "<<sum;
cout<<"n Difference of "<<n1<<"
and "<<n2<<"= "<<diff;
}
};
void main()
{
result z;
z.getdata();
z.add();
z.sub();
z.display();
getch();
}
OUTPUT
POLYMORPHISM
• It makesthe code More readable.
• Function with samenamebut different arguments. Functioning is
different.
• Poly refersto many.
TypesofPolymorphism
• Compile time Polymorphism.
• Run time polymorphism.
REAL LIFE EXAMPLE OF POLYMORPHISM IN C++
COMPILE TIME POLYMORPHISM
In C++ programming you can achieve compile time polymorphism
in two way, which is given below;
 Method overloading
 Method overriding
DIFFERENT WAYS TO OVERLOAD THE
METHOD
There are two ways to overload the method in C++
 By changing number of arguments or parameters
 By changing the data type
METHOD OVERLOADING IN C++ BY
CHANGING NUMBER OF ARGUMENTS
#include<iostream.h>
#include<conio.h>
class Addition
{
public:
void sum(int a, int b)
{
cout<<a+b;
}
void sum(int a, int b, int c)
{
cout<<a+b+c;
}
};
void main()
{
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20, 30);
}
OUTPUT
METHOD OVERLOADING IN C++ BY
CHANGING THE DATA TYPE
#include<iostream.h>
#include<conio.h>
class Addition
{
public:
void sum(int a, int b)
{
cout<<a+b;
}
void sum(float a, float b,float c)
{
cout<<a+b+c;
}
};
void main()
{
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10.5,20.5,30.0);
}
OUTPUT
METHOD OVERRIDING IN C++
#include<iostream.h>
#include<conio.h>
class Base
{
public:
void show()
{
cout<<"Base class";
}
};
class Derived:public Base
{
public:
void show()
{
cout<<"Derived Class";
}
};
void main()
{
Base b; //Base class object
Derived d; //Derived class object
b.show(); //Early Binding Occurs
d.show();
getch();
}
Base class
Derived Class
OUTPUT
RUN TIME POLYMORPHISM(VIRTUAL FUNCTION)
#include<iostream.h>
#include<conio.h>
class A
{
public:
virtual void show()
{
cout<<"Hello base class";
}
};
class B : public A
{
public:
void show()
{
cout<<"Hello derive class";
}
};
void main()
{
A obj1;
B obj2;
obj1.show();
obj2.show();
getch();
}
OUTPUT
FRIEND FUNCTION IN C++
#include<iostream.h>
#include<conio.h>
class employee
{
private:
friend void sal();
};
void sal()
{
int salary=4000;
cout<<"Salary: "<<salary;
}
void main()
{
employee e;
sal();
getch();
}
Salary: 4000
OUTPUT
CONSTRUCTOR IN C++
 A class constructor is a special member function of a class that is
executed whenever we create new objects of that class.
FEATURES OF CONSTRUCTOR
 The same name as the class itself.
 no return type.
SYNTAX
classname()
{
....
}
Why use constructor ?
 The main use of constructor is placing user defined values in
place of default values.
How Constructor eliminate default values ?
 Constructor are mainly used for eliminate default values by user
defined values, whenever we create an object of any class then
its allocate memory for all the data members and initialize there
default values. To eliminate these default values by user defined
values we use constructor.
#include<iostream.h>
#include<conio.h>
class sum
{
int a,b,c;
sum()
{
a=10;
b=20;
c=a+b;
cout<<"Sum: "<<c;
}
};
void main()
{
sum s;
getch();
}
CONSTRUCTOR
OUTPUT
Sum: 30
DESTRUCTOR IN C++
 Destructor is a member function which deletes an object. A
destructor function is called automatically when the object goes
out of scope:
When destructor call
 when program ends
 when a block containing temporary variables ends
 when a delete operator is called
Features of destructor
 The same name as the class but is preceded by a tilde (~)
 no arguments and return no values
Syntax
~classname() { ...... }
#include<iostream.h>
#include<conio.h>
class sum
{
int a,b,c;
sum()
{
a=10;
b=20;
c=a+b;
cout<<"Sum: "<<c;
}
~sum()
{
cout<<endl<<"call destructor";
}
delay(500);
};
void main()
{
sum s;
cout<<endl<<"call main";
getch();
}
Sum: 30
call main
call destructor
OUTPUT
EXCEPTION HANDLING
• Exceptions provide a way to transfer control from one part of a
program to another. C++ exception handling is built upon three
keywords: try, catch, and throw.
• throw: A program throws an exception when a problem shows
up. This is done using a throw keyword.
• catch: A program catches an exception with an exception
handler at the place in a program where you want to handle the
problem. The catch keyword indicates the catching of an
exception.
• try: A try block identifies a block of code for which particular
exceptions will be activated. It's followed by one or more catch
blocks.
Try
{
// protected code
}
catch( ExceptionName e1 )
{
// catch block
}
catch( ExceptionName e2 )
{
// catch block }
catch( ExceptionName eN )
{
// catch block
}
WITHOUT EXCEPTION
#include <iostream>
#include <string>
using namespace std;
int main() {
int numerator, denominator, result;
cout <<"Enter the Numerator:";
cin>>numerator;
cout<<"Enter the denominator:";
cin>>denominator;
result = numerator/denominator;
cout<<"nThe result of division is:" <<result;
}
WITH EXCEPTION
#include <iostream>
#include <string>
using namespace std;
int main() {
int numerator, denominator, result;
cout <<"Enter the Numerator:";
cin>>numerator;
cout<<"Enter the denominator:";
cin>>denominator;
try {
if(denominator == 0)
{
throw denominator;
}
result = numerator/denominator;
cout<<"nThe result of division is:" <<result;
}
catch(int num) {
cout<<"You cannot enter "<<num<<" in
denominator.";
}
}
THIS POINTER
#include <iostream>
#include <conio.h>
using namespace std;
class sample
{
int a, b;
public:
void input(int a, int b)
{
this->a=a+b;
this->b=a-b;
}
void output()
{
cout<<"a = "<<a<<endl<<"b = "<<b;
}
};
int main()
{
sample x;
x.input(5,8);
x.output();
getch();
return 0;
}
INLINE FUNCTION
#include<iostream.h>
#include<conio.h>
int a,b;
inline int sum(int a,int b)
{
return(a+b);
}
int main()
{
int x,y;
clrscr();
cout<<"two num";
cin>>x>>y;
cout<<"sum of two
num"<<sum(x,y);
getch();
return 0;
}
Inline Function
TEMPLATES
• In c++ programming allows functions or class to work on
more than one data type at once without writing different
codes for different data types.
• It is often uses in larger programs for the purpose of of
code reusability and flexibility of program.
It can used in two ways:
1. function Template
2. Class Template
FUNCTION TEMPLATE
A single function template can work on different types at
once but, different functions are needed to perform identical
task on different data types.
Syntax:
template<class type>ret-type func-name ()
{
//body of the function
}
#include<iostream.h>
#include<conio.h>
template<class T>
void show(T a)
{
cout<<“n a=“<<a;
}
void main()
{
clrscr();
show(‘n’);
show(12.34);
show(10);
show(“nils”);
getch();
}
Output:
CLASS TEMPLATE
The general form of class templates is shown here:
Syntax:
template<class type>class class-name
{
// body of function
}
#include<iostream.h>
#include<conio.h>
template<class T>
class a
{
private:
T x;
public:
a()
{}
a(T a)
{
x=a;
}
Void show()
{
Cout<<“n x = “<<x; }
};
Void main()
{
clrscr();
a<char>a1(‘B’);
a1.show();
a<int>a2(10);
a2.show();
a<float> a3(12.34f);
a3.show();
a<double>a4(10.5);
A4.show();
getch();
}
Output:

Weitere ähnliche Inhalte

Was ist angesagt?

Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programmingSachin Sharma
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ pptKumar
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in JavaSpotle.ai
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principlesdeonpmeyer
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Sanjit Shaw
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)MD Sulaiman
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 

Was ist angesagt? (20)

Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 

Andere mochten auch

L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variablesteach4uin
 
Import data MySQL to Excel & Excel to MySQL
Import data MySQL to Excel & Excel to MySQLImport data MySQL to Excel & Excel to MySQL
Import data MySQL to Excel & Excel to MySQLThooyavan Venkatachalam
 
Basic elements of java
Basic elements of java Basic elements of java
Basic elements of java Ahmad Idrees
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in JavaTushar B Kute
 
Python: Multiple Inheritance
Python: Multiple InheritancePython: Multiple Inheritance
Python: Multiple InheritanceDamian T. Gordon
 
Java 101 intro to programming with java
Java 101  intro to programming with javaJava 101  intro to programming with java
Java 101 intro to programming with javaHawkman Academy
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 

Andere mochten auch (14)

Java
JavaJava
Java
 
Presentation
PresentationPresentation
Presentation
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 
Import data MySQL to Excel & Excel to MySQL
Import data MySQL to Excel & Excel to MySQLImport data MySQL to Excel & Excel to MySQL
Import data MySQL to Excel & Excel to MySQL
 
Basic elements of java
Basic elements of java Basic elements of java
Basic elements of java
 
Java notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esectionJava notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esection
 
Java Datatypes
Java DatatypesJava Datatypes
Java Datatypes
 
Java features
Java featuresJava features
Java features
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Python: Multiple Inheritance
Python: Multiple InheritancePython: Multiple Inheritance
Python: Multiple Inheritance
 
Java 101 intro to programming with java
Java 101  intro to programming with javaJava 101  intro to programming with java
Java 101 intro to programming with java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
advanced security system for women
advanced security system for womenadvanced security system for women
advanced security system for women
 
Type Casting in C++
Type Casting in C++Type Casting in C++
Type Casting in C++
 

Ähnlich wie OOPS Basics With Example

Ähnlich wie OOPS Basics With Example (20)

INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Virtual function
Virtual functionVirtual function
Virtual function
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Object oriented programming 2
Object oriented programming 2Object oriented programming 2
Object oriented programming 2
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
Inheritance
InheritanceInheritance
Inheritance
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
 
C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)
 
Constructor&method
Constructor&methodConstructor&method
Constructor&method
 
OOPS 22-23 (1).pptx
OOPS 22-23 (1).pptxOOPS 22-23 (1).pptx
OOPS 22-23 (1).pptx
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
C++ programs
C++ programsC++ programs
C++ programs
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 

Kürzlich hochgeladen

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Kürzlich hochgeladen (20)

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

OOPS Basics With Example

  • 2. FEATURES OF OBJECT ORIENTED PROGRAMMING • Objects • Classes • Abstraction • Encapsulation • Inheritance • Polymorphism • Overloading • Exception Handling • Constructor & Destructor
  • 4. OBJECTS  Objects arebasic unit of OOP.  They areinstance of a class.  Consists of various data membersand member functions.  These data typesand memberfunctions arebundled together asa unit is called objects.
  • 5. CLASSES  It is similar to Structure in C.  Class is userdefined data type.  It holds owndata membersand member functions.  Class can beaccessed and usedonly byinstance of that class. It is basically blueprint for object.
  • 6. // Header Files #include <iostream.h> #include<conio.h> // Class Declaration class person { //Access - Specifier public: // Variable Declaration string name; int number; }; //Main Function int main() { // Object Creation For Class person obj; //Get Input Values For Object Variables cout<<"Enter the Name :"; cin>>obj.name; cout<<"Enter the Number :"; cin>>obj.number; //Show the Output cout << obj.name << ": " << obj.number << endl; getch(); return 0; }
  • 7. ACCESS SPECIFIERS Private: Private member defines that the members or methods can be accessed within the same class only. Public: Public member defines that the variable or methods can be accessed at any where within the project. Protected: Protected member can be accessed to the class which is inherited by other class.  By default, all members and function of a class is private i.e if no access specifier is specified.
  • 8. SYNTAX OF DECLARING ACCESS MODIFIERS IN C++ class { private: // private members and function public: // public members and function protected: // protected members and function };
  • 9. ABSTRACTION  A model complex of a system that includes only the details essential to perspective of the viewerof the system.  An Abstraction is a model of a complex system that includes only the essential details.  Abstractions are the fundamental way that we manage complexity. Helps to managecomplexity of alarge system.  Support our quality goals of modifiability and reusability.
  • 10. #include<iostream.h> #include<conio.h> class sum { // hidden data from outside world private: int a,b,c; public: void add() { cout<<"Enter any two numbers: "; cin>>a>>b; c=a+b; cout<<"Sum: "<<c; } }; void main() { sum s; s.add(); getch(); }
  • 11. ENCAPSULATION  It can besaid Data binding.  Encapsulation is the method of combining the data and functions inside a class. This hides the data from being accessed from outside a class directly, only through the functions inside the class is able to access the information.
  • 12. #include <iostream.h> class Add { private: int x,y,r; public: int Addition(int x, int y) { r= x+y; return r; } void show( ) { cout << "The sum is::" << r << "n";} }s; void main() { Add s; s.Addition(10, 4); s.show(); } OUTPUT The sum is:: 14 integer values "x,y,r" of the class "Add" can be accessed only through the function "Addition". These integer values are encapsulated inside the class "Add". ENCAPSULATION
  • 13. INHERITANCE  Code reusability.  Process of forming newclassfrom anexisting class. Inheritedclassis called Base class.  classwhich inherits is called Derived class. PROS Helps to reducecode size. TYPESOFINHERITANCEIN C++ 1. Single inheritance. 2. Multilevel inheritance. 3. Hierarchical inheritance. 4. Multiple inheritance. 5. Hybrid inheritance.
  • 14. SINGLE INHERITANCE #include<iostream> using namespace std; class single_base { protected: int a,b; public: void get() { cout<<"Enter a & b"; cin>>a>>b; } }; class single_derived : public single_base { int c; public: void output() { c=a+b; cout<<"Sum:"<<c; } }; int main() { single_derived obj; obj.get(); obj.output(); return 0; } OUTPUT
  • 15. MULTILEVEL INHERITANCE #include <iostream> using namespace std; class A { public: void display() { cout<<"Base class content."; } }; class B : public A { }; class C : public B { }; int main() { C obj; obj.display(); return 0; }
  • 16. HIERARCHICAL INHERITANCE #include<iostream> using namespace std; class father // Base class derivation { int age; char name [20]; public: void get() { cout << "nEnter father's name:"; cin >> name; cout << "Enter father's age:"; cin >> age; } void show() { cout << "nnFather's name is " << name; cout << "nFather's age is " << age; } };
  • 17. class son : public father // First derived class derived from father class { int age; char name [20]; public: void get() { father :: get(); cout << "Enter son's name:"; cin >> name; cout << "Enter son's age:"; cin >> age; } void show() { father::show(); cout << "nSon's name is " << name; cout << "nSon's age is " << age; } };
  • 18. class daughter : public father // Second derived class derived from the father class { int age; char name [20]; public: void get() { father :: get(); cout << "Enter daughter's name:"; cin >> name; cout << "Enter daughter's age:"; cin >> age; } void show() { father::show(); cout << "nDaughter's name is " << name; cout << "nDaughter's age is " << age; } };
  • 19. int main () { son s1; daughter d1; s1.get(); d1.get(); s1.show(); d1.show(); return 0; } OUTPUT
  • 20. MULTIPLE INHERITANCE #include<iostream> using namespace std; class base1 { public: int a; void firstdata() { cout<<"Enter a:"; cin>>a; } }; class base2 { public: int b; void seconddata() { cout<<"Enter b:"; cin>>b; } }; class deriverd : public base1,public base2 { public: int c; void addition() { c=a+b; cout<<"Output"<<c; } }; int main() { deriverd obj; obj.firstdata(); obj.seconddata(); obj.addition(); return 0; } output
  • 21. HYBRID INHERITANCE #include<iostream.h> #include<conio.h> class arithmetic { protected: int num1, num2; public: void getdata() { cout<<"For Addition:"; cout<<"n Enter the first number: "; cin>>num1; cout<<"n Enter the second number: "; cin>>num2; } }; class plus: public arithmetic { protected: int sum; public: void add() { sum=num1+num2; } }; class minus { protected: int n1,n2,diff; public: void sub() { cout<<"n For Subtraction:"; cout<<"n Enter the first number: "; cin>>n1; cout<<"n Enter the second number: "; cin>>n2; diff=n1-n2; } }; class result:public plus, public minus { public: void display() { cout<<"n Sum of "<<num1<<" and "<<num2<<"= "<<sum; cout<<"n Difference of "<<n1<<" and "<<n2<<"= "<<diff; } }; void main() { result z; z.getdata(); z.add(); z.sub(); z.display(); getch(); } OUTPUT
  • 22. POLYMORPHISM • It makesthe code More readable. • Function with samenamebut different arguments. Functioning is different. • Poly refersto many. TypesofPolymorphism • Compile time Polymorphism. • Run time polymorphism.
  • 23. REAL LIFE EXAMPLE OF POLYMORPHISM IN C++
  • 24. COMPILE TIME POLYMORPHISM In C++ programming you can achieve compile time polymorphism in two way, which is given below;  Method overloading  Method overriding
  • 25. DIFFERENT WAYS TO OVERLOAD THE METHOD There are two ways to overload the method in C++  By changing number of arguments or parameters  By changing the data type
  • 26. METHOD OVERLOADING IN C++ BY CHANGING NUMBER OF ARGUMENTS #include<iostream.h> #include<conio.h> class Addition { public: void sum(int a, int b) { cout<<a+b; } void sum(int a, int b, int c) { cout<<a+b+c; } }; void main() { Addition obj; obj.sum(10, 20); cout<<endl; obj.sum(10, 20, 30); } OUTPUT
  • 27. METHOD OVERLOADING IN C++ BY CHANGING THE DATA TYPE #include<iostream.h> #include<conio.h> class Addition { public: void sum(int a, int b) { cout<<a+b; } void sum(float a, float b,float c) { cout<<a+b+c; } }; void main() { Addition obj; obj.sum(10, 20); cout<<endl; obj.sum(10.5,20.5,30.0); } OUTPUT
  • 28. METHOD OVERRIDING IN C++ #include<iostream.h> #include<conio.h> class Base { public: void show() { cout<<"Base class"; } }; class Derived:public Base { public: void show() { cout<<"Derived Class"; } }; void main() { Base b; //Base class object Derived d; //Derived class object b.show(); //Early Binding Occurs d.show(); getch(); } Base class Derived Class OUTPUT
  • 29. RUN TIME POLYMORPHISM(VIRTUAL FUNCTION) #include<iostream.h> #include<conio.h> class A { public: virtual void show() { cout<<"Hello base class"; } }; class B : public A { public: void show() { cout<<"Hello derive class"; } }; void main() { A obj1; B obj2; obj1.show(); obj2.show(); getch(); } OUTPUT
  • 30. FRIEND FUNCTION IN C++ #include<iostream.h> #include<conio.h> class employee { private: friend void sal(); }; void sal() { int salary=4000; cout<<"Salary: "<<salary; } void main() { employee e; sal(); getch(); } Salary: 4000 OUTPUT
  • 31. CONSTRUCTOR IN C++  A class constructor is a special member function of a class that is executed whenever we create new objects of that class. FEATURES OF CONSTRUCTOR  The same name as the class itself.  no return type. SYNTAX classname() { .... }
  • 32. Why use constructor ?  The main use of constructor is placing user defined values in place of default values. How Constructor eliminate default values ?  Constructor are mainly used for eliminate default values by user defined values, whenever we create an object of any class then its allocate memory for all the data members and initialize there default values. To eliminate these default values by user defined values we use constructor.
  • 33. #include<iostream.h> #include<conio.h> class sum { int a,b,c; sum() { a=10; b=20; c=a+b; cout<<"Sum: "<<c; } }; void main() { sum s; getch(); } CONSTRUCTOR OUTPUT Sum: 30
  • 34. DESTRUCTOR IN C++  Destructor is a member function which deletes an object. A destructor function is called automatically when the object goes out of scope: When destructor call  when program ends  when a block containing temporary variables ends  when a delete operator is called Features of destructor  The same name as the class but is preceded by a tilde (~)  no arguments and return no values Syntax ~classname() { ...... }
  • 35. #include<iostream.h> #include<conio.h> class sum { int a,b,c; sum() { a=10; b=20; c=a+b; cout<<"Sum: "<<c; } ~sum() { cout<<endl<<"call destructor"; } delay(500); }; void main() { sum s; cout<<endl<<"call main"; getch(); } Sum: 30 call main call destructor OUTPUT
  • 36. EXCEPTION HANDLING • Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch, and throw. • throw: A program throws an exception when a problem shows up. This is done using a throw keyword. • catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception. • try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.
  • 37. Try { // protected code } catch( ExceptionName e1 ) { // catch block } catch( ExceptionName e2 ) { // catch block } catch( ExceptionName eN ) { // catch block }
  • 38. WITHOUT EXCEPTION #include <iostream> #include <string> using namespace std; int main() { int numerator, denominator, result; cout <<"Enter the Numerator:"; cin>>numerator; cout<<"Enter the denominator:"; cin>>denominator; result = numerator/denominator; cout<<"nThe result of division is:" <<result; }
  • 39. WITH EXCEPTION #include <iostream> #include <string> using namespace std; int main() { int numerator, denominator, result; cout <<"Enter the Numerator:"; cin>>numerator; cout<<"Enter the denominator:"; cin>>denominator; try { if(denominator == 0) { throw denominator; } result = numerator/denominator; cout<<"nThe result of division is:" <<result; } catch(int num) { cout<<"You cannot enter "<<num<<" in denominator."; } }
  • 40. THIS POINTER #include <iostream> #include <conio.h> using namespace std; class sample { int a, b; public: void input(int a, int b) { this->a=a+b; this->b=a-b; } void output() { cout<<"a = "<<a<<endl<<"b = "<<b; } }; int main() { sample x; x.input(5,8); x.output(); getch(); return 0; }
  • 41. INLINE FUNCTION #include<iostream.h> #include<conio.h> int a,b; inline int sum(int a,int b) { return(a+b); } int main() { int x,y; clrscr(); cout<<"two num"; cin>>x>>y; cout<<"sum of two num"<<sum(x,y); getch(); return 0; } Inline Function
  • 42. TEMPLATES • In c++ programming allows functions or class to work on more than one data type at once without writing different codes for different data types. • It is often uses in larger programs for the purpose of of code reusability and flexibility of program. It can used in two ways: 1. function Template 2. Class Template
  • 43. FUNCTION TEMPLATE A single function template can work on different types at once but, different functions are needed to perform identical task on different data types. Syntax: template<class type>ret-type func-name () { //body of the function }
  • 44. #include<iostream.h> #include<conio.h> template<class T> void show(T a) { cout<<“n a=“<<a; } void main() { clrscr(); show(‘n’); show(12.34); show(10); show(“nils”); getch(); } Output:
  • 45. CLASS TEMPLATE The general form of class templates is shown here: Syntax: template<class type>class class-name { // body of function }
  • 46. #include<iostream.h> #include<conio.h> template<class T> class a { private: T x; public: a() {} a(T a) { x=a; } Void show() { Cout<<“n x = “<<x; } }; Void main() { clrscr(); a<char>a1(‘B’); a1.show(); a<int>a2(10); a2.show(); a<float> a3(12.34f); a3.show(); a<double>a4(10.5); A4.show(); getch(); } Output: