Classes and Objects CHAPTER 6 Defining a class

Classes and Objects CHAPTER 6

Defining a class �Lets define a automobile class; �Why do we need to define the class? �What do we need to define in a class?

Designing a Class Diagram Class Name Data/characteristics/attributes Ask the question about the characteristics of the objects of this class? What can objects of this class do? How can we control objects of this class?

Class Car class Car { // characteristics // constructors // operations/functions/methods }

How to instantiate an object? �Car mycar = new Car(); �Car mycar = new Car(“Blue Car”);

How to invoke operations? �Car mycar = new Car(); �mycar. set. Color(“Blue”);

10 things 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. Class Object Constructors Functions Data Class design Processing Class structure Object instantiation Naming conventions Using the object and its functions

Ball Class ball // class name Ball // attributes float x, y ; // location color ball. Color; float radius; // constructors : create object and set //initial values ball() // default constructor ball(float x. Val, float y. Val, color b. Color, float radius. Val); //functions or behaviors or operations void display() void bounce() void move()

How to create a new ball? ball basket. Ball; basket. Ball = new ball(); ball bouncy. Ball; bouncy. Ball = new (50. 0, 60. 0, color(255, 0, 0), 40. 0); // that creates a red ball at location(x=50. 0, y=60. 0) and of size radius = 40. 0)

How to activate the operations? object. Name. operation. Name(parameters) Example: bouncy. Ball. display(); bouncy. Ball. move(); bouncy. Ball. bounce(); The dot notation: object. function()
- Slides: 10