SlideShare ist ein Scribd-Unternehmen logo
1 von 32
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

Weitere ähnliche Inhalte

Was ist angesagt?

Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 aug
shashank12march
 

Was ist angesagt? (20)

4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 aug
 
C++ classes
C++ classesC++ classes
C++ classes
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1 Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
C++ training
C++ training C++ training
C++ training
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Object and class
Object and classObject and class
Object and class
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 

Andere mochten auch (11)

C++ chapter 1
C++ chapter 1C++ chapter 1
C++ chapter 1
 
OOP Basics
OOP BasicsOOP Basics
OOP Basics
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 
Oop Presentation
Oop PresentationOop Presentation
Oop Presentation
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
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
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 

Ähnlich wie Classes cpp intro thomson bayan college

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
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
Connex
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
arjun431527
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
Jay Patel
 

Ähnlich wie Classes cpp intro thomson bayan college (20)

CS1Lesson13-IntroClasses.pptx
CS1Lesson13-IntroClasses.pptxCS1Lesson13-IntroClasses.pptx
CS1Lesson13-IntroClasses.pptx
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
Csc253 chapter 09
Csc253 chapter 09Csc253 chapter 09
Csc253 chapter 09
 
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)
 
Learning C++ - Class 4
Learning C++ - Class 4Learning C++ - Class 4
Learning C++ - Class 4
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
polymorphism.ppt
polymorphism.pptpolymorphism.ppt
polymorphism.ppt
 
Lecture02-OOP-Review.ppt
Lecture02-OOP-Review.pptLecture02-OOP-Review.ppt
Lecture02-OOP-Review.ppt
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
 
9781285852744 ppt ch09
9781285852744 ppt ch099781285852744 ppt ch09
9781285852744 ppt ch09
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 
Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2
 
CIS110 Computer Programming Design Chapter (11)
CIS110 Computer Programming Design Chapter  (11)CIS110 Computer Programming Design Chapter  (11)
CIS110 Computer Programming Design Chapter (11)
 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructors
 
Object & classes
Object & classes Object & classes
Object & classes
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 

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