1 / 32

Introduction to Object Oriented Programming (Continued)

Introduction to Object Oriented Programming (Continued). Cats. 1. 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. 2.

jon
Download Presentation

Introduction to Object Oriented Programming (Continued)

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. Introduction to Object Oriented Programming(Continued) Cats 1

  2. 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 2

  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. New Project • Create a new C++ console project in Visual Studio.

  5. New Project

  6. New Project

  7. Add main

  8. Start with a Stub Build and run

  9. Program Running We have a working program!

  10. Add Class Cat

  11. Add Class Cat

  12. Add Class Cat

  13. Class Cat

  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. 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. 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. 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. 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. Cat.cpp #include <cstring> #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. 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. A Bogus Warning

  22. Suppressing the Warning #define _CRT_SECURE_NO_WARNINGS #include <cstring> #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. Program Running

  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. 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. main.cpp #include <iostream> #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. Program Running

  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. In Cat.cpp #define _CRT_SECURE_NO_WARNINGS #include <cstring> #include <iostream> #include "Cat.h" using namespace std; ... void Cat::Display() const { cout << "Cat: " << this->name << " "; cout << "DoB: " << this->date_of_birth.Month << "/" << this->date_of_birth.Day << "/" << this->date_of_birth.Year << " "; cout << "Weight: " << this->weight << endl; }

  30. main.cpp #include <iostream> #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. Program Running

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

More Related