11 Introduction to Object Oriented Programming (Continued) Cats.

32
1 Introduction to Object Oriented Programming (Continued) Cats

Transcript of 11 Introduction to Object Oriented Programming (Continued) Cats.

Page 1: 11 Introduction to Object Oriented Programming (Continued) Cats.

11

Introduction to

Object Oriented Programming(Continued)

Cats

Page 2: 11 Introduction to Object Oriented Programming (Continued) Cats.

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.

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.

4

New Project

Create a new C++ console project in Visual Studio.

Page 5: 11 Introduction to Object Oriented Programming (Continued) Cats.

5

New Project

Page 6: 11 Introduction to Object Oriented Programming (Continued) Cats.

6

New Project

Page 7: 11 Introduction to Object Oriented Programming (Continued) Cats.

7

Add main

Page 8: 11 Introduction to Object Oriented Programming (Continued) Cats.

8

Start with a Stub

Build and run

Page 9: 11 Introduction to Object Oriented Programming (Continued) Cats.

9

Program Running

We have a working program!

Page 10: 11 Introduction to Object Oriented Programming (Continued) Cats.

10

Add Class Cat

Page 11: 11 Introduction to Object Oriented Programming (Continued) Cats.

11

Add Class Cat

Page 12: 11 Introduction to Object Oriented Programming (Continued) Cats.

12

Add Class Cat

Page 13: 11 Introduction to Object Oriented Programming (Continued) Cats.

13

Class Cat

Page 14: 11 Introduction to Object Oriented Programming (Continued) Cats.

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.

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.

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.

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.

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.

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.

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.

21

A Bogus Warning

Page 22: 11 Introduction to Object Oriented Programming (Continued) Cats.

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.

23

Program Running

Page 24: 11 Introduction to Object Oriented Programming (Continued) Cats.

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.

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.

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.

27

Program Running

Page 28: 11 Introduction to Object Oriented Programming (Continued) Cats.

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.

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.

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.

31

Program Running

Page 32: 11 Introduction to Object Oriented Programming (Continued) Cats.

3232

Assignment

Do today's example for yourself if you have not done it in class.

Read Chapter 10.

End of Presentation