Download presentation
Presentation is loading. Please wait.
Published byAugustus Lloyd Modified over 8 years ago
1
CS 101: Objects Abhiram Ranade
2
Inside Turtle Graphics ● You have probably guessed that there isnt a real turtle that is obeying your commands, but the computer is simulating a turtle. ● Simulate x : Create the illusion that x is actually present. ● So somewhere the computer must be keeping track of the turtle's state, e.g. position on the screen, in which direction it points, etc.
3
How a computer keeps track of a turtle ● Inside the memory, a small area is created in which the turtle's position, and orientation are stored. ● There could be more complex turtles, say a turtle might be hungry, might be tired, might have a velocity etc. These properties would also be stored in this area. ● This area is called the turtle object.
4
Multiple Turtles ● To create integers size, length you write: int size, length; ● To create 3 turtles you write: Turtle t1,t2,t3; This creates 3 areas in memory called t1,t2,t3, each of which can store data for a turtle. ● t1,t2,t3 are objects of class Turtle. ● class is similar to type.
5
Controlling each turtle ● On creation each turtle is positioned at the center of the screen, pointing north. ● To move t1 foward, write: t1.forward(10); ● To turn t2 left write: t2.left(90); ● forward, left: methods or member functions
6
The Turtle Class class Turtle{ float xpos=0,ypos=0, angle=90, f=3.141592/180; bool penon=true; public: // explained later void forward(float length){ float xd = length * cos(angle*f); float yd = length * sine(angle*f); if penon then Drawline(xpos,ypos,xpos+xd,ypos+yd); xpos = xpos + xd; ypos = ypos + yd; }... definitions of back, left, right,... }
7
Bool(ean) ● Primitive type in C++. Just as int can take as value any integer, bool type can take values true or false. ● Can be used where conditions are needed. ● Can be set: void penup(){ penon = false; }
8
Execution ● Turtle t1,t2,t3; : 3 areas get created in memory, one each for t1, t2, t3. There are 3 copies of xpos, ypos, angle. ● t1.forward(10) : Procedure forward is executed. length = 10, and xpos,ypos from t1's area are used. (Calling program suspends...) ● How forward knows which copy to use? t1 in t1.forward(10) is also an (implicit) argument to forward. “Really” forward has 2 arguments.
9
Classes and Objects ● Class: General description of how a certain kind of objects is to be represented and manipulated on a computer. e.g. Turtle. ● Object: A specific object that satisfies the description given for a class. e.g. t1,t2,t3. ● Description of the class must include how objects can be constructed, and manipulated.
10
How a Class is built ● Identify what object is to be modelled. What quantities define its state? – Turtle: position, orientation. – student: name, rollno, marks in midterm, final, lab – book: author, title, publisher, who has borrowed it.
11
How a class is built (contd.) ● Understand how the object will be used. What operations would a user want to perform on it? – Turtle: forward, back, right, pendown,... – Student: Print the Net Marks of student W = midterm*0.3 + final*0.5 + lab*0.2. – Book: Issue book to student W. ● Implementor writes “member functions” to implement operations.
12
How a class is used ● User can construct objects or instances of the class, e.g. using “Turtle t1,t2,t3;” ● User can invoke public member functions on instances, e.g. “t1.forward(90);” ● User cannot directly access non public variables in the objects, viz. xpos, ypos, angle. => User does not need to worry how the state is represented, or how forward works.
13
Class philosophy in real life ● You buy a TV from the market. ● Knobs are provided on it which allow you to change the channel, increase the volume,.. ● You dont have to worry how the channel changes, or also about getting electrical shock. ● TV Designer worries about how channel change happens. ● Class,Object,Method,User,Implementor = ?
14
Class Philosopy on computers ● Contract between Implementors and Users ● If you only use the public methods, the turtle will work as you expect. ● Implementor (your TAs) decide what is public and what is private. ● Everything could be public: TV in which all wires are exposed, Turtle in which user can mistakenly write t1.xpos = t1.xpos + 10;
15
Class Philosophy ● By deciding what is public and what is private, we separate the responsibilities of the implementor and the user. ● Implementor is free to redesign Turtle class if needed, without affecting the user program, if the public method call does not change. – May happen if the implementor discovers a better method to have the same effect.
16
What if you write code for yourself? ● Still use classes. Separate implementation and use. When you are drawing, you dont want to worry about manipulating coordinates. ● If you can think of a better implementation, if you have separated the private and public parts, then your code which uses the objects will not have to change.
17
Our turtles can see.. ● Turtle has member function turnTowards(Turtle t) This is called as: t1.turnToward(t2); t1 will point towards t2. If t1 moves forward, then it will pursue t2. ● What if all turtles pursue each other after starting in a square?
18
Old turtle vs new turtles ● Old turtle is still present, and will respond to functions forward, back, left, right. ● New turtles will need to be controlled using member functions. Member functions have same names, but are different. ● Better to use either old or new. You may hide the old turtle by calling HideTurtle().
19
Homework ● Write a program in which 4 turtles chase each other. This should cause 4 spirals to be drawn which merge into the center of the square. ● Write a program in which t1 chases t2, t2 chases t3, but t3 goes along some path, say a circle or a polygon. Observe the trajectories.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.