Function-Oriented Software Design (continued): Lecture 6 Dr. R. Mall.
11 Introduction to Object Oriented Programming (Continued) Cats.
-
Upload
muriel-townsend -
Category
Documents
-
view
230 -
download
0
Transcript of 11 Introduction to Object Oriented Programming (Continued) Cats.
![Page 1: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/1.jpg)
11
Introduction to
Object Oriented Programming(Continued)
Cats
![Page 2: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/2.jpg)
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
![Page 3: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/3.jpg)
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
![Page 4: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/4.jpg)
4
New Project
Create a new C++ console project in Visual Studio.
![Page 5: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/5.jpg)
5
New Project
![Page 6: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/6.jpg)
6
New Project
![Page 7: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/7.jpg)
7
Add main
![Page 8: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/8.jpg)
8
Start with a Stub
Build and run
![Page 9: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/9.jpg)
9
Program Running
We have a working program!
![Page 10: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/10.jpg)
10
Add Class Cat
![Page 11: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/11.jpg)
11
Add Class Cat
![Page 12: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/12.jpg)
12
Add Class Cat
![Page 13: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/13.jpg)
13
Class Cat
![Page 14: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/14.jpg)
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.
![Page 15: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/15.jpg)
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?
![Page 16: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/16.jpg)
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
![Page 17: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/17.jpg)
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);
};
![Page 18: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/18.jpg)
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.
![Page 19: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/19.jpg)
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)
{
}
![Page 20: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/20.jpg)
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
![Page 21: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/21.jpg)
21
A Bogus Warning
![Page 22: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/22.jpg)
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.
![Page 23: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/23.jpg)
23
Program Running
![Page 24: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/24.jpg)
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.
![Page 25: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/25.jpg)
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.
![Page 26: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/26.jpg)
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;
}
![Page 27: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/27.jpg)
27
Program Running
![Page 28: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/28.jpg)
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;
![Page 29: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/29.jpg)
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;
}
![Page 30: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/30.jpg)
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.
![Page 31: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/31.jpg)
31
Program Running
![Page 32: 11 Introduction to Object Oriented Programming (Continued) Cats.](https://reader034.fdocuments.net/reader034/viewer/2022052317/56649f2a5503460f94c441ae/html5/thumbnails/32.jpg)
3232
Assignment
Do today's example for yourself if you have not done it in class.
Read Chapter 10.
End of Presentation