Zebra0.com

cpp rectangleClasses: Creating a simple Rectangle class

Classes: Creating a simple Rectangle class

Learn C++ in step-by-step lessons.

Classes: Creating a simple Rectangle class

In this lesson we will go through several steps to create a rectangle class. These steps will give you an overview of some of the features and advantages of using a class.

Bear in mind that a rectangle is fairly simple, other classes may have a great more complexity.

Note: All code shown works for the latest version of Microsoft Visual Studio. If you are not using tat, please get a simple "Hello World" program working and adjust as necessary.


Goals:

Objectives:

Please study the material at each of the links below.

  1. Class terminology: Basic object oriented terminology
  2. Class Guidelines: Basic guidelines and principles
  3. Rectangle class 1: private width and length Rectangle class 1: private width and length: Step 1 creates the private member varaibles length and width, constructors, and the getter and setter methods.
  4. Rectangle class 2: user input, check for errors Rectangle class 2: user input, check for errors: Step 2 adds user input and checks for errors in main, not the class
  5. Rectangle class 3: friend ostream Rectangle class 3: friend ostream: Step 3 adds ostream as friend so cout<
  6. Rectangle class 4: methods area and diagonal Rectangle class 4: methods area and diagonal: Step 4 adds methods area and diagonal.
  7. Rectangle class 5: overload operators for comparing Rectangle class 5: overload operators for comparing: Step 5 adds comparison operators
  8. Rectangle class 6: overload operators = and + Rectangle class 6: overload operators = and +: Step 6 adds assignment = and addition + operators
  9. Rectangle class 7: Other additions: Step 7 challenges you to add additional methods to the class.
  10. Rectangle class in separate files Rectangle class in separate files: Create the Rectangle as Rectangle.h and Rectangle.cpp
  11. Include the Rectangle class Include the Rectangle class : Include Rectangle.h and Rectangle.cpp
GlossaryGlossary for rectangle lesson
Full Glossary