Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 16: Classes and Objects.

Similar presentations


Presentation on theme: "Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 16: Classes and Objects."— Presentation transcript:

1 Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 16: Classes and Objects

2 In This Lesson We Will: Introduce the concept of classes Introduce the concept of classes Identify the two types of class members Identify the two types of class members Re-visit the concept of variable scope Re-visit the concept of variable scope Discuss the use of external classes Discuss the use of external classes

3 Topic Classes

4 What is a Class? A class defines a new data type A class defines a new data type Allows a programmer to associate information that is of different types Allows a programmer to associate information that is of different types Allows a programmer to define variables that contain the proper information Allows a programmer to define variables that contain the proper information A class is a mechanism used to model the real world A class is a mechanism used to model the real world Associates data with appropriate actions Associates data with appropriate actions Structures pertain to real-world objects Structures pertain to real-world objects

5 Class Members Properties: data “fields” which describe an object of that class Properties: data “fields” which describe an object of that class usually made private usually made private Methods: actions that may be performed by objects of that class Methods: actions that may be performed by objects of that class usually made public usually made public properties should be modified only through well-defined methods properties should be modified only through well-defined methods

6 Example (Part 1) Define a class: Pie (on paper) Define a class: Pie (on paper) Give it two properties: Give it two properties: flavor and price flavor and price Assign a get and set method for each of those properties Assign a get and set method for each of those properties

7 Example (Part 2) Define a class: Register (on paper) Define a class: Register (on paper) Give it one property: Give it one property: total total Create four methods: Create four methods: initialize initialize getTotal getTotal getReceipts getReceipts addMoney addMoney

8 Topic A Class As a User-Defined Type

9 User-Defined Types enum : a discrete set of possibilities; very similar to a group of constants enum : a discrete set of possibilities; very similar to a group of constants struct : an association between elements of different data types struct : an association between elements of different data types generally data elements are public generally data elements are public usually used JUST to store values usually used JUST to store values class : a description of real-world objects class : a description of real-world objects generally data elements are private generally data elements are private used to model objects that take action used to model objects that take action

10 Topic Member Modifiers

11 Visibility Modifiers One type of modifier influences the visibility of a class member One type of modifier influences the visibility of a class member public : A class member that is visible to external users of the class public : A class member that is visible to external users of the class private : A class member that is not visible outside of the class private : A class member that is not visible outside of the class protected : roughly equivalent to public within a limited scope, private outside of it protected : roughly equivalent to public within a limited scope, private outside of it

12 Visibility Modifiers (cont.) In practice: In practice: Use private for properties Use private for properties Use public for methods Use public for methods Methods: define actions which may be invoked externally Methods: define actions which may be invoked externally Properties: define the object, should be modified only through methods Properties: define the object, should be modified only through methods

13 Visibility Modifiers (cont.) The protected modifier allows a compromise between the others The protected modifier allows a compromise between the others It is used primarily within the context of inheritance, which topic will be left for a more advanced class It is used primarily within the context of inheritance, which topic will be left for a more advanced class

14 Other Modifiers const : Used to identify things that should not be allowed to change const : Used to identify things that should not be allowed to change We have seen data constants We have seen data constants May be used with methods and classes; that use is covered in another course May be used with methods and classes; that use is covered in another course static : Used to identify items that belong to an entire class of objects static : Used to identify items that belong to an entire class of objects

15 Example (Declaration) With that information, should be possible to create the class declarations for our Pie and Register classes With that information, should be possible to create the class declarations for our Pie and Register classes Use separate files for each one Use separate files for each one

16 Topic Mechanics

17 Defining Classes In C++ the relationship between files and classes is loose In C++ the relationship between files and classes is loose Permitted to define more than one class in a file and to split a class's definition and implementation across more than one file Permitted to define more than one class in a file and to split a class's definition and implementation across more than one file For our purposes we will try to keep one class to a file For our purposes we will try to keep one class to a file

18 Complications Compilation order is important Compilation order is important Must ensure that classes are compiled EXACTLY once Must ensure that classes are compiled EXACTLY once Must declare class before providing its implementation Must declare class before providing its implementation Use #define statements to help control these things Use #define statements to help control these things (Add those in now) (Add those in now)

19 Topic Scope

20 Property Scope We have encountered the importance of scope several times We have encountered the importance of scope several times The data members of a class have a scope that encompasses the entire class - meaning that they are visible to all of the methods defined within that class The data members of a class have a scope that encompasses the entire class - meaning that they are visible to all of the methods defined within that class

21 Concepts Re-visited This affects the parameter-passing discussion: there is no need to pass properties at all This affects the parameter-passing discussion: there is no need to pass properties at all Allows methods to make permanent changes to the properties of a class Allows methods to make permanent changes to the properties of a class In essence, allows a function to return more than one value, since a method may modify several properties of a class In essence, allows a function to return more than one value, since a method may modify several properties of a class

22 Implementation Scope Scope is also important as we go to implement the methods defined within the class Scope is also important as we go to implement the methods defined within the class Must explicitly associate each method with the class that owns it Must explicitly associate each method with the class that owns it Once that is done, complete the implementation in much the way we did ordinary functions Once that is done, complete the implementation in much the way we did ordinary functions

23 Example (Implementation) Complete the methods defined in the earlier classes Note that the implementation may be placed in the same file as the declarations

24 Topic Using Externally-Defined Classes

25 Example (Finish) In a third file (Store.cpp) write a program which uses the Pie and Register classes to model the operation of a small bakery In a third file (Store.cpp) write a program which uses the Pie and Register classes to model the operation of a small bakery Use an array of six pies to model the store's display shelf Use an array of six pies to model the store's display shelf Enter into a loop which will allow the store owner to sell pies, replace pies, and close the store Enter into a loop which will allow the store owner to sell pies, replace pies, and close the store

26 Worth Noting Note the use of double quotes when including user-defined classes; causes the #include to search current directory Note the use of double quotes when including user-defined classes; causes the #include to search current directory Note the use of the scope-resolution operator ( :: ) when providing method implementations Note the use of the scope-resolution operator ( :: ) when providing method implementations Note the use of the #define statements to ensure that file is included only once Note the use of the #define statements to ensure that file is included only once

27 Also Worth Noting Most “real-life” development efforts place the class declaration in a header (.h) file, and the implementation in a separate (.cpp) file Most “real-life” development efforts place the class declaration in a header (.h) file, and the implementation in a separate (.cpp) file Purpose is to reduce interdependence, since most bugs occur in implementation Purpose is to reduce interdependence, since most bugs occur in implementation

28 In This Lesson We Have: Introduced the concept of classes Introduced the concept of classes Discussed properties and methods Discussed properties and methods Described the scope of class properties Described the scope of class properties Explained the modifiers used for class members Explained the modifiers used for class members Described the use of externally-defined classes Described the use of externally-defined classes


Download ppt "Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 16: Classes and Objects."

Similar presentations


Ads by Google