Adding a New Class. In this Chapter: T Defining a New Class. T Adding a New Class Attribute to the app Class. T app Class: Attribute Restrictions

Similar documents
Object Oriented Design

TDDE18 & 726G77. Functions

Getting Started. 1 by Conner Irwin

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Abstraction in Software Development

C++ Style Guide. 1.0 General. 2.0 Visual Layout. 3.0 Indentation and Whitespace

CE221 Programming in C++ Part 1 Introduction

Chapter 9 Classes : A Deeper Look, Part 1

IT 374 C# and Applications/ IT695 C# Data Structures

INHERITANCE: CONSTRUCTORS,

Come and join us at WebLyceum

Chapter 17: Linked Lists

CS2720 Practical Software Development

How to lay out a web page with CSS

Function Overloading

Lab 1: First Steps in C++ - Eclipse

1.1 The hand written header file

TREX Set-Up Guide: Creating a TREX Executable File for Windows

Chapter 17: Linked Lists

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

PIC 10A. Lecture 17: Classes III, overloading

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

RECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function!

Orientation & Localization

Ch02. True/False Indicate whether the statement is true or false.

Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming

Object-Oriented Programming in C++

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

Chapter 4: Writing Classes

CSCI 262 C++ Style Guide

amaysim Guide to Mobile Internet Settings and MMS for Windows 6.5

1. An object of a class can be treated as an object of its corresponding class.

Developing Intelligent Apps


Object-Oriented Programming

Today we spend some time in OO Programming (Object Oriented). Hope you did already work with the first Starter and the box at:

CSCE 110 PROGRAMMING FUNDAMENTALS

Session A First Game Program

COMP6771 Advanced C++ Programming

Function Call Stack and Activation Records

Programming Lab 1 (JS Hwk 3) Due Thursday, April 28

Visual C# 2010 Express

Anatomy of a Class Encapsulation Anatomy of a Method

CPSC 427: Object-Oriented Programming

CST8152 Compilers Creating a C Language Console Project with Microsoft Visual Studio.Net 2003

Static Methods. Why use methods?

Lecture #1. Introduction to Classes and Objects

Lab 2: ADT Design & Implementation

ADTs & Classes. An introduction

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

CS302 - Data Structures using C++

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

# 1. Objectives. Objectives. 13.Visual Studio Projects. C/C++ The array is an Aggregate!

Assumptions. History

Due Date: See Blackboard

Program a Game Engine from Scratch. Development Checkpoint #8. Sprite & Resource Manager

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Chapter 11. Abstract Data Types and Encapsulation Concepts

ECE 480 Application Note. By: Jacob Hersha 4/3/15. Creating a Sequence of Media with Visual Studio

It is necessary to follow all of the sections below in the presented order. Skipping steps may prevent subsequent sections from working correctly.

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times

For Teacher's Use Only Q No Total Q No Q No

Due Date: See Blackboard

Opening Microsoft Visual Studio. On Microsoft Windows Vista and XP to open the visual studio do the following:

Come and join us at WebLyceum

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

G52CPP C++ Programming Lecture 9

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

This document explains how to obtain a direct link from within an existing Facebook page to the hotel s booking

2. [20] Suppose we start declaring a Rectangle class as follows:

UNIVERSITY OF SWAZILAND

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

The purpose of this tutorial is to introduce you to the Construct 2 program. First, you will be told where the software is located on the computer

6.096 Introduction to C++ January (IAP) 2009

Appendix M: Introduction to Microsoft Visual C Express Edition

1.00 Lecture 8. Using An Existing Class, cont.

Activating AspxCodeGen 4.0

CIS Intro to Programming in C#

Exchange Address Book Order

Intermediate Programming, Spring 2017*

Tutorial - Hello World

OBJECT ORIENTED PROGRAMMING USING C++

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Recommended Maintenance Plan for Siriusware Clients for SQL server 2005

EECE.3220: Data Structures Spring 2017

C++ Coding Standards and Practices. Tim Beaudet March 23rd 2015

Getting Started with Eric Meyer's CSS Sculptor 1.0

Text. In this Chapter: T Colouring Text. T Positioning Text. T Setting Text Visibility. T Sizing Text. T Determining Text Dimensions

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

Assistant Professor Computer Science. Introduction to Human-Computer Interaction

PIC 10A Objects/Classes

Introduction to ADTs

MRO Delay Line. Coding and Documentation Guidelines for Prototype Delay Line Software. John Young. rev June 2007

Introduction to ADTs

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

Financial computing with C++

C++ 8. Constructors and Destructors

Implementing an ADT with a Class

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Praktikum: Entwicklung interaktiver eingebetteter Systeme

Transcription:

Adding a New Class In this Chapter: T Defining a New Class T Adding a New Class Attribute to the app Class T app Class: Attribute Restrictions The Official AppGameKit Tier 2 Starter s Guide: Adding a New Class 77

The Die Class Introduction FIG-4.1 To see how we can tackle our games programming using the class-oriented aspects of C++, we ll start by creating a new project which, as well as defining the app class, will also define a Die (dice) class which we can use to display and roll a six-sided die (see FIG-4.1). A Visual Representation of a Die Class Object Activity 4.1 Make a copy of the folder CoreMethods and rename the copy UsingADie. Features Properties of the Die Class Since this is to be a visual component, we ll need to know the ID of the image and sprite used for the die as well as the last value that has been rolled. When the die is created, we ll want to load, position and size the image it uses within a sprite. Positioning and sizing and even using a new image should also be available after the die has been created. And, of course, we want to be able to roll the die. This last operation will create both a visual effect and set the value thrown to a random eger between 1 and 6. Class Diagrams In its simplest form a class diagram is a visual representation of the attributes and operations of a class. The following features can be added when describing the various elements of the class: + at the start of a property indicates that it is public. - at the start of a property indicates that it is private = value after a property shows that it is a constant attribute. The property name should be given in uppercase OR after a parameter, giving it s default value. underlined below a static property. 78 The Official AppGameKit Tier 2 Starter s Guide: Adding a New Class

«constructor»name for constructors. «destructor»name for destructors. «friend»name for functions which are friends of the class. When naming the constructors and the destructor in a class diagram, you can use the C++ convention of naming them after the class or you can use other terms such as Create and Destroy or New and Delete. FIG-4.2 The Class Diagram for Die The class diagram for Die is shown in FIG-4.2. - value : - sprid : - imgid : Die <<constructor>>die(fn:char*,sz:float,x:float,y:float) +Roll() : +GetValue() : +SetDieSize(sz:float) +SetDiePosition(x:float, y:float) +SetDieImage(img:) Implementing the Class The Die Header File FIG-4.3 The Header File for the Die Class To implement and test our new class, we ll start by creating a header file which contains the code shown in FIG-4.3. class Die { private: value; //Value thrown sprid; //ID of sprite used to show dice; imgid; //ID of image used for dice; public: Die(char*, float, float, float); Roll(); GetValue(); void SetDieSize(float); void SetDiePosition(float, float); void SetDieImage(); }; While Visual Studio will automatically include the line #pragma once (code shown in FIG-4.3 will go here) at the start of our new header file to ensure that the header file is copied no more than once o the final build before compilation, when using Android Studio creating the new header file will automatically insert the following lines to achieve the same safeguard: The Official AppGameKit Tier 2 Starter s Guide: Adding a New Class 79

FIG-4.4 Visual Studio #ifndef USINGADIE_DIE_H #define USINGADIE_DIE_H (code shown in FIG-4.3 will go here) #endif //USINGADIE_DIE_H The steps needed to add the new header file to our existing project using Visual Studio are shown in FIG-4.4. For Android Studio see FIG-4.5. With a new copy of CoreMethods renamed as UsingADie, we need to right click on the project name (Template), choose Add and New Item. Template New Item... Add From the dialog box, choose Header File (.h) and name the file Die.h then click Add. The new file will automatically open up in the edit window, where we can add the necessary code. Header File(.h) Die.h Click class Die { private: value; //Value thrown sprid; //ID of sprite used to show dice; imgid; //ID of image used for dice; public: Die(char*, float, float, float); Roll(); GetValue(); void SetDieSize(float); void SetDiePosition(float, float); void SetDieImage(); }; FIG-4.5 Android Studio After making a copy of CoreMethods and renaming it UsingADie, we need to display the contents of UsingADie\AGK2Template\src\main\jni. Right-clicking on jni displays a context menu. Selecting New and C/C++ Header File will create the empty file we require. Right click jni New C/C++ Header File 80 The Official AppGameKit Tier 2 Starter s Guide: Adding a New Class

FIG-4.5 (continued) Android Studio In the dialog box, name the file Die (don t add the.h extension - Android Studio will do this automatically) then click OK. The new file will be listed within the jni folder and opened in the edit window, with comments and the necessary guard code. Code created automatically // // Created by User 1 on 15/04/2016. // #ifndef USINGADIE_DIE_H #define USINGADIE_DIE_H #endif //USINGADIE_DIE_H Activity 4.2 Load project UsingADie and create a new file called Die.h. Enter the code given in FIG-4.3 in the new file. Next, we need to create the source code for the new class s methods. The constructor loads the image used by the die before creating, sizing and positioning the sprite used to display the die. It also rolls the die to assign it an initial value. The Roll() method not only assigns the die a new random value between 1 and 6 but also creates a visual effect of the die being shaken before finally displaying the rolled value. This method also returns the value that has been generated. The GetValue() method returns the value currently assigned to the die by the previous roll. New AGK2 functions used in these routines are shown in FIG-4.6. FIG-4.6 AGK2 Functions Used in Die.cpp Function Name Parameters Returned Description Example SetSpriteAnimation GetImageWidth sprid width height frames ImgID None Separates the sprites image o a set of frames. Frames are width by height pixels and the total number of frames is frames. Returns the width of an image in pixels. SetSpriteAnimation(1,20,40,3) Sprite 1 s image is split o 3 frames each 20 by 40 pixels w = GetImageWidth(3) Returns the width of image 3 GetImageHeight imgid Returns the height of an image in pixels. h = GetImageHeight(3) Returns the height of image 3 SetSpriteFrame sprid frame None Sets the sprite s displayed frame to frame number frame (Frame numbers start at 1). SetSpriteFrame(1,3) Sets sprite 1 to display frame 3 Random2 low high Returns a random value lying between low and high (inclusive). num = Random2(1,6) num is set to a random value between 1 and 6 The Official AppGameKit Tier 2 Starter s Guide: Adding a New Class 81