Download presentation
Presentation is loading. Please wait.
Published byPolly Parker Modified over 9 years ago
1
Beginning C++ Through Game Programming, Second Edition by Michael Dawson
2
Chapter 8 Classes: Critter Caretaker
3
Objectives Create new types by defining classes Declare class data members and member functions Instantiate objects from classes Set member access levels Declare static data members and member functions
4
Classes Define new types Like a blueprint Code that groups data members and member functions From a class, create individual objects that have their own copies of each data member and access to all of the member functions Classes for game objects: space ship, sword, mutant chicken, etc.
5
Critter Class class Critter // class definition // defines a new type Critter { public: int m_Hunger; // data member void Greet(); // member func prototype };
6
Critter Class Explained For a virtual pet Begin with class followed by name Surround the class body with curly braces {} and end it with a semicolon ( ; ) Declare data members to represent object qualities Can begin name with m_ for clarity Declare member functions to represent object abilities
7
Defining Member Functions Can define member functions outside of a class definition // member function definition void Critter::Greet() { cout << "Hi. My hunger level is " << m_Hunger << ".\n"; } Qualify with the class name Can access object members Sending m_Hunger to cout means that Greet() displays the value of m_Hunger for the specific object through which the function is called
8
Instantiating Objects When you create an object, you instantiate it from a class Critter crit1; Critter crit2; Two Critter objects in existence, crit1 and crit2
9
Accessing Data Members Can access an available data member of an object with member selection operator crit1.m_Hunger = 9; Assigns 9 to crit1 ’s data member m_Hunger crit2.m_Hunger = 3; Assigns 3 to crit2 ’s data member m_Hunger Each object has its own m_Hunger data member with its own value
10
Calling Member Functions crit1.Greet(); Calls crit1 ’s Greet() member function Accesses crit1 ’s m_Hunger data member
11
Constructor Special member function automatically called every time a new object instantiated Useful for performing initialization of a new object
12
Critter Constructor class Critter { public: int m_Hunger; // constructor prototype Critter(int hunger = 0); void Greet(); }; // constructor definition Critter::Critter(int hunger) { cout << "A new critter has been born!" << endl; m_Hunger = hunger; }
13
Declaring and Defining a Constructor As with any member function, can declare the constructor in the class definition and can define constructor outside of class Constructor has no return type Must have same name as the class itself
14
Default Constructor A constructor that requires no arguments If you don’t define a default constructor, the compiler defines default If you write a constructor, compiler won’t provide a default constructor Usually a good idea to have a default constructor; supply your own when necessary.
15
Member Initializers Shorthand to assign values to data members in a constructor Critter::Critter(int hunger = 0, int boredom = 0): m_Hunger(hunger), m_Boredom(boredom) {} // empty constructor body
16
Calling a Constructor Automatically Don’t explicitly call a constructor Whenever you instantiate a new object, its constructor is automatically called Critter crit(7); Message A new critter has been born! is displayed Constructor assigns 7 to the object’s m_Hunger data member
17
Setting Member Access Levels Treat objects as encapsulated entities Avoid directly altering or accessing an object’s data members Call an object’s member functions instead Can enforce member restrictions
18
Private Critter //Private Critter //Demonstrates setting member access levels #include using namespace std; class Critter { public: // begin public section Critter(int hunger = 0); int GetHunger() const; void SetHunger(int hunger); private: // begin private section int m_Hunger; };
19
Private Critter (cont.) Critter::Critter(int hunger): m_Hunger(hunger) { cout << "A new critter has been born!\n"; } int Critter::GetHunger() const { return m_Hunger; } void Critter::SetHunger(int hunger) { if (hunger < 0) cout << "Can't set negative.\n"; else m_Hunger = hunger; }
20
Specifying Public and Private Access Levels Every data member and member function has an access level Access level determines from where in your program you can access member Any public member can be accessed in any part of the program private members can only be directly accessed by code in the class So in a main() function, the following would be illegal if uncommented: //cout << crit.m_Hunger;
21
Accessor Member Functions Allow indirect access to a data member GetHunger() is an accessor member function In a main() function, following line is legal: cout << crit.GetHunger(); SetHunger() limits access; can't set to negative number.
22
Constant Member Functions Can’t modify a data member of its class or call a non-constant member function of its class Protects from accidentally altering a data member in the member function Makes your intentions clear to other programmers Get accessor member functions can generally be defined as constant Constant member function can alter a static data member
23
Static Data Members and Member Functions Static data member –Data member that exists for the entire class –Can exist before any object of class does Static member function –Exists for the entire class –Doesn't need an object to call it through
24
Static Critter #include using namespace std; class Critter { public: //static member variable declaration static int s_Total; Critter(int hunger = 0); //static member function prototype static int GetTotal(); private: int m_Hunger; };
25
Static Critter (cont.) //static member variable initialization int Critter::s_Total = 0; Critter::Critter(int hunger): m_Hunger(hunger) { cout << "A critter has been born!"; ++s_Total; } //static member function definition int Critter::GetTotal() { return s_Total; }
26
Declaring and Initializing Static Data Members Declares a static data member s_Total : static int s_Total; Can prefix variable name with s_ for clarity Initialize static data member outside class definition Qualify static data member with its class name int Critter::s_Total = 0;
27
Accessing Static Data Members Can access a public static data member anywhere in program cout << Critter::s_Total << "\n\n"; Can also access a static member through any object of the class cout << crit1.s_Total << "\n\n"; Inside class definition, don't need to use class name ++s_Total; Can make a static data member private
28
Static Member Functions Can access a public static member function anywhere in program Begin defintion with static static int GetTotal() Call static member functions outside a class using class name cout << Critter::GetTotal(); Can access through any object of the class cout << crit1.GetTotal(); Static member functions don’t have direct access to any class data members or member functions
29
Summary In object-oriented programming (OOP), you define different types of objects with relationships that interact with each other You can create a new type by defining a class A class is a blueprint for an object You can access data members and member functions of objects through the member selection operator (. ) Every class has a constructor—a special member function that’s automatically called every time a new object is instantiated
30
Summary (cont.) A default constructor requires no arguments Member initializers provide shorthand to assign values to data members in a constructor You can set member access levels in a class by using the keywords public, private, and protected A public member can be accessed by any part of your code through an object A private member can only be accessed by a member function of that class
31
Summary (cont.) An accessor member function allows indirect access to a data member A static data member exists for the entire class A static member function exists for the entire class A constant member function can’t modify non-static data members or call non- constant member functions of its class
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.