SlideShare a Scribd company logo
1 of 34
• Presentation by- TANMAY P DHATRAK
• ROLL NO - 15
• Subject - OOP
• TOPIC– CONCEPT’S InOOP
• PRN - 190105181014
• Guided by – YogitaShewaleMa’am
Concepts in OOP
By
TANMAY DHATRAK
CLASSES AND OBJECTS
 The main purpose of C++ programming is to add object
orientation to the C programming language.
 A class is used to specify the form of an object and it combines
data representation and methods for manipulating that data
into one neat package.
 The data and functions within a class are called members of the
class.
 CLASS ACCESS MODIFIERS -
 Data hiding is one of the important features of Object Oriented
Programming which allows preventing the functions of a program to
access directly the internal representation of a class type.
 A class can have multiple public, protected, or private labeled sections.
Each section remains in effect until either another section label or the
closing right brace of the class body is seen.
 Public : Any class can access the features
 Protected : Any subclass can access the feature
 Private : No other class can access the feature
 CLASS ACCESS MODIFIERS
1. class Base {
2. public:
3. // public members go here
4.
5. protected:
6. // protected members go here
7.
8. private:
9. // private members go here
10. };
 THE PUBLIC MEMBERS EXAMPLE -
• A public member is accessible from anywhere outside the class but
within a program.
• #include <iostream>
• using namespace std;
• class Line
• {
• public:
• double length;
• void setLength( double len );
• double getLength( void );
• };
• // Member functions definitions
• double Line::getLength(void)
• {
• return length ;
• }
• void Line::setLength( double len )
• {
• length = len;
• }
• // Main function for the program
• int main( )
• { Line line;
• // set line length
• line.setLength(6.0);
• cout << "Length of line : " <<
line.getLength() <<endl;
• // set line length without member
function
• line.length = 10.0; // OK: because
length is public
• cout << "Length of line : " <<
line.length <<endl;
• return 0; }
OUTPUT –
Length of line : 6
Length of line : 10
 THE PRIVATE MEMBERS -
• A private member variable or function cannot be accessed, or even viewed
from outside the class. Only the class and friend functions can access
private members.
• By default all the members of a class would be private, for example
• in the following class width is a private member.
• class Box
• { double width;
• public:
• double length;
• void setWidth( double wid );
• double getWidth( void );
• };
 THE PRIVATE MEMBERS EXAMPLE -
 #include <iostream>
 using namespace std;
 class Box
 { public:
 double length;
 void setWidth( double wid );
 double getWidth( void );
 private:
 double width;
 };
 // Member functions definitions
 double Box::getWidth(void)
 {
 return width ;
 }
 void Box::setWidth( double wid ) {
 width = wid;
 }
 int main( ) {
 Box box;
 // set box length without member function
 box.length = 10.0; // OK: because length is public
 cout << "Length of box : " << box.length <<endl;
 
 // set box width without member function
 // box.width = 10.0; // Error: because width is private
 box.setWidth(10.0); // Use member function to set it.
 cout << "Width of box : " << box.getWidth() <<endl;
 return 0; }
OUTPUT –
Length of box : 10
Width of box : 10
 THE PROTECTED MEMBERS EXAMPLE -
 A protected member variable or function is very similar to a private member
but it provided one additional benefit that they can be accessed in child
classes which are called derived classes.
 #include <iostream>
 using namespace std;
 class Box
 {
 protected:
 double width;
 };
 THE PROTECTED MEMBERS EXAMPLE -
 class SmallBox:Box // SmallBox is the derived class.
 { public:
 void setSmallWidth( double wid );
 double getSmallWidth( void );
 };
 // Member functions of child class
 double SmallBox::getSmallWidth(void) {
 return width ;
 }
 THE PROTECTED MEMBERS EXAMPLE -
 void SmallBox::setSmallWidth( double wid ) {
 width = wid;
 }
 // Main function for the program
 int main( ) {
 SmallBox box;
 // set box width using member function
 box.setSmallWidth(5.0);
 cout << "Width of box : "<<
box.getSmallWidth() << endl;
 return 0;
 }
Width of box : 5
 CLASS DEFINITIONS -
 When you define a class, you define a blueprint for a data type.
This doesn't actually define any data, but it does define what the
class name means, that is, what an object of the class will consist
of and what operations can be performed on such an object.
 A class definition starts with the keyword class followed by the
class name;
 CLASS DEFINITIONS EXAMPLE -
1. class Box
2. {
3. public:
4. double length; // Length of a box
5. double breadth; // Breadth of a box
6. double height; // Height of a box
7. };
 ACCESSING THE DATA MEMBERS -
1. #include <iostream>
2. using namespace std;
3. class Box
4. {
5. public:
6. double length;// Length of a box
7. double breadth; // Breadth of a box
8. double height;// Height of a box
9. };
 ACCESSING THE DATA MEMBERS -
1. int main( )
2. {
// Declare Box1 of type Box
3. Box Box1;
4. Box Box2; // Declare Box2 of type Box
5. double volume = 0.0; // Store the volume of a box here
6. // box 1 specification
7. Box1.height = 5.0;
8. Box1.length = 6.0;
9. Box1.breadth = 7.0;
1. // box 2 specification
2. Box2.height = 10.0;
3. Box2.length = 12.0;
4. Box2.breadth = 13.0;
5. // volume of box 1
6. volume = Box1.height * Box1.length * Box1.breadth;
7. cout << "Volume of Box1 : " << volume <<endl;
8. // volume of box 2
9. volume = Box2.height * Box2.length * Box2.breadth;
10. cout << "Volume of Box2 : " << volume <<endl;
11. return 0;
12. }
Volume of Box1 : 210 Volume of
Box2 : 1560
 CLASS MEMBER FUNCTIONS -
 A member function of a class is a function that has its definition or its
prototype within the class definition like any other variable.
 class Box
 {
 public:
 double length;
 double breadth;
// Length of a box
// Breadth of a box
 double height; // Height of a box
 double getVolume(void);// Returns box volume
 };
 CLASS MEMBER FUNCTIONS -
// Length of a box
// Breadth of a box
// Height of a box
double getVolume(void)
{
return length * breadth * height;
}
Method 1
 class Box
 {
 public:
 double length;
 double breadth;
 double height;





 };
Method 2
 double Box::getVolume(void)
 {
 return length * breadth * height;
 }
 Box myBox; // Create an
object
 myBox.getVolume();
 // Call member function for the
object
 Member functions can be defined within the class
definition or separately using scope resolution operator, ::.
 INHERITANCE -
 “C++ is Multi Inheritance, unlike Java is Single inheritance”.
 Inheritance allows us to define a class in terms of another class, which
makes it easier to create and maintain an application. This also provides
an opportunity to reuse the code functionality and fast implementation
time.
 When creating a class, instead of writing completely new data
members and member functions, the programmer can designate that
the new class should inherit the members of an existing class. This
existing class is called the base class, and the new class is referred to as
the derived class.
 BASE & DERIVED CLASSES -
 A class can be derived from more than one classes, which means it
can inherit data and functions from multiple base classes.
 To define a derived class, we use a class derivation list to specify
the base class(es). A class derivation list names one or more base
classes and has the form:
 class derived-class: access-specifier base-class
 Where access-specifier is one of public, protected, or private.
 “base-class” is the name of a previously defined class.
 Consider a base class Shape and its derived class Rectangle:
 ACCESS CONTROL AND INHERITANCE
 A derived class can access all the non-private members of its base
class.
 A derived class inherits all base class methods with the following
 exceptions:
 Constructors, destructors and copy constructors of the base class.
 Overloaded operators of the base class.
 The friend functions of the base class.
 TYPE OF INHERITANCE [ PUBLIC] -
 Public Inheritance:
 When deriving a class from a public base class, public members of
the base class become public members of the derived class and
protected members of the base class become protected members of
the derived class.
 A base class's privatemembers are never accessible directly from a
derived class, but can be accessed through calls to the public and
protected members of the base class.
 TYPE OF INHERITANCE [ PROTECTED AND
PRIVATE ]-
 Protected Inheritance
 When deriving from a protected base class, public and protected
members of the base class become protected members of the derived
class.
 Private Inheritance
 When deriving from a private base class, public and protected
members of the base class become private members of the derived
class.
 FUNCTION OVERLOADING -
 An overloaded declaration is a declaration that had been
declared with the same name as a previously declared
declaration in the same scope, except that both declarations
have different arguments and obviously different definition
(implementation).
 POLYMORPHISM -
 The word polymorphism means having many forms. Typically,
polymorphism occurs when there is a hierarchy of classes and
they are related by inheritance.
 C++ polymorphism means that a call to a member function will
cause a different function to be executed depending on the type
of object that invokes the function
 POLYMORPHISM [STATIC RESOLUTION ] -
 Output of previous example:
 WHY !!!
 The reason for the incorrect output is that the call of the function
area() is being set once by the compiler as the version defined in the
base class. This is called static resolution of the function call, or
static linkage - the function call is fixed before the program is
executed. This is also sometimes called early binding because the
area() function is set during the compilation of the program.
Parent class area Parent class area
 POLYMORPHISM -
 class Shape {
 protected:
 int width, height;
 public:
 Shape( int a=0, int b=0) {
 width = a;
 height = b;
 }
 virtual int area() {
 cout << "Parent class area :" <<endl;
 return 0;
 }
 };
Rectangle class area Triangle class
area
This time, the compiler looks at
the contents of the pointer
instead of it's type. Hence, since
addresses of objects of tri and rec
classes are stored in *shape the
respective area() function is
called.
 VIRTUAL FUNCTION -
 A virtual function is a function in a base class that is declared
using the keyword virtual. Defining in a base class a virtual
function, with another version in a derived class, signals to the
compiler that we don't want static linkage for this function.
 PURE VIRTUAL FUNCTIONS -
 class Shape {
 protected:
 int width, height;
 public:
 Shape( int a=0, int b=0) {
 width = a;
 height = b;
 }
 // pure virtual function
 virtual int area() = 0;
 };
The = 0 tells the compiler that
the function has no body
This virtual function will be
called pure virtual function.
It's possible that you'd want to
include a virtual function in a base
class so that it may be redefined
in a derived class to suit the
objects of that class, but that
there is no meaningful definition
you could give for the function in
the base class.
 DATA ENCAPSULATION -
 Data encapsulation led to the important OOP concept of data
hiding.
 Encapsulation is an Object Oriented Programming concept that
binds together the data and functions that manipulate the data,
and that keeps both safe from outside interference and
misuse.
 Data encapsulation is a mechanism of bundling the data, and
the functions that use them.
o C++ supports the properties of encapsulation and data
hiding through the creation of user-defined types, called
classes.
o We already have studied that a class can contain private,
protected and public members.
o By default, all items defined in a class are private
 DATA ENCAPSULATION -
 DATA ENCAPSULATION -
// Length of a box
// Breadth of a box
// Height of a box
 class Box {
 public:
 double getVolume(void) {
 return length * breadth * height;
 }
 private:
 double length;
 double breadth;
 double height;
 };
The variables length, breadth,
and height are private.
This means that they can be
accessed only by other members
of the Box class, and not by any
other part of your program.
This is one way
encapsulation is achieved.
THANK YOU

More Related Content

Similar to OOP.ppt

Similar to OOP.ppt (20)

inheritance
inheritanceinheritance
inheritance
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Class and object
Class and objectClass and object
Class and object
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
27c
27c27c
27c
 
27csharp
27csharp27csharp
27csharp
 
Access specifier
Access specifierAccess specifier
Access specifier
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
lecture3.pptx
lecture3.pptxlecture3.pptx
lecture3.pptx
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 

Recently uploaded

Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 

Recently uploaded (20)

Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 

OOP.ppt

  • 1. • Presentation by- TANMAY P DHATRAK • ROLL NO - 15 • Subject - OOP • TOPIC– CONCEPT’S InOOP • PRN - 190105181014 • Guided by – YogitaShewaleMa’am
  • 3. CLASSES AND OBJECTS  The main purpose of C++ programming is to add object orientation to the C programming language.  A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package.  The data and functions within a class are called members of the class.
  • 4.  CLASS ACCESS MODIFIERS -  Data hiding is one of the important features of Object Oriented Programming which allows preventing the functions of a program to access directly the internal representation of a class type.  A class can have multiple public, protected, or private labeled sections. Each section remains in effect until either another section label or the closing right brace of the class body is seen.  Public : Any class can access the features  Protected : Any subclass can access the feature  Private : No other class can access the feature
  • 5.  CLASS ACCESS MODIFIERS 1. class Base { 2. public: 3. // public members go here 4. 5. protected: 6. // protected members go here 7. 8. private: 9. // private members go here 10. };
  • 6.  THE PUBLIC MEMBERS EXAMPLE - • A public member is accessible from anywhere outside the class but within a program. • #include <iostream> • using namespace std; • class Line • { • public: • double length; • void setLength( double len ); • double getLength( void ); • };
  • 7. • // Member functions definitions • double Line::getLength(void) • { • return length ; • } • void Line::setLength( double len ) • { • length = len; • } • // Main function for the program • int main( ) • { Line line; • // set line length • line.setLength(6.0); • cout << "Length of line : " << line.getLength() <<endl; • // set line length without member function • line.length = 10.0; // OK: because length is public • cout << "Length of line : " << line.length <<endl; • return 0; } OUTPUT – Length of line : 6 Length of line : 10
  • 8.  THE PRIVATE MEMBERS - • A private member variable or function cannot be accessed, or even viewed from outside the class. Only the class and friend functions can access private members. • By default all the members of a class would be private, for example • in the following class width is a private member. • class Box • { double width; • public: • double length; • void setWidth( double wid ); • double getWidth( void ); • };
  • 9.  THE PRIVATE MEMBERS EXAMPLE -  #include <iostream>  using namespace std;  class Box  { public:  double length;  void setWidth( double wid );  double getWidth( void );  private:  double width;  };  // Member functions definitions  double Box::getWidth(void)  {  return width ;  }  void Box::setWidth( double wid ) {  width = wid;  }  int main( ) {  Box box;  // set box length without member function  box.length = 10.0; // OK: because length is public  cout << "Length of box : " << box.length <<endl;    // set box width without member function  // box.width = 10.0; // Error: because width is private  box.setWidth(10.0); // Use member function to set it.  cout << "Width of box : " << box.getWidth() <<endl;  return 0; } OUTPUT – Length of box : 10 Width of box : 10
  • 10.  THE PROTECTED MEMBERS EXAMPLE -  A protected member variable or function is very similar to a private member but it provided one additional benefit that they can be accessed in child classes which are called derived classes.  #include <iostream>  using namespace std;  class Box  {  protected:  double width;  };
  • 11.  THE PROTECTED MEMBERS EXAMPLE -  class SmallBox:Box // SmallBox is the derived class.  { public:  void setSmallWidth( double wid );  double getSmallWidth( void );  };  // Member functions of child class  double SmallBox::getSmallWidth(void) {  return width ;  }
  • 12.  THE PROTECTED MEMBERS EXAMPLE -  void SmallBox::setSmallWidth( double wid ) {  width = wid;  }  // Main function for the program  int main( ) {  SmallBox box;  // set box width using member function  box.setSmallWidth(5.0);  cout << "Width of box : "<< box.getSmallWidth() << endl;  return 0;  } Width of box : 5
  • 13.  CLASS DEFINITIONS -  When you define a class, you define a blueprint for a data type. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.  A class definition starts with the keyword class followed by the class name;
  • 14.  CLASS DEFINITIONS EXAMPLE - 1. class Box 2. { 3. public: 4. double length; // Length of a box 5. double breadth; // Breadth of a box 6. double height; // Height of a box 7. };
  • 15.  ACCESSING THE DATA MEMBERS - 1. #include <iostream> 2. using namespace std; 3. class Box 4. { 5. public: 6. double length;// Length of a box 7. double breadth; // Breadth of a box 8. double height;// Height of a box 9. };
  • 16.  ACCESSING THE DATA MEMBERS - 1. int main( ) 2. { // Declare Box1 of type Box 3. Box Box1; 4. Box Box2; // Declare Box2 of type Box 5. double volume = 0.0; // Store the volume of a box here 6. // box 1 specification 7. Box1.height = 5.0; 8. Box1.length = 6.0; 9. Box1.breadth = 7.0;
  • 17. 1. // box 2 specification 2. Box2.height = 10.0; 3. Box2.length = 12.0; 4. Box2.breadth = 13.0; 5. // volume of box 1 6. volume = Box1.height * Box1.length * Box1.breadth; 7. cout << "Volume of Box1 : " << volume <<endl; 8. // volume of box 2 9. volume = Box2.height * Box2.length * Box2.breadth; 10. cout << "Volume of Box2 : " << volume <<endl; 11. return 0; 12. } Volume of Box1 : 210 Volume of Box2 : 1560
  • 18.  CLASS MEMBER FUNCTIONS -  A member function of a class is a function that has its definition or its prototype within the class definition like any other variable.  class Box  {  public:  double length;  double breadth; // Length of a box // Breadth of a box  double height; // Height of a box  double getVolume(void);// Returns box volume  };
  • 19.  CLASS MEMBER FUNCTIONS - // Length of a box // Breadth of a box // Height of a box double getVolume(void) { return length * breadth * height; } Method 1  class Box  {  public:  double length;  double breadth;  double height;       }; Method 2  double Box::getVolume(void)  {  return length * breadth * height;  }  Box myBox; // Create an object  myBox.getVolume();  // Call member function for the object  Member functions can be defined within the class definition or separately using scope resolution operator, ::.
  • 20.  INHERITANCE -  “C++ is Multi Inheritance, unlike Java is Single inheritance”.  Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.  When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
  • 21.  BASE & DERIVED CLASSES -  A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes.  To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form:  class derived-class: access-specifier base-class  Where access-specifier is one of public, protected, or private.  “base-class” is the name of a previously defined class.  Consider a base class Shape and its derived class Rectangle:
  • 22.  ACCESS CONTROL AND INHERITANCE  A derived class can access all the non-private members of its base class.  A derived class inherits all base class methods with the following  exceptions:  Constructors, destructors and copy constructors of the base class.  Overloaded operators of the base class.  The friend functions of the base class.
  • 23.  TYPE OF INHERITANCE [ PUBLIC] -  Public Inheritance:  When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class.  A base class's privatemembers are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
  • 24.  TYPE OF INHERITANCE [ PROTECTED AND PRIVATE ]-  Protected Inheritance  When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.  Private Inheritance  When deriving from a private base class, public and protected members of the base class become private members of the derived class.
  • 25.  FUNCTION OVERLOADING -  An overloaded declaration is a declaration that had been declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation).
  • 26.  POLYMORPHISM -  The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.  C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function
  • 27.  POLYMORPHISM [STATIC RESOLUTION ] -  Output of previous example:  WHY !!!  The reason for the incorrect output is that the call of the function area() is being set once by the compiler as the version defined in the base class. This is called static resolution of the function call, or static linkage - the function call is fixed before the program is executed. This is also sometimes called early binding because the area() function is set during the compilation of the program. Parent class area Parent class area
  • 28.  POLYMORPHISM -  class Shape {  protected:  int width, height;  public:  Shape( int a=0, int b=0) {  width = a;  height = b;  }  virtual int area() {  cout << "Parent class area :" <<endl;  return 0;  }  }; Rectangle class area Triangle class area This time, the compiler looks at the contents of the pointer instead of it's type. Hence, since addresses of objects of tri and rec classes are stored in *shape the respective area() function is called.
  • 29.  VIRTUAL FUNCTION -  A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don't want static linkage for this function.
  • 30.  PURE VIRTUAL FUNCTIONS -  class Shape {  protected:  int width, height;  public:  Shape( int a=0, int b=0) {  width = a;  height = b;  }  // pure virtual function  virtual int area() = 0;  }; The = 0 tells the compiler that the function has no body This virtual function will be called pure virtual function. It's possible that you'd want to include a virtual function in a base class so that it may be redefined in a derived class to suit the objects of that class, but that there is no meaningful definition you could give for the function in the base class.
  • 31.  DATA ENCAPSULATION -  Data encapsulation led to the important OOP concept of data hiding.  Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse.  Data encapsulation is a mechanism of bundling the data, and the functions that use them.
  • 32. o C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. o We already have studied that a class can contain private, protected and public members. o By default, all items defined in a class are private  DATA ENCAPSULATION -
  • 33.  DATA ENCAPSULATION - // Length of a box // Breadth of a box // Height of a box  class Box {  public:  double getVolume(void) {  return length * breadth * height;  }  private:  double length;  double breadth;  double height;  }; The variables length, breadth, and height are private. This means that they can be accessed only by other members of the Box class, and not by any other part of your program. This is one way encapsulation is achieved.