SlideShare a Scribd company logo
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:
Ad

More Related Content

What's hot (20)

Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Ramish Suleman
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS
abhishek kumar
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
Mohamed Loey
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
Boopathi K
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
Rabin BK
 
File in C language
File in C languageFile in C language
File in C language
Manash Kumar Mondal
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
baabtra.com - No. 1 supplier of quality freshers
 
Polymorphism in Python
Polymorphism in Python Polymorphism in Python
Polymorphism in Python
Home
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
Oops ppt
Oops pptOops ppt
Oops ppt
abhayjuneja
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 
Java program structure
Java program structure Java program structure
Java program structure
Mukund Kumar Bharti
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS
abhishek kumar
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
Mohamed Loey
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vishal Patil
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
Boopathi K
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
Rabin BK
 
Polymorphism in Python
Polymorphism in Python Polymorphism in Python
Polymorphism in Python
Home
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 

Viewers also liked (14)

Java
JavaJava
Java
kavirishi
 
Presentation
PresentationPresentation
Presentation
Fiaz Khokhar
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
teach4uin
 
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
Thooyavan Venkatachalam
 
Basic elements of java
Basic elements of java Basic elements of java
Basic elements of java
Ahmad Idrees
 
Java notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esectionJava notes(OOP) jkuat IT esection
Java notes(OOP) jkuat IT esection
Arc Keepers Solutions
 
Java Datatypes
Java DatatypesJava Datatypes
Java Datatypes
Mayank Aggarwal
 
Java features
Java featuresJava features
Java features
myrajendra
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
Python: Multiple Inheritance
Python: Multiple InheritancePython: Multiple Inheritance
Python: Multiple Inheritance
Damian 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 java
Hawkman Academy
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
advanced security system for women
advanced security system for womenadvanced security system for women
advanced security system for women
Thooyavan Venkatachalam
 
Type Casting in C++
Type Casting in C++Type Casting in C++
Type Casting in C++
Sachin Sharma
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
teach4uin
 
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
Thooyavan 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 Java
Tushar B Kute
 
Python: Multiple Inheritance
Python: Multiple InheritancePython: Multiple Inheritance
Python: Multiple Inheritance
Damian 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 java
Hawkman Academy
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Ad

Similar to 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
DeepasCSE
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
AhalyaR
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
RutujaTandalwade
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
Amritsinghmehra
 
OOP unit II inheritance.pptx object oriented programming
OOP unit II inheritance.pptx object oriented programmingOOP unit II inheritance.pptx object oriented programming
OOP unit II inheritance.pptx object oriented programming
Srishti951154
 
Object Oriented Programming (OOP) using C++ - Lecture 4
Object Oriented Programming (OOP) using C++ - Lecture 4Object Oriented Programming (OOP) using C++ - Lecture 4
Object Oriented Programming (OOP) using C++ - Lecture 4
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Inheritance
InheritanceInheritance
Inheritance
Misbah Aazmi
 
Virtual function
Virtual functionVirtual function
Virtual function
harman kaur
 
05 Object Oriented Concept Presentation.pptx
05 Object Oriented Concept Presentation.pptx05 Object Oriented Concept Presentation.pptx
05 Object Oriented Concept Presentation.pptx
ToranSahu18
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
AbhimanyuKumarYadav3
 
Object oriented programming 2
Object oriented programming 2Object oriented programming 2
Object oriented programming 2
Aadil Ansari
 
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
Muhammed Thanveer M
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
Javier Gonzalez-Sanchez
 
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
sindhus795217
 
Inheritance
InheritanceInheritance
Inheritance
GowriLatha1
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
BArulmozhi
 
C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)
Ananda Kumar HN
 
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
OOP_EXPLAINED_example_of_cod_and_explainations.pdfOOP_EXPLAINED_example_of_cod_and_explainations.pdf
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
DerekDixmanChakowela
 
labwork practice on inhetitance-1.pptx
labwork  practice on  inhetitance-1.pptxlabwork  practice on  inhetitance-1.pptx
labwork practice on inhetitance-1.pptx
soniasharmafdp
 
Object Oriented Programming (OOP) using C++ - Lecture 3
Object Oriented Programming (OOP) using C++ - Lecture 3Object Oriented Programming (OOP) using C++ - Lecture 3
Object Oriented Programming (OOP) using C++ - Lecture 3
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
DeepasCSE
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
AhalyaR
 
OOP unit II inheritance.pptx object oriented programming
OOP unit II inheritance.pptx object oriented programmingOOP unit II inheritance.pptx object oriented programming
OOP unit II inheritance.pptx object oriented programming
Srishti951154
 
Virtual function
Virtual functionVirtual function
Virtual function
harman kaur
 
05 Object Oriented Concept Presentation.pptx
05 Object Oriented Concept Presentation.pptx05 Object Oriented Concept Presentation.pptx
05 Object Oriented Concept Presentation.pptx
ToranSahu18
 
Object oriented programming 2
Object oriented programming 2Object oriented programming 2
Object oriented programming 2
Aadil Ansari
 
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
Muhammed Thanveer M
 
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf22 scheme  OOPs with C++ BCS306B_module2.pdfmodule2.pdf
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
sindhus795217
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
BArulmozhi
 
C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)C++ prgms 5th unit (inheritance ii)
C++ prgms 5th unit (inheritance ii)
Ananda Kumar HN
 
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
OOP_EXPLAINED_example_of_cod_and_explainations.pdfOOP_EXPLAINED_example_of_cod_and_explainations.pdf
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
DerekDixmanChakowela
 
labwork practice on inhetitance-1.pptx
labwork  practice on  inhetitance-1.pptxlabwork  practice on  inhetitance-1.pptx
labwork practice on inhetitance-1.pptx
soniasharmafdp
 
Ad

Recently uploaded (20)

Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 

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:
  翻译: