O'Reilly logo

Beginning Object-Oriented Programming with C# by Jack Purdum

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 10

Designing and Writing Custom Classes

What you will learn in this chapter:

  • Constructor details
  • Method overloading
  • Cohesion
  • Coupling
  • Property methods
  • Getters and setters
  • Changing the state of an object
  • Sensing an error in a class method

wrox.com code downloads for this chapter

You can find the wrox.com code downloads for this chapter at www.wrox.com/remtitle.cgi?isbn=9781118336922 on the Download Code tab. The code in the Chapter10 folder is individually named according to the names throughout the chapter.

Chapter 9 was an introduction about designing your own classes. That chapter ended by designing and writing a simple class named clsDates that added two methods: getLeapYear () and getEaster(). This chapter continues the theme of class design but discusses additional details about writing classes.

When you finish this chapter, you will have a solid foundation upon which to start writing your own classes. With a little effort, you'll find that writing your own classes is both worthwhile and kind of fun.

Constructors

You should think of constructors as methods designed to create, or instantiate, an object. The sole purpose of a constructor is to enable you to instantiate an object with a known state. The beauty of C# (and most other OOP languages) is that you get a default constructor automatically.

Default Constructors

To make the creation of a default constructor possible, a constructor always has the same name as its class. For example, if you want to create an object ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required