Classes in C++

Class Definitions

Object-Oriented C++ Basics

Classes vs. Structs

Organization of program

Object oriented (OO) programming language features

OO Abstraction & modelling

Class Components

Information hiding of class members

Member functions

Syntax for class interface (in *.h file)

class classname {
     // complete member list
};   // semicolon is very important

Constructors and destructors

Constructors are used to initialize objects and each class can have many:

C++ automatically provides a default constructor and a (shallow) copy constructor for every class if you don’t explicitly create any constructors. However, if you define any constructor, default ones are not created automatically for the class.

Copy constructors are invoked

Initializer lists for data members

Constructors using default values

Point p1(3, 4); // use both
Point p2(3);    // use x; y defaults to 0
Point p3;       // both default to 0

Destructors

Other C++ class definition features

Inline function definitions

Instance variables (data members)

The this pointer to current object

Constant member functions

static class members

Class composition - objects in classes

Misc points about classes

Operators = and == on objects: