SlideShare a Scribd company logo
1 of 66
Chapter 13
 Procedural programming focuses on the process/actions
that occur in a program
 Object-Oriented programming is based on the data and
the functions that operate on it. Objects are instances of
ADTs that represent the data and its functions
 If the data structures change, many functions must also
be changed
 Programs that are based on complex function hierarchies
are:
 difficult to understand and maintain
 difficult to modify and extend
 easy to break
 class: like a struct (allows bundling of related variables),
but variables and functions in the class can have different
properties than in a struct
 object: an instance of a class, in the same way that a
variable can be an instance of a struct
 A Class is like a blueprint and objects are like houses built
from the blueprint
 attributes: members of a class
 methods or behaviors: member functions of a class
 data hiding: restricting access to certain members of an
object
 public interface: members of an object that are available
outside of the object. This allows the object to provide
access to some data and functions without sharing its
internal details and design, and provides some protection
from data corruption
 Objects are created from a class
 Format:
class ClassName
{
declaration;
declaration;
};
 Used to control access to members of the class
 public: can be accessed by functions outside of the class
 private: can only be called by or accessed by functions
that are members of the class
Private Members
Public Members
 Can be listed in any order in a class
 Can appear multiple times in a class
 If not specified, the default is private
 const appearing after the parentheses in a member
function declaration specifies that the function will not
change any data in the calling object.
 When defining a member function:
 Put prototype in class declaration
 Define function using class name and scope resolution
operator (::)
int Rectangle::setWidth(double w)
{
width = w;
}
 Mutator: a member function that stores a value in a
private member variable, or changes its value in some
way
 Accessor: function that retrieves a value from a private
member variable. Accessors do not change an object's
data, so they should be marked const.
 An object is an instance of a class
 Defined like structure variables:
Rectangle r;
 Access members using dot operator:
r.setWidth(5.2);
cout << r.getWidth();
 Compiler error if attempt to access private
member using dot operator
Program 13-1 (Continued)
Program 13-1 (Continued)
Program 13-1 (Continued)
 Some data is the result of a calculation.
 In the Rectangle class the area of a rectangle is calculated.
 length x width
 If we were to use an area variable here in the Rectangle class, its
value would be dependent on the length and the width.
 If we change length or width without updating area, then area would
become stale.
 To avoid stale data, it is best to calculate the value of that data
within a member function rather than store it in a variable.
 Can define a pointer to an object:
Rectangle *rPtr = nullptr;
 Can access public members via pointer:
rPtr = &otherRectangle;
rPtr->setLength(12.5);
cout << rPtr->getLength() << endl;
 We can also use a pointer to dynamically allocate an
object.
 Making data members private provides data protection
 Data can be accessed only through public functions
 Public functions define the class’s public interface
Code outside the class must use the class's public
member functions to interact with the object.
 Place class declaration in a header file that serves
as the class specification file. Name the file
ClassName.h, for example, Rectangle.h
 Place member function definitions in
ClassName.cpp, for example, Rectangle.cpp File
should #include the class specification file
 Programs that use the class must #include the
class specification file, and be compiled and linked
with the member function definitions
 Member functions can be defined
 inline: in class declaration
 after the class declaration
 Inline appropriate for short function bodies:
int getWidth() const
{ return width; }
1 // Specification file for the Rectangle class
2 // This version uses some inline member functions.
3 #ifndef RECTANGLE_H
4 #define RECTANGLE_H
5
6 class Rectangle
7 {
8 private:
9 double width;
10 double length;
11 public:
12 void setWidth(double);
13 void setLength(double);
14
15 double getWidth() const
16 { return width; }
17
18 double getLength() const
19 { return length; }
20
21 double getArea() const
22 { return width * length; }
23 };
24 #endif
 Regular functions – when called, compiler stores return
address of call, allocates memory for local variables, etc.
 Code for an inline function is copied into program in place
of call – larger executable program, but no function call
overhead, hence faster execution
 Member function that is automatically called when an
object is created
 Purpose is to construct an object
 Constructor function name is class name
 Has no return type
Continues...
Contents of Rectangle.ccpVersion3
(continued)
 A default constructor is a constructor that takes no arguments.
 If you write a class with no constructor at all, C++ will write a default
constructor for you, one that does nothing.
 A simple instantiation of a class (with no arguments) calls the
default constructor:
Rectangle r;
 To create a constructor that takes arguments:
 indicate parameters in prototype:
Rectangle(double, double);
 Use parameters in the definition:
Rectangle::Rectangle(double w, double len)
{
width = w;
length = len;
}
 You can pass arguments to the constructor when you
create an object:
Rectangle r(10, 5);
 If all of a constructor's parameters have default
arguments, then it is a default constructor. For example:
Rectangle(double = 0, double = 0);
 Creating an object and passing no arguments will cause
this constructor to execute:
Rectangle r;
 When all of a class's constructors require arguments, then
the class has NO default constructor.
 When this is the case, you must pass the required
arguments to the constructor when creating an object.
 Member function automatically called when an object is
destroyed
 Destructor name is ~classname, e.g., ~Rectangle
 Has no return type; takes no arguments
 Only one destructor per class, i.e., it cannot be
overloaded
 If constructor allocates dynamic memory, destructor
should release it
Contents of InventoryItem.hVersion1
(continued)
 When an object is dynamically allocated with the new
operator, its constructor executes:
Rectangle *r = new Rectangle(10, 20);
 When the object is destroyed, its destructor executes:
delete r;
 A class can have more than one constructor
 Overloaded constructors in a class must have different
parameter lists:
Rectangle();
Rectangle(double);
Rectangle(double, double);
Continues...
 Do not provide more than one default constructor for a
class: one that takes no arguments and one that has
default arguments for all parameters
Square();
Square(int = 0); // will not compile
 Since a destructor takes no arguments, there can only be
one destructor for a class
 Non-constructor member functions can also be
overloaded:
void setCost(double);
void setCost(char *);
 Must have unique parameter lists as for constructors
 A private member function can only be called by another
member function
 It is used for internal processing by the class, not for use
outside of the class
 See the createDescription function in ContactInfo.h
(Version 2)
 Objects can be the elements of an array:
InventoryItem inventory[40];
 Default constructor for object is used when array is
defined
 Must use initializer list to invoke constructor that takes
arguments:
InventoryItem inventory[3] =
{ "Hammer", "Wrench", "Pliers" };
 If the constructor requires more than one argument, the
initializer must take the form of a function call:
 It isn't necessary to call the same constructor for each
object in an array:
 Objects in an array are referenced using subscripts
 Member functions are referenced using dot notation:
inventory[2].setUnits(30);
cout << inventory[2].getUnits();
Program 13-14 (Continued)
 UML stands for Unified Modeling Language.
 The UML provides a set of standard diagrams for
graphically depicting object-oriented systems
 A UML diagram for a class has three main sections.
class Rectangle
{
private:
double width;
double length;
public:
bool setWidth(double);
bool setLength(double);
double getWidth() const;
double getLength() const;
double getArea() const;
};
 In UML you indicate a private member with a minus (-)
and a public member with a plus(+).
These member variables are
private.
These member functions are
public.
 To indicate the data type of a member variable, place a
colon followed by the name of the data type after the
name of the variable.
- width : double
- length : double
 To indicate the data type of a function’s parameter
variable, place a colon followed by the name of the data
type after the name of the variable.
+ setWidth(w : double)
 To indicate the data type of a function’s return value, place
a colon followed by the name of the data type after the
function’s parameter list.
+ setWidth(w : double) : void
Constructors
Destructor
No return type listed for
constructors or destructors

More Related Content

What's hot

Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIEduardo Bergavera
 
Ch.1 oop introduction, classes and objects
Ch.1 oop introduction, classes and objectsCh.1 oop introduction, classes and objects
Ch.1 oop introduction, classes and objectsITNet
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console ProgramHock Leng PUAH
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3Atif Khan
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IEduardo Bergavera
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objectsmaznabili
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++M Hussnain Ali
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects newlykado0dles
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++Hoang Nguyen
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programmingnirajmandaliya
 
Unit iii vb_study_materials
Unit iii vb_study_materialsUnit iii vb_study_materials
Unit iii vb_study_materialsgayaramesh
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5Mahmoud Ouf
 
Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summaryEduardo Bergavera
 

What's hot (20)

Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Ch.1 oop introduction, classes and objects
Ch.1 oop introduction, classes and objectsCh.1 oop introduction, classes and objects
Ch.1 oop introduction, classes and objects
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console Program
 
Unit ii
Unit   iiUnit   ii
Unit ii
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 
Intake 38 5
Intake 38 5Intake 38 5
Intake 38 5
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
Structures
StructuresStructures
Structures
 
java tutorial 3
 java tutorial 3 java tutorial 3
java tutorial 3
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
 
java tutorial 2
 java tutorial 2 java tutorial 2
java tutorial 2
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Unit iii vb_study_materials
Unit iii vb_study_materialsUnit iii vb_study_materials
Unit iii vb_study_materials
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5
 
Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summary
 

Viewers also liked

Women's right to land & housing
Women's right to land & housingWomen's right to land & housing
Women's right to land & housingVIBHUTI PATEL
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingfarhan amjad
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-javaDeepak Singh
 
C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismJussi Pohjolainen
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)farhan amjad
 

Viewers also liked (6)

Functions
FunctionsFunctions
Functions
 
Women's right to land & housing
Women's right to land & housingWomen's right to land & housing
Women's right to land & housing
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-java
 
C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphism
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
 

Similar to Chapter 13 introduction to classes

3 functions and class
3   functions and class3   functions and class
3 functions and classtrixiacruz
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classDeepak Singh
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsRai University
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objectsRai University
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01Zafor Iqbal
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)nirajmandaliya
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures topu93
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture topu93
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
Assignment Examples Final 07 Oct
Assignment Examples Final 07 OctAssignment Examples Final 07 Oct
Assignment Examples Final 07 OctSriram Raj
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#ANURAG SINGH
 

Similar to Chapter 13 introduction to classes (20)

C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
 
Class and object
Class and objectClass and object
Class and object
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
 
Class and object
Class and objectClass and object
Class and object
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
My c++
My c++My c++
My c++
 
Assignment Examples Final 07 Oct
Assignment Examples Final 07 OctAssignment Examples Final 07 Oct
Assignment Examples Final 07 Oct
 
C++ language
C++ languageC++ language
C++ language
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 

Chapter 13 introduction to classes

  • 2.  Procedural programming focuses on the process/actions that occur in a program  Object-Oriented programming is based on the data and the functions that operate on it. Objects are instances of ADTs that represent the data and its functions
  • 3.  If the data structures change, many functions must also be changed  Programs that are based on complex function hierarchies are:  difficult to understand and maintain  difficult to modify and extend  easy to break
  • 4.  class: like a struct (allows bundling of related variables), but variables and functions in the class can have different properties than in a struct  object: an instance of a class, in the same way that a variable can be an instance of a struct
  • 5.  A Class is like a blueprint and objects are like houses built from the blueprint
  • 6.  attributes: members of a class  methods or behaviors: member functions of a class
  • 7.  data hiding: restricting access to certain members of an object  public interface: members of an object that are available outside of the object. This allows the object to provide access to some data and functions without sharing its internal details and design, and provides some protection from data corruption
  • 8.  Objects are created from a class  Format: class ClassName { declaration; declaration; };
  • 9.
  • 10.  Used to control access to members of the class  public: can be accessed by functions outside of the class  private: can only be called by or accessed by functions that are members of the class
  • 12.  Can be listed in any order in a class  Can appear multiple times in a class  If not specified, the default is private
  • 13.  const appearing after the parentheses in a member function declaration specifies that the function will not change any data in the calling object.
  • 14.  When defining a member function:  Put prototype in class declaration  Define function using class name and scope resolution operator (::) int Rectangle::setWidth(double w) { width = w; }
  • 15.  Mutator: a member function that stores a value in a private member variable, or changes its value in some way  Accessor: function that retrieves a value from a private member variable. Accessors do not change an object's data, so they should be marked const.
  • 16.  An object is an instance of a class  Defined like structure variables: Rectangle r;  Access members using dot operator: r.setWidth(5.2); cout << r.getWidth();  Compiler error if attempt to access private member using dot operator
  • 17.
  • 21.  Some data is the result of a calculation.  In the Rectangle class the area of a rectangle is calculated.  length x width  If we were to use an area variable here in the Rectangle class, its value would be dependent on the length and the width.  If we change length or width without updating area, then area would become stale.  To avoid stale data, it is best to calculate the value of that data within a member function rather than store it in a variable.
  • 22.  Can define a pointer to an object: Rectangle *rPtr = nullptr;  Can access public members via pointer: rPtr = &otherRectangle; rPtr->setLength(12.5); cout << rPtr->getLength() << endl;
  • 23.  We can also use a pointer to dynamically allocate an object.
  • 24.  Making data members private provides data protection  Data can be accessed only through public functions  Public functions define the class’s public interface
  • 25. Code outside the class must use the class's public member functions to interact with the object.
  • 26.  Place class declaration in a header file that serves as the class specification file. Name the file ClassName.h, for example, Rectangle.h  Place member function definitions in ClassName.cpp, for example, Rectangle.cpp File should #include the class specification file  Programs that use the class must #include the class specification file, and be compiled and linked with the member function definitions
  • 27.  Member functions can be defined  inline: in class declaration  after the class declaration  Inline appropriate for short function bodies: int getWidth() const { return width; }
  • 28. 1 // Specification file for the Rectangle class 2 // This version uses some inline member functions. 3 #ifndef RECTANGLE_H 4 #define RECTANGLE_H 5 6 class Rectangle 7 { 8 private: 9 double width; 10 double length; 11 public: 12 void setWidth(double); 13 void setLength(double); 14 15 double getWidth() const 16 { return width; } 17 18 double getLength() const 19 { return length; } 20 21 double getArea() const 22 { return width * length; } 23 }; 24 #endif
  • 29.  Regular functions – when called, compiler stores return address of call, allocates memory for local variables, etc.  Code for an inline function is copied into program in place of call – larger executable program, but no function call overhead, hence faster execution
  • 30.  Member function that is automatically called when an object is created  Purpose is to construct an object  Constructor function name is class name  Has no return type
  • 31.
  • 34.
  • 35.  A default constructor is a constructor that takes no arguments.  If you write a class with no constructor at all, C++ will write a default constructor for you, one that does nothing.  A simple instantiation of a class (with no arguments) calls the default constructor: Rectangle r;
  • 36.  To create a constructor that takes arguments:  indicate parameters in prototype: Rectangle(double, double);  Use parameters in the definition: Rectangle::Rectangle(double w, double len) { width = w; length = len; }
  • 37.  You can pass arguments to the constructor when you create an object: Rectangle r(10, 5);
  • 38.  If all of a constructor's parameters have default arguments, then it is a default constructor. For example: Rectangle(double = 0, double = 0);  Creating an object and passing no arguments will cause this constructor to execute: Rectangle r;
  • 39.  When all of a class's constructors require arguments, then the class has NO default constructor.  When this is the case, you must pass the required arguments to the constructor when creating an object.
  • 40.  Member function automatically called when an object is destroyed  Destructor name is ~classname, e.g., ~Rectangle  Has no return type; takes no arguments  Only one destructor per class, i.e., it cannot be overloaded  If constructor allocates dynamic memory, destructor should release it
  • 41.
  • 43.
  • 44.  When an object is dynamically allocated with the new operator, its constructor executes: Rectangle *r = new Rectangle(10, 20);  When the object is destroyed, its destructor executes: delete r;
  • 45.  A class can have more than one constructor  Overloaded constructors in a class must have different parameter lists: Rectangle(); Rectangle(double); Rectangle(double, double);
  • 47.
  • 48.  Do not provide more than one default constructor for a class: one that takes no arguments and one that has default arguments for all parameters Square(); Square(int = 0); // will not compile  Since a destructor takes no arguments, there can only be one destructor for a class
  • 49.  Non-constructor member functions can also be overloaded: void setCost(double); void setCost(char *);  Must have unique parameter lists as for constructors
  • 50.  A private member function can only be called by another member function  It is used for internal processing by the class, not for use outside of the class  See the createDescription function in ContactInfo.h (Version 2)
  • 51.  Objects can be the elements of an array: InventoryItem inventory[40];  Default constructor for object is used when array is defined
  • 52.  Must use initializer list to invoke constructor that takes arguments: InventoryItem inventory[3] = { "Hammer", "Wrench", "Pliers" };
  • 53.  If the constructor requires more than one argument, the initializer must take the form of a function call:
  • 54.  It isn't necessary to call the same constructor for each object in an array:
  • 55.  Objects in an array are referenced using subscripts  Member functions are referenced using dot notation: inventory[2].setUnits(30); cout << inventory[2].getUnits();
  • 56.
  • 58.  UML stands for Unified Modeling Language.  The UML provides a set of standard diagrams for graphically depicting object-oriented systems
  • 59.  A UML diagram for a class has three main sections.
  • 60. class Rectangle { private: double width; double length; public: bool setWidth(double); bool setLength(double); double getWidth() const; double getLength() const; double getArea() const; };
  • 61.  In UML you indicate a private member with a minus (-) and a public member with a plus(+). These member variables are private. These member functions are public.
  • 62.  To indicate the data type of a member variable, place a colon followed by the name of the data type after the name of the variable. - width : double - length : double
  • 63.  To indicate the data type of a function’s parameter variable, place a colon followed by the name of the data type after the name of the variable. + setWidth(w : double)
  • 64.  To indicate the data type of a function’s return value, place a colon followed by the name of the data type after the function’s parameter list. + setWidth(w : double) : void
  • 65.
  • 66. Constructors Destructor No return type listed for constructors or destructors