1 / 55

C++ Classes in Depth

C++ Classes in Depth. Topics. Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects. Objectives. At the completion of this topic, students should be able to:. Design classes for use in a C++ program

keenan
Download Presentation

C++ Classes in Depth

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Classes in Depth

  2. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects

  3. Objectives At the completion of this topic, students should be able to: Design classes for use in a C++ program Explain the difference between a class and an object Explain what attributes and behaviors are Explain the terms encapsulation and data hiding Create a program that uses a programmer designed class

  4. Let’s review some concepts from CS 1400

  5. Objects are used to model real-world things. You can think of an object as a rather complex variable that contains multiple chunks of data

  6. Let’s consider an object that represents a time.

  7. Real world objects have attributes An object’s attributes describe its “state of being” What things do we normally associate with the state of a time? minutes seconds am/pm hours

  8. We hide the data from anyone outside of the object by making it “private” Joe WageEarner 12356 $12.50 40 A Time object

  9. The attributes of an object are stored inside the object as data members. The hour value is stored as an integer 3 The minute value is stored as an integer 35 pm am or pm is stored as a string An “Time” object

  10. An object also has behaviors behaviors define how you interact with the object What can you do with a time? set the hours Get the hours

  11. Behaviors are expressed as methods that give us controlled access to the data. These are “public” so that we can see them. Joe WageEarner 12356 getHour ( ) setHour( ) $12.50 40 An “Time” object

  12. Then we write statements that send messages to the objects Joe WageEarner joe.getHours( ); 12356 getHour ( ) setHour ( ) $12.50 40 An “Time” object named joe

  13. An Object’s Attributes and Behaviors Should Work Together this is called cohesion

  14. Encapsulation Time object getHour( ) member methods are declared as public calling method we should not allow code outside of the object to reach in and change the data directly. Instead, we call methods in the object to do it for us. hour minute member data is declared as private The terms public and private are called access modifiers

  15. A Class is a blueprint that a program uses when it creates an object. A class reserves no space in memory When an object is created from the class blueprint, memory is reserved to hold the object’s attributes. An object is known as an instance of the class. Each object has it’s own space and data.

  16. A class is said to be an abstraction of the real world object that we are modeling. Classes are sometimes called Abstract Data Types.

  17. We use a UML Class Diagram to document the data and methods contained in our class.

  18. A UML class diagramis used to describe a class in a very precise way. A class diagram is a rectangle. At the top of the rectangle is the class name. A line separates the class name from the rest of the diagram. Time

  19. Time Following the class name we write the data members of the class. A line separates the data members from the rest of the diagram. - hour: int access modifier: + public - private data member name data type

  20. Time Following the data members, we write the member methods. • hour: int • minute: int + getHour( ): int access modifier + public - private method name return type parameters

  21. It is important that class diagrams be drawn precisely and that they conform to the form shown in these examples.

  22. In C++ the class definition usually goes into a separate file called the header file. It has a file extension of .h time.h class Time { private: int hour; int minutes; string amOrPm public: Time( ); intgetHour( ) const; intgetMinutes( ) const; string getAmOrPm( ); void setHour( int n); void setMinutes( int n); void setAmOrPm(string a); };

  23. In C++ the class definition usually goes into a separate file called the header file. It has a file extension of .h time.h class Time { private: int hour; int minutes; string amOrPm public: Time( ); intgetHour( ) const; intgetMinutes( ) const; string getAmOrPm( ); void setHour( int n); void setMinutes( int n); void setAmOrPm(string a); }; Notice that getter functions (functions that do not change the state of the object) are declared as const functions.

  24. Declaring Member Data Member data is always “private” class Time { private: int hour; int minutes; string amOrPm; } Indent each line inside the block variable name data type

  25. Declaring Member Data class Time { private: int hour; int minutes; string amOrPm; } We call data members of a class “Instance Data” because each instance (object) of the class will contain its own unique copy of this data.

  26. Declaring Member Methods public class Time { private: . . . public: Time( ); intgetHour( ) const; intgetMinutes( ) const; string getAmOrPm( ) const; void setHour( int n); void setMinutes( int n); void setAmOrPm(string a); Member methods are usually public When would you make a method private? These are called function prototypes return type parameters function name

  27. In C++ the code that implements the member functions of the class goes into a related file with the file extension .cpp time.cpp always #include the .h file for this class #include “time.h” . . . string Time::getHour( ) const { return hour; } void Time::setHour( double n) { hour = n } . . . Class name followed by ::

  28. “Setter”Methods they are usually named “set” plus the name of the instance variable they will store the value in. void Time::setHour(int hr) { hour = hr; } setters never return anything setters always take a parameter

  29. “Getter”Methods getters take no parameters getters always return something int Time::getHour( ) const { return hour; } The const keyword is required to tell the compiler that the function does not alter the object. they are usually named “get” plus the name of the instance variable they will return the value of.

  30. The methods of a class are the public services or the public interface that this class offers to its clients. Clients are neither aware of nor involved in the implementation details of these methods. Clients care what a method does, but not how it does it.

  31. Static Data Every instance object has its own copy of all of the instance variables in a class. What if you want to have all objects of a class share a single instance of a variable? For example, all savingsAccount objects might have the same interest rate. To do this, declare the variable as private static double interestRate = 0.0725;

  32. Static Methods A static method can be called without creating an object of the class that it belongs to. Static methods don’t operate on data in an object unless the data itself is declared as static. When you invoke a static method you use the name of the class instead of an object name.

  33. The this reference Every instance object contains a variable named this that is a pointer to itself. The keyword is often used when you want to explicitly name a variable as a data member of this object. For example, in the Time class we have discussed is a method void setHour( inthr) { hour = hr; }

  34. The this reference Using the this reference we could write: void setHour( inthr) { this->hour = hr; } More to come on pointers and “this” in a few weeks.

  35. Creating Objects

  36. Class definition Time startTime; class Time { private: int hour; int minute; . . . } this statement takes the Time class definition and uses it to create the object “startTime”. When creating the object, storage is allocated for the each of the data members defined in the class. hour minute startTime

  37. Constructors When an object is created, the data members inside the object are not initialized. They take on the values in the bits in the memory that the object occupies. This is pretty dangerous, so we initialize the data inside an object by using a constructor. The constructor gets called when the object is created.

  38. Constructors A constructor has the same name as the class. No return type is mentioned Time::Time( ) { hour = 0; minutes = 0; } If you do not write a constructor, the compiler creates one for you. It takes no parameters and its body is empty, i.e. it does nothing.

  39. You can also write a parameterized constructor. Constructors Time::Time(inthr, int min ) { hour = hr; minutes = min; } If you write a parameterized constructor, you must also write a non-parameterized one. The non-parameterized constructor is called the default constructor.

  40. Sending Messages to Objects

  41. parameter startTime.setHour(8); message . method name object name hour minute startTime

  42. parameter startTime.setHour(8); This statement send the setHour message to the object named startTime. As the method executes, the value of the parameter hr is stored in the instance variable hour. message hour minute startTime void setHour(int hr) { hour = hr; }

  43. The code that creates objects and sends messages to them is typically written in main( ) in what we call a driver. This code is saved in a separate .cpp file. driver.cpp always #include the .h file for any class used in main #include <iostream> #include “time.h” using namespace std; int main( ) { Time newTime; . . . myTime.setHour(3); . . . }

  44. Designing a Class

  45. Classes model real world things, like … * an employee * a student * a car * a rectangle * a circle * a bowling team . . .

  46. * A Rectangle What words would you use to describe a rectangle, i.e. what are its attributes? These become data members of the class. it’s width it’s height it’s color

  47. Draw a Class Diagram Rectangle • width: int • height: int

  48. * A Rectangle What can I do with/to a Rectangle? These become member functions of the class. create one change it’s height or width calculate it’s area

  49. Draw a Class Diagram Rectangle • width: int • height: int • color: string + Rectangle(:int, :int, :string) + setHeight(:int) + setWidth(:int) + getArea( ): int

  50. Create the .h File class Rectangle { private: int height; int width; string color; public: Rectangle ( ); Rectangle (int, int, string); void setWidth( int ); void setHeight( int ); intgetArea( ); }; For brevity I have not included the function prologues

More Related