Presentation is loading. Please wait.

Presentation is loading. Please wait.

11 Introduction to Object Oriented Programming (Continued) Cats.

Similar presentations


Presentation on theme: "11 Introduction to Object Oriented Programming (Continued) Cats."— Presentation transcript:

1 11 Introduction to Object Oriented Programming (Continued) Cats

2 22 Objectives You will be able to: Write and use classes with multiple member variables. Use the "this" pointer in methods. Use "const" correctly in various places within a method declaration. Understand #pragma once

3 3 Class Cat Let's create a class to represent cats. Perhaps for use in a veterinarian's office. Each cat has: Name (Up to 20 letters) Weight Date of birth

4 4 New Project Create a new C++ console project in Visual Studio.

5 5 New Project

6 6

7 7 Add main

8 8 Start with a Stub Build and run

9 9 Program Running We have a working program!

10 10 Add Class Cat

11 11 Add Class Cat

12 12 Add Class Cat

13 13 Class Cat

14 14 #pragma once Same as the traditional guard: #ifndef CAT_H #define CAT_H... // Definition of class Cat... #endif Not ISO Standard C++, but widely supported by current compilers.

15 15 Add Member Variables Each cat has: Name (Up to 20 letters) Weight Date of birth For Name, use a C string (array of char.) For Weight, use a double. What about Date of Birth?

16 16 Date of Birth The C standard library has a way to represent date/time values and functions to manipulate them. But it's complicated Let's define a simple, easy to use, struct to represent dates in a user-friendly form: Day Month Year

17 17 Cat.h #pragma once struct Date { int Day; int Month; int Year; }; class Cat { private: char name[21]; Date date_of_birth; double weight; public: Cat(void); ~Cat(void); };

18 18 Constructor Add to Cat.h: Cat(const char* name_, Date dob, double weight_); Why const? Makes name_ a read-only pointer. Guarantees that the function will not modify the string that is passed as that argument. DOES NOT mean that the argument has to be a constant.

19 19 Cat.cpp #include #include "Cat.h" Cat::Cat(const char* name_, Date dob, double weight_) { strncpy(this->name, name_, 20); this->name[20] = 0; this->date_of_birth = dob; this->weight = weight_; } Cat::~Cat(void) { }

20 20 The "this" Pointer In any C++ method, the keyword this is a pointer to the object through which the method was called. Not necessary in this case Or most cases. Program would compile the same without it. Build the project

21 21 A Bogus Warning

22 22 Suppressing the Warning #define _CRT_SECURE_NO_WARNINGS #include #include "Cat.h" Cat::Cat(const char* name_, Date dob, double weight_) { The #define must the first line of the file in order to suppress Visual Studio's warning about strncpy. Build and run.

23 23 Program Running

24 24 Accessor Functions We can create Cat objects now, but we can't do anything with them. To make the attributes of a Cat visible outside the class, create public we need to provide accessor functions. Functions that just return the value of a member variable.

25 25 Accessor Functions public: Cat(const char* name_, Date dob, double weight_); const char* Name() const { return name;}; Date Date_of_Birth() const { return date_of_birth;}; double Weight() const {return weight;}; ~Cat(void); }; What are all those const's ? Note that we are putting the implementation of the methods in the class definition. Functions will be compiled in line.

26 26 main.cpp #include #include "Cat.h" using namespace std; int main() { cout << "This is program Cats\n"; Date dob = {12,1,2008}; Cat Fluffy("Fluffy", dob, 8.4); cout << "Cat: " << Fluffy.Name() << " "; cout << "DoB: " << Fluffy.Date_of_Birth().Month << "/" << Fluffy.Date_of_Birth().Day << "/" << Fluffy.Date_of_Birth().Year << " "; cout << "Weight: " << Fluffy.Weight(); cin.get(); // Hold the window open. return 0; }

27 27 Program Running

28 28 Display Method We normally want a method in each class that outputs the member variables to the screen. Add a new public method to Cat.h: void Display() const;

29 29 In Cat.cpp #define _CRT_SECURE_NO_WARNINGS #include #include "Cat.h" using namespace std;... void Cat::Display() const { cout name << " "; cout << "DoB: " date_of_birth.Month << "/" date_of_birth.Day << "/" date_of_birth.Year << " "; cout weight << endl; }

30 30 main.cpp #include #include "Cat.h" using namespace std; int main() { cout << "This is program Cats\n"; Date dob = {12,1,2008}; Cat Fluffy("Fluffy", dob, 8.4); … Fluffy.Display(); cout << endl; cin.get(); // Hold window open. return 0; } Build and run.

31 31 Program Running

32 32 Assignment Do today's example for yourself if you have not done it in class. Read Chapter 10. End of Presentation


Download ppt "11 Introduction to Object Oriented Programming (Continued) Cats."

Similar presentations


Ads by Google