Classes


This lab discusses:

  1. What is a class
  2. Class definitions
  3. Access control
  4. Constructor
  5. Member function definitions
  6. How to instantiate an object of a class
  7. How to call member functions

Class Definition

class Class_name
{
public:
  constructor declaration
  member function declarations
private:
  data members
};

Member Function Definitions

output Class_name::memberfunName(input parameters)
{
  body of the function
}

Example: Point.cpp (Point class)

Exercise

Write a Rectangle class using the Point class and test it using a main program (i.e., in a main() function).

A rectangle is specified by two corner points. The sides of the rectangle are parallel to the coordinate axes.

Consider the following design description of the Rectangle class:

Knows:
Can do:


BU CAS CS - Introduction to Classes
This page created by Jisook Youn <jisook@cs.bu.edu>.
Material partially taken from the textbook "Computing Concepts with C++ Essentials", by Horstmann.