SlideShare a Scribd company logo
Object-Oriented Programming
         Using C++
         Third Edition


          Chapter 7
        Using Classes
Creating Classes
• A class is a category of objects; it is a new data
  type
     – Classes provide a description of an object
     – Classes provide a convenient way to group related
       data and the functions that use the data
     – When you create an object from the class, you
       automatically create all the related fields
     – You think about them and manipulate them as real-
       life classes and objects
• Abstract data type (ADT): a type that you define


Object-Oriented Programming Using C++, Third Edition       2
Creating Classes (continued)




Student aSophomore;
aSophomore.idNum = 7645;                       Error! By default, all members of a
cout<<aSophomore.idNum;                        class are private




Object-Oriented Programming Using C++, Third Edition                             3
Creating Classes (continued)


                                 Access modifier




Object-Oriented Programming Using C++, Third Edition   4
Encapsulating Class Components
• To encapsulate components is to contain them
     – Encapsulation is an example of a black box
• An interface intercedes between you and the inner
  workings of an object




Object-Oriented Programming Using C++, Third Edition   5
Designing Classes
• If you need a class for students, you should ask:
     –   What shall we call it?
     –   What are its attributes?
     –   What methods are needed by Student?
     –   Any other methods?
• In most cases, you declare both fields and functions
     – You declare a field using a data type and an identifier
     – You declare a function by writing its prototype, which
       serves as the interface to the function


Object-Oriented Programming Using C++, Third Edition         6
Designing Classes




• To instantiate an object is to declare or create it
     Student aSophomore;
     aSophomore.displayStudentData();
• A function that uses your class is a class client


Object-Oriented Programming Using C++, Third Edition    7
Implementing Class Functions
• When you construct a class, you create two parts:
     – Declaration section: contains the class name,
       variables (attributes), and function prototypes
     – Implementation section: contains the functions
• Use both the class name and the scope resolution
  operator (::) when you implement a class function




Object-Oriented Programming Using C++, Third Edition     8
Implementing Class Functions
                  (continued)




Object-Oriented Programming Using C++, Third Edition   9
Using Public Functions to Alter Private
                Data




Object-Oriented Programming Using C++, Third Edition   10
Using Public Functions to Alter Private
          Data (continued)




     …




Object-Oriented Programming Using C++, Third Edition   11
Using Private Functions and Public Data




Object-Oriented Programming Using C++, Third Edition   12
…
              …




Object-Oriented Programming Using C++, Third Edition   13
Considering Scope when Defining
            Member Functions




Object-Oriented Programming Using C++, Third Edition   14
Considering Scope when Defining
      Member Functions (continued)




Object-Oriented Programming Using C++, Third Edition   15
Using Static Class Members
• When a class field is static, only one memory
  location is allocated
     – All members of the class share a single storage
       location for a static data member of that same class
• When you create a non-static variable within a
  function, a new variable is created every time you
  call that function
• When you create a static variable, the variable
  maintains its memory address and previous value
  for the life of the program

Object-Oriented Programming Using C++, Third Edition      16
Defining Static Data Members

                                                  Since it is not const,
                                                  anyone can modify it




Object-Oriented Programming Using C++, Third Edition                       17
Defining Static Data Members
                   (continued)




• Static variables are sometimes called class
  variables, class fields, or class-wide fields
  because they don’t belong to a specific object; they
  belong to the class


Object-Oriented Programming Using C++, Third Edition   18
Using Static Functions
• A static function can be used without a
  declared object
• Non-static functions can access static variables
  (provided there is an object)
• Static functions cannot access non-static variables




Object-Oriented Programming Using C++, Third Edition   19
Using Static Functions (continued)




Object-Oriented Programming Using C++, Third Edition   20
Understanding the this Pointer




                …
                …
                …




Object-Oriented Programming Using C++, Third Edition   21
Understanding the this Pointer
                 (continued)




Object-Oriented Programming Using C++, Third Edition   22
Understanding the this Pointer
                 (continued)
• The this pointer holds the memory address of the
  current object that is using the function
• The this pointer is automatically supplied when
  you call a non-static member function of a class
     – For example, clerk.displayValues();
     – Is actually displayValues(&clerk);
• The actual argument list used by the compiler for
  displayValues() is displayValues(Employee *this)
• The this pointer is a constant pointer


Object-Oriented Programming Using C++, Third Edition   23
Using the this Pointer Explicitly




Object-Oriented Programming Using C++, Third Edition   24
Using the Pointer-to-Member Operator




Object-Oriented Programming Using C++, Third Edition   25
Understanding Polymorphism
• Polymorphism is the object-oriented program
  feature that allows the same operation to be carried
  out differently depending on the object
• For example,
     – clerk.displayValues();
     – shirt.displayValues();
     – XYZCompany.displayValues();




Object-Oriented Programming Using C++, Third Edition   26
Understanding Polymorphism (continued)




Object-Oriented Programming Using C++, Third Edition   27
You Do It: Creating and Using a Class
class CollegeCourse
{
    private:
      string department;
      int courseNum;
      int seats;
    public:
      void setDepartmentAndCourse(string, int);
      void setSeats(int);
      void displayCourseData();
};



Object-Oriented Programming Using C++, Third Edition   28
Using a static Field
class Letter
{
   private:
    string title;
    string recipient;
    static int count;
   public:
    void setRecipient(string, string);
    void displayGreeting();
    static void displayCount();
};


Object-Oriented Programming Using C++, Third Edition   29
Understanding How static and
       Non-static Fields are Stored




Object-Oriented Programming Using C++, Third Edition   30
Summary
• A class is a category of objects
• When you create a class, you hide, or encapsulate,
  the individual components
• When you construct a class, you create the
  declaration section and the implementation section
• When you create a class, usually you want to make
  data items private, and to make functions public
• The scope resolution operator (::) identifies a member
  function as being in scope within a class


 Object-Oriented Programming Using C++, Third Edition   31
Summary (continued)
• Each class object gets its own block of memory for
  its data members
• You can access a static, class-wide field using a
  static function
• One copy of each class member function is stored
  no matter how many objects exist
• Within any member function, you can explicitly use
  the this pointer to access the object’s data fields
• Polymorphism allows the same operation to be
  carried out differently depending on the object


Object-Oriented Programming Using C++, Third Edition   32
Ad

More Related Content

What's hot (20)

4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
Praveen M Jigajinni
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
Sohanur63
 
Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 aug
shashank12march
 
C++ classes
C++ classesC++ classes
C++ classes
Aayush Patel
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Madishetty Prathibha
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
Muthukumaran Subramanian
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
Marlom46
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
Fajar Baskoro
 
[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects
Muhammad Hammad Waseem
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
Rokonuzzaman Rony
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
Atif Khan
 
Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1 Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1
Sakthi Durai
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
C++ training
C++ training C++ training
C++ training
PL Sharma
 
Class or Object
Class or ObjectClass or Object
Class or Object
Rahul Bathri
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
Madishetty Prathibha
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
Vinod Kumar
 
Object and class
Object and classObject and class
Object and class
mohit tripathi
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
maznabili
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
Atul Sehdev
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
Sohanur63
 
Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 aug
shashank12march
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
Marlom46
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
Fajar Baskoro
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
Atif Khan
 
Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1 Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1
Sakthi Durai
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
C++ training
C++ training C++ training
C++ training
PL Sharma
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
Vinod Kumar
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
maznabili
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
Atul Sehdev
 

Viewers also liked (11)

C++ chapter 1
C++ chapter 1C++ chapter 1
C++ chapter 1
jasvinder162
 
OOP Basics
OOP BasicsOOP Basics
OOP Basics
CIB Egypt
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
M Hussnain Ali
 
Oop Presentation
Oop PresentationOop Presentation
Oop Presentation
Ghaffar Khan
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Object oriented programming (oop) cs304 power point slides lecture 01
Object oriented programming (oop)   cs304 power point slides lecture 01Object oriented programming (oop)   cs304 power point slides lecture 01
Object oriented programming (oop) cs304 power point slides lecture 01
Adil Kakakhel
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
emailharmeet
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
BG Java EE Course
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
Sachin Sharma
 
Oops ppt
Oops pptOops ppt
Oops ppt
abhayjuneja
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
M Hussnain Ali
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Object oriented programming (oop) cs304 power point slides lecture 01
Object oriented programming (oop)   cs304 power point slides lecture 01Object oriented programming (oop)   cs304 power point slides lecture 01
Object oriented programming (oop) cs304 power point slides lecture 01
Adil Kakakhel
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
Sachin Sharma
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
thinkphp
 
Ad

Similar to Classes cpp intro thomson bayan college (20)

CS1Lesson13-IntroClasses.pptx
CS1Lesson13-IntroClasses.pptxCS1Lesson13-IntroClasses.pptx
CS1Lesson13-IntroClasses.pptx
ChetanChauhan203001
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
Terry Yoast
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
Terry Yoast
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
OOPS_Lab_Manual - programs using C++ programming language
OOPS_Lab_Manual - programs using C++ programming languageOOPS_Lab_Manual - programs using C++ programming language
OOPS_Lab_Manual - programs using C++ programming language
PreethaV16
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
Dhaval Kaneria
 
Csc253 chapter 09
Csc253 chapter 09Csc253 chapter 09
Csc253 chapter 09
PCC
 
Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)
Asfand Hassan
 
Learning C++ - Class 4
Learning C++ - Class 4Learning C++ - Class 4
Learning C++ - Class 4
Ali Aminian
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
Connex
 
Object Oriented Programming notes provided
Object Oriented Programming notes providedObject Oriented Programming notes provided
Object Oriented Programming notes provided
dummydoona
 
polymorphism.ppt
polymorphism.pptpolymorphism.ppt
polymorphism.ppt
Shubham79233
 
Object oriented programming. (1).pptx
Object oriented programming.      (1).pptxObject oriented programming.      (1).pptx
Object oriented programming. (1).pptx
baadshahyash
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
arjun431527
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
9781285852744 ppt ch09
9781285852744 ppt ch099781285852744 ppt ch09
9781285852744 ppt ch09
Terry Yoast
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
Jay Patel
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
ANURAG SINGH
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
sidra tauseef
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
Terry Yoast
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
Terry Yoast
 
OOPS_Lab_Manual - programs using C++ programming language
OOPS_Lab_Manual - programs using C++ programming languageOOPS_Lab_Manual - programs using C++ programming language
OOPS_Lab_Manual - programs using C++ programming language
PreethaV16
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
Dhaval Kaneria
 
Csc253 chapter 09
Csc253 chapter 09Csc253 chapter 09
Csc253 chapter 09
PCC
 
Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)
Asfand Hassan
 
Learning C++ - Class 4
Learning C++ - Class 4Learning C++ - Class 4
Learning C++ - Class 4
Ali Aminian
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
Connex
 
Object Oriented Programming notes provided
Object Oriented Programming notes providedObject Oriented Programming notes provided
Object Oriented Programming notes provided
dummydoona
 
Object oriented programming. (1).pptx
Object oriented programming.      (1).pptxObject oriented programming.      (1).pptx
Object oriented programming. (1).pptx
baadshahyash
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
arjun431527
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
9781285852744 ppt ch09
9781285852744 ppt ch099781285852744 ppt ch09
9781285852744 ppt ch09
Terry Yoast
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
Jay Patel
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
ANURAG SINGH
 
Ad

Recently uploaded (20)

Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
Nguyen Thanh Tu Collection
 
Grade 3 - English - Printable Worksheet (PDF Format)
Grade 3 - English - Printable Worksheet  (PDF Format)Grade 3 - English - Printable Worksheet  (PDF Format)
Grade 3 - English - Printable Worksheet (PDF Format)
Sritoma Majumder
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
National Information Standards Organization (NISO)
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
Nguyen Thanh Tu Collection
 
Grade 3 - English - Printable Worksheet (PDF Format)
Grade 3 - English - Printable Worksheet  (PDF Format)Grade 3 - English - Printable Worksheet  (PDF Format)
Grade 3 - English - Printable Worksheet (PDF Format)
Sritoma Majumder
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 

Classes cpp intro thomson bayan college

  • 1. Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes
  • 2. Creating Classes • A class is a category of objects; it is a new data type – Classes provide a description of an object – Classes provide a convenient way to group related data and the functions that use the data – When you create an object from the class, you automatically create all the related fields – You think about them and manipulate them as real- life classes and objects • Abstract data type (ADT): a type that you define Object-Oriented Programming Using C++, Third Edition 2
  • 3. Creating Classes (continued) Student aSophomore; aSophomore.idNum = 7645; Error! By default, all members of a cout<<aSophomore.idNum; class are private Object-Oriented Programming Using C++, Third Edition 3
  • 4. Creating Classes (continued) Access modifier Object-Oriented Programming Using C++, Third Edition 4
  • 5. Encapsulating Class Components • To encapsulate components is to contain them – Encapsulation is an example of a black box • An interface intercedes between you and the inner workings of an object Object-Oriented Programming Using C++, Third Edition 5
  • 6. Designing Classes • If you need a class for students, you should ask: – What shall we call it? – What are its attributes? – What methods are needed by Student? – Any other methods? • In most cases, you declare both fields and functions – You declare a field using a data type and an identifier – You declare a function by writing its prototype, which serves as the interface to the function Object-Oriented Programming Using C++, Third Edition 6
  • 7. Designing Classes • To instantiate an object is to declare or create it Student aSophomore; aSophomore.displayStudentData(); • A function that uses your class is a class client Object-Oriented Programming Using C++, Third Edition 7
  • 8. Implementing Class Functions • When you construct a class, you create two parts: – Declaration section: contains the class name, variables (attributes), and function prototypes – Implementation section: contains the functions • Use both the class name and the scope resolution operator (::) when you implement a class function Object-Oriented Programming Using C++, Third Edition 8
  • 9. Implementing Class Functions (continued) Object-Oriented Programming Using C++, Third Edition 9
  • 10. Using Public Functions to Alter Private Data Object-Oriented Programming Using C++, Third Edition 10
  • 11. Using Public Functions to Alter Private Data (continued) … Object-Oriented Programming Using C++, Third Edition 11
  • 12. Using Private Functions and Public Data Object-Oriented Programming Using C++, Third Edition 12
  • 13. … Object-Oriented Programming Using C++, Third Edition 13
  • 14. Considering Scope when Defining Member Functions Object-Oriented Programming Using C++, Third Edition 14
  • 15. Considering Scope when Defining Member Functions (continued) Object-Oriented Programming Using C++, Third Edition 15
  • 16. Using Static Class Members • When a class field is static, only one memory location is allocated – All members of the class share a single storage location for a static data member of that same class • When you create a non-static variable within a function, a new variable is created every time you call that function • When you create a static variable, the variable maintains its memory address and previous value for the life of the program Object-Oriented Programming Using C++, Third Edition 16
  • 17. Defining Static Data Members Since it is not const, anyone can modify it Object-Oriented Programming Using C++, Third Edition 17
  • 18. Defining Static Data Members (continued) • Static variables are sometimes called class variables, class fields, or class-wide fields because they don’t belong to a specific object; they belong to the class Object-Oriented Programming Using C++, Third Edition 18
  • 19. Using Static Functions • A static function can be used without a declared object • Non-static functions can access static variables (provided there is an object) • Static functions cannot access non-static variables Object-Oriented Programming Using C++, Third Edition 19
  • 20. Using Static Functions (continued) Object-Oriented Programming Using C++, Third Edition 20
  • 21. Understanding the this Pointer … … … Object-Oriented Programming Using C++, Third Edition 21
  • 22. Understanding the this Pointer (continued) Object-Oriented Programming Using C++, Third Edition 22
  • 23. Understanding the this Pointer (continued) • The this pointer holds the memory address of the current object that is using the function • The this pointer is automatically supplied when you call a non-static member function of a class – For example, clerk.displayValues(); – Is actually displayValues(&clerk); • The actual argument list used by the compiler for displayValues() is displayValues(Employee *this) • The this pointer is a constant pointer Object-Oriented Programming Using C++, Third Edition 23
  • 24. Using the this Pointer Explicitly Object-Oriented Programming Using C++, Third Edition 24
  • 25. Using the Pointer-to-Member Operator Object-Oriented Programming Using C++, Third Edition 25
  • 26. Understanding Polymorphism • Polymorphism is the object-oriented program feature that allows the same operation to be carried out differently depending on the object • For example, – clerk.displayValues(); – shirt.displayValues(); – XYZCompany.displayValues(); Object-Oriented Programming Using C++, Third Edition 26
  • 27. Understanding Polymorphism (continued) Object-Oriented Programming Using C++, Third Edition 27
  • 28. You Do It: Creating and Using a Class class CollegeCourse { private: string department; int courseNum; int seats; public: void setDepartmentAndCourse(string, int); void setSeats(int); void displayCourseData(); }; Object-Oriented Programming Using C++, Third Edition 28
  • 29. Using a static Field class Letter { private: string title; string recipient; static int count; public: void setRecipient(string, string); void displayGreeting(); static void displayCount(); }; Object-Oriented Programming Using C++, Third Edition 29
  • 30. Understanding How static and Non-static Fields are Stored Object-Oriented Programming Using C++, Third Edition 30
  • 31. Summary • A class is a category of objects • When you create a class, you hide, or encapsulate, the individual components • When you construct a class, you create the declaration section and the implementation section • When you create a class, usually you want to make data items private, and to make functions public • The scope resolution operator (::) identifies a member function as being in scope within a class Object-Oriented Programming Using C++, Third Edition 31
  • 32. Summary (continued) • Each class object gets its own block of memory for its data members • You can access a static, class-wide field using a static function • One copy of each class member function is stored no matter how many objects exist • Within any member function, you can explicitly use the this pointer to access the object’s data fields • Polymorphism allows the same operation to be carried out differently depending on the object Object-Oriented Programming Using C++, Third Edition 32