上海交通大学交大密西根 联合学院·一 ◆ 181 UM-SJTU Joint Institute ■ University of Michigan Shanghal Jiao Tong University Vg101 Introduction to Computer and Programming Example on Objects and Classes
Vg101 Introduction to Computer and Introduction to Computer and Programming Programming Example on Objects and Classes
上海交通大学交大密西根 联合学院·一 ◆ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Bouncing Ball Problem:Implement bounding ball using more object oriented version First of all,let's think how many objects or classes will be associated with this applications. -A window with rectangular boundary and -A Ball
Bouncing Ball Bouncing Ball • Problem: Implement bounding ball using more object oriented version • First of all, let’s think how many objects or classes will be associated with this applications. – A window with rectangular boundary and – A Ball
上海交通大学交大密西根 B 联合学院·一 ◆ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Characteristics of a Window Attributes - size(width,height):double colar:string title:string ● Since it is primarily for graphics,it also has - pen:PenT mouse:MouseT
Characteristics of a Window Characteristics of a Window • Attributes – size(width, height): double – colar: string – title: string • Since it is primarily for graphics, it also has – pen: PenT – mouse: MouseT
上海交通大学交大密西根 联合学院·一 ■ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Data Members of GwindowsT class GwindowsT:public ConsoleT//inherit from ConsoleT { /data members of the class protected: /∥another attributes double width,height; /size of a window object string title; /title of a window object string color; /color of a window object public: static PenT pen; /pen of the window class static MouseT mouse; /mouse of the window class
Data Members of Data Members of GwindowsT GwindowsT class GwindowsT: public ConsoleT// inherit from ConsoleT { // data members of the class protected: // another attributes double width, height; // size of a window object string title; // title of a window object string color; // color of a window object .... public: static PenT pen; // pen of the window class static MouseT mouse; // mouse of the window class
上海交通大学交大密西根 联合学院一 ◆ 181 UM-SJTU Joint Institute ■ University of Michigan Shanghal Jiao Tong University PenT and MouseT Characteristics of MouseT -Location (x,y):double Characteristics of a PenT; -location:PointT color:string Characteristics of a PointT - location (x,y):double
PenT and MouseT • Characteristics of MouseT – Location (x, y): double • Characteristics of a PenT: – location: PointT – color: string • Characteristics of a PointT – location (x, y): double
上海交通大学交大密西根 8 联合学院·一 ◆ 1811 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Data Manipulation Methods Constructors Used for initialize defined objects ● Accessors - Used to access private data members ● Mutators - Used to change/set private data members Other Method Functions Used for other functions of the objects
Data Manipulation Methods Data Manipulation Methods • Constructors – Used for initialize defined objects • Accessors – Used to access private data members • Mutators – Used to change/set private data members • Other Method Functions – Used for other functions of the objects
上海交通大学交大密西根 联合学院·一 ◆ 81T UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Constructors When we define regular variables,we can define it without initialization or define it and get it initialized at the same time. We wish the classes we defined has the same property.It is therefore,we normally define two kinds of constructors Constructor with no specified initial values Constructor with specified initial values
Constructors Constructors • When we define regular variables, we can define it without initialization or define it and get it initialized at the same time. • We wish the classes we defined has the same property. It is therefore, we normally define two kinds of constructors – Constructor with no specified initial values – Constructor with specified initial values
上海交通大学交大密西根 联合学院·一 ◆ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Constructors of PointT class PointT double x,y; public; PointT O); /PointT pt; PointT (double x,double y);//PointT pt (3,5.4); PointT (const PointT &p);//PointT point(pt);
Constructors of Constructors of PointT class PointT { double x, y; public: PointT (); // PointT pt; PointT (double x, double y);// PointT pt (3, 5.4); PointT (const PointT &p); // PointT point (pt); ……
上海交通大学交大密西根 联合学院·一 ◆] 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Constructors of PenT class PenT { public: PenT O; /PenT pen; PenT (double x,double y,const string clr); /∥PenT(3,5.4,“Red")
Constructors of Constructors of PenT class PenT { public: PenT (); // PenT pen ; PenT (double x, double y, const string clr); // PenT (3, 5.4, “Red”) ……
上海交通大学交大密西根 联合学院·一 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Constructors of GwindowsT class GwindowsT:public ConsoleT protected: double width,height; /size of a window object string title; /title of a window object string color; /color of a window object public: static PenT pen; /pen of the window class static MouseT mouse; /mouse of the window class GwindowsTO; I∥GwindowsT window; GwindowsT const double width,const double height,string color "White",string title ="") ∥GwindowsT window(12.0,6.0,“Red",“Bouncing Ball"); ∥GwindowsT window(12.0,6.0,“Red"); /∥GwindowsT window(12.0,6.0);
Constructors of Constructors of GwindowsT GwindowsT class GwindowsT: public ConsoleT { protected: double width, height; // size of a window object string title; // title of a window object string color; // color of a window object public: static PenT pen; // pen of the window class static MouseT mouse; // mouse of the window class GwindowsT (); // GwindowsT window; GwindowsT ( const double width, const double height, string color = "White", string title = ""); // GwindowsT window (12.0, 6.0, “Red”, “Bouncing Ball”); // GwindowsT window (12.0, 6.0, “Red”); // GwindowsT window (12.0, 6.0);