Download Defining Classes in Object-Oriented Programming: A C# Example with Time1 Class and more Slides C programming in PDF only on Docsity! 1 CS 112 Introduction to Programming Lecture #16: Defining Classes http://flint.cs.yale.edu/cs112/ 2 Outline rAdmin. and review rClasses and objects rDefining classes m Basic syntax m Examples 3 PrintMonth GetMonthDays GetMonthDays(); PrintMonth(); Main Summary: Method Control Flow 4 Summary rDesign • Top-down; stepwise refinement r Implementation and testing • Write the simple functions first • Write a piece and test a piece – Test a method » using some dummy method calls, e.g. Call the method in Main() with some typical test cases » using Visual Studio Immediate window 5 Outline rAdmin. and review Ø Classes and objects rDefining a class m Basic syntax m Examples 6 Object-Oriented Design r The focus of methods is on doing things; roughly speaking, we can say that methods focus on the verbs. r In object-oriented design, we focus on the nouns m In object-oriented design, we tend to group methods together according to the nouns m Important for large, complex programs 2 7 C# Classes rA C# class plays dual roles: m Program module: containing a list of (static) method declarations and (static) data fields m Blueprint for generating objects • It is the model or pattern from which objects are created • Supports two techniques which are essence of object- oriented programming – “data encapsulation” (for abstraction) – “inheritance” (for code reuse) 8 User-Defined Class r A user-defined class is also called a user-defined type m class written by a programmer r A class encapsulates (wrap together) data and methods: • data members (member variables or instance variables) • methods that manipulate data members 9 Objects r An object has: m state - descriptive characteristics m behaviors - what it can do (or be done to it) r For example, consider a coin in a computer game m The state of the coin is its current face (head or tail) m The behavior of the coin is that it can be flipped r Note the interactions between state and behaviors m the behavior of an object might change its state m the behavior of an object might depend on its state 10 Outline rAdmin. and review rClasses and objects Ø Defining a class Ø Basic syntax m Examples 11 public int x, y; private char ch; class MyClass Defining Classes r Use Project < Add Class to add a new class to your project r A class contains data declarations and method declarations Data declarations Method declarations Member (data/method) Access Modifiers public : member is accessible outside the class private : member is accessible only inside the class definition 12 Data Declarations r You can define two types of variables in a class but not in any method (called class variables) m static class variables m nonstatic variables are called instance variables (fields) because each instance (object) of the class has its own copy m class variables can be accessed in all methods of the class r Comparison: Local variables • Variables declared within a method or within a block statement • Variables declared as local variables can only be accessed in the method or the block where they are declared