Home Previous Next

CSC100 :: Lecture Note :: Week 13
Assignments | Code | Handouts | Resources | Email Thurman {Twitter::@compufoo Facebook::CSzero}
{GDT::Bits:: Time  |  Weather  |  Populations  |  Special Dates}

Overview Assignment(s): All assignments have been assigned.

Code: string.cpp | Time.cpp


A struct (structure) is an aggregate of elements of arbitary types. (An array is an aggregate of elements of the same type.)

An example of a structure.

   struct Grade {
      float points;
      char letter;
   };  /* the semicolon after the  }  is required */

Structures are sometimes referred to as records.

A struct consists of zero or more fields (or members).

More about structures.

{TopOfPage} {Tutorial} {CodingGround (online IDE)} {CPP.sh (online IDE)} {C at MIT} {GDT::C/C++ Resource}

Introduction to Classes

We could spend weeks on the topic of class design. When developing a large program or a system (i.e. a collection of programs that work together to provide a specific application), class design is one of the most critical steps in the software development cycle.

In this course sequence we get an introduction to what classes are and how to create and use them using the C++ programming language.

A class is a blueprint from which objects are instantiated (i.e. created). [Classes are also referred to as models or patterns or templates -- although you should avoid using the term template.]

Objects are instances of a class. Objects have memory allocated for them and are usually considered system resources. Memory is allocated from the heap (or free store).]

Object-oriented programs require you to think of programs as being a collection of objects that work together to provide an application.

Object-oriented design is a software design methodology that models the characteristics of abstract or real objects using classes and objects.

The class declares data the defines the state that objects of the class can take on, and methods that define the behavior that the objects can exhibit or be subjected to.

An object is a software "capsule" containing variables and related methods. The values of the variables define an object's state, and the methods define the behavior an object can be subjected to.

A method is a function defined in a class. C++ classes can contain instance methods and class methods. Class methods are defined to be static.

The public methods of a class defines the API or Application Programmer Interface for the class.

The data that is specific to a particular instance of an object is called instance data. Ideally, clients should not have to beware of the instance data in order to use objects of your class.

A class declaration has the following syntax.

   class SomeClassName {

   // class, private and public are C++ keywords

Once an object instantiated (created), messages can be sent to it as follows:

   SomeClassName objectVariable; 


Where objectVariable is the name of a variable that stores the data contained within an object, and methodName is the name of a method defined in the class SomeClassName.

objectVariable is referred to as the recipient object. Or here it described as: methodName() is invoked on behalf of objectVariable. Or the methodName message is sent to objectVariable.

The methodName() method is called. If arguments are supplied, then the value of those arguments are passed to the method (the method receives them as parameters). Implicitly, every method that is invoked on behalf of an object (referred to as an instance method), receives a parameter that has the name this. this is an objectVariable that points to the recipient object.

[class example: Time.cpp | Dog.cpp]

When defining instance variables, you should use private as much as possible. Private instance data members can be accessed only by the methods defined in the class.

Making data private provides the following benefits:

By convention, mutator methods begin with the word set. In many cases, set is followed with the name of instance variable.

When you want the client to be able to access private data, then an accessor or getter method is provided.

By convention, accessor methods begin with the word get which it then often following by the name of the instance variable.

Accessor methods are often covert the value of instance variables from their internal representation to types that are usable by the client.

Accessor methods are usually short methods.

{TopOfPage} {Tutorial} {CodingGround (online IDE)} {CPP.sh (online IDE)} {C at MIT} {GDT::C/C++ Resource}

Home Previous Next