Classes
This lab discusses:
-
What is a class
-
Class definitions
-
Access control
-
Constructor
-
Member function definitions
-
How to instantiate an object of a class
-
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:
-
- its bottom-left corner point
- its top-right corner point
- Can do:
-
- Initialization
- Return its bottom-left corner point
- Return its top-right corner point
- Return its circumference
- Return its area
- Move by dx and dy
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.