Class Definitions
Object-Oriented C++ Basics
Classes vs. Structs
- Classes are an extension to structs and the two are nearly identical
- Only difference between C++'s structs and classes:
- class members are private by default
- struct members are public by default
- Standard coding convention:
- use stucts for "dumb data" with no methods
- use classes for anything else
Organization of program
- Header files contain class declarations (interface)
- Program files contain class (function) definitions (implementation)
-
main()function still needed for execution starting point
Object oriented (OO) programming language features
- Generally objects = fields + methods but C++ terminology is "objects = data members + member functions"
- public interface, private implementation
- Multiple polymorphism forms in OO
- object polmorphism via subclassing
- templates/generics
- function & operator overloading
- inheritance
- abstract base classes
OO Abstraction & modelling
- data members (properties) - can be of different types or classes
- function members (behaviors)
-- can be defined there or elsewhere
-- assume full access to data members
Class Components
Information hiding of class members
- public parts - anybody can access
- private parts - access by same class members only
- declared via
public:andprivate:in .h file
Member functions
- Defined with
MyClass::myfunc(args) { ... code ... } - Can be implemented independently of class declaration, interspersed with other non-class 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:
- Default constructor - no parameters; primitive fields are not initialized, object fields are initialized with their default constructors
- Conversion constructor - takes only one parameter, compiler then uses for type conversions from values of that parameter type to the class type
- Copy constructor - has parameter of the class type; default version is shallow copy of data fields (similar to default assignment operator)
- Alternate constructors - have any combination of parameters different from those above
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
- explicitly when initializing an object in declaration
- implicitly when passing an object call by value
- implicitly when returning an object by value
Initializer lists for data members
- Special syntax for initializing fields
-
Point :: Point () : x(0), y(0) { }initializes x and y to 0 - initialization happens in the order of declaration, not list order above (doesn't usually matter though)
- All data members initialized even if not listed explicitly
- Body of constructor can be used to change or do more initializations
- May need to do this for members of other class types, particularly if they don't have default constructors
Constructors using default values
- Can leave off certain arguments if they have a default value listed
- Defaults only declared in the header, not in the definition!
- Function implementation is as before
- Must be defaulted from right to left when used, as in
Point (int xy=0, int yv=0)- calling options are only to leave out right-most params
Point p1(3, 4); // use both
Point p2(3); // use x; y defaults to 0
Point p3; // both default to 0
Destructors
- Named by prepending
~to the class name -MyClass::~MyClass() { ... } - Destructor code invoked when object lifetime ends
- Destruction is shallow (no destruction of objects referenced) unless code
...does more - Locally declared objects are destroyed at function return - their destructors are called right before their memory is freed
- C++ provides a destructor for your class if you don't explicitly write one.
Other C++ class definition features
Inline function definitions
- Code is not a function call, its inlined by compiler for speed
- Since its like a macro its OK to put this code in
.hfile - Need semicolon:
int Myclass::getX() { return x ; } ;
Instance variables (data members)
- They are like struct fields
- They are not automatically references, as in Java - use
&varto make a reference - The class constructor should set their initial value
The this pointer to current object
- Can use
this->datamemberinstead of justdatamemberfor clarification - Can use
*thisas name for current object - Like
thisin Java, you don't often need to use it
Constant member functions
- Use
constat end of function header to say that it can't modify any data members of itself (the current object) - Must use
constin both prototype and implementation header - Can be applied to
constand non-constqualified objects - A non-
constfunction cannot be applied toconstqualified object - violates spirit ofconst
static class members
- Class-wide info, shared by all objects
- Similar to
staticin Java classes - Declare data:
static int sharedint; - Can be accessed through class object or globally via
classname::(if public) - Declare functions: static int changeshared (int);
- Function can only be static if it only accesses static data
- No
thisfor static members
Class composition - objects in classes
- Class data members are constructed in order of declaration and before the class constructor is executed
- May need to initialize member objects with constructor initializer list if they don't have default constructors
Misc points about classes
Operators = and == on objects:
-
=assignment operator - memberwise assignment is provided by default (not deep copy) -
==equality operator is not provided automatically for your class objects, you have to define it explicitly (see Overloading)