F Classes
F Classes
Michael
Hanke
Classes
Constructors
and Classes in C++
Destructors
Summary
Michael Hanke
Michael
Hanke
Outline
Classes
Constructors
and
Destructors
Summary
1 Classes
3 Summary
Michael
Hanke
What is a Class?
Classes
Constructors
and
Destructors
Summary
Michael
Hanke
Formal Class Declaration
Classes C++ class declaration
Constructors
and
Destructors
class identifier {
Summary
public:
// Public class members
protected:
// Protected class members
private:
// Private class members
}; // Do not forget the semicolon here!!
Michael
Hanke
Class Declaration (cont)
Classes
Constructors
and
Destructors
Summary
Michael
Hanke
A Simple Class
Classes
Constructors
and
Destructors
Michael
Hanke
A C-Style Implementation
Classes
Constructors
and
Destructors
Summary
class Point {
public:
double x;
double y;
};
Note: The keyword class can be replaced by struct. The latter is
the way one would do it in C.
• The coordinates can be accessed via P.x and P.y using
explicitly the implementation.
• What if we instead would use polar coordinates in the
implementation? The user must rewrite his/her code!
Michael
Hanke
A C++-Style Implementation
Classes
Constructors
and
Destructors
Michael
Hanke
Another Implementation
Classes
Constructors
and
Destructors
class Point {
Summary
private:
double r;
double phi;
public:
double X() { return r*std::cos(phi); }
double Y() { return r*std::sin(phi); }
void zero() { r = phi = 0.0; }
};
The user interface did not change!
• The variables r, phi are called data members of the class.
• The functions X, Y, zero are the member functions of the class.
Michael
Hanke
Programming Style: Separation of
Classes Interface and Implementation
Constructors
and
Destructors
The interface file point.hpp may look like this:
Summary
#ifndef POINT_HPP
#define POINT_HPP
class Point {
double x;
double y;
public:
double X();
double Y();
void zero();
};
#endif
Michael
Hanke
Implementation
Classes
Constructors
and
Destructors #include “point.hpp”
Summary
double Point::X() {
return x;
}
double Point::Y() {
return y;
}
void Point::zero() {
x = y = 0.0;
}
The user of the class will most probably never see the
implementation!
Michael
Hanke
Efficiency Considerations: Inlining
Classes
Constructors
and
Destructors • The principle of data hiding leads often to very many small
Summary member functions.
• Calling a function includes an overhead compared with the
simple data member access (e.g., P.x).
• The overhead can lead to low efficiency if calls happen rather
often (inside innermost loops).
• This overhead can be avoided by function inlining.
• Note: Inlining is a hint to the compiler. The compiler can do it
or not.
• Function bodies defined in header files are inlined be default,
while functions defined in the implementation are not. (Guess
why?)
Michael
Hanke
Efficiency Considerations: const
Classes
Constructors
and
Destructors
Summary
• A compiler can often optimize the code much better if it can use
additonal assumptions about the function behavior.
• One important property is if certain objects are constant.
• Example: In the definition
const int N = 10;
the variable N will never change its value. Doing so will result in
a compilation error.
• As a byproduct, the user interface may become safer.
Michael
Hanke
const And Pointers
Classes
Constructors
and
Destructors
Summary
Michael
Hanke
Efficiency Considerations: point
Classes Class
Constructors
and
Destructors For efficiency, the header file should look like this:
Summary
#ifndef POINT_HPP
#define POINT_HPP
class Point {
private: // Can be omitted here
double x;
double y;
public:
double X() const { return x; }
double Y() const { return y; }
void zero() { x = y = 0.0; }
};
#endif
The keyword const indicates that the object will not change its state
when queuried for the coordinates.
Michael
Hanke
Constructors
Classes
Constructors
and
Destructors
• Constructors determine what happens if an instance of a class
Summary
(an object) is created.
• Built-in data types have default constructors: E.g., a statement
int i; reserves memory for one instance of type integer.
• The initial value of an instance of a built-in type is undefined!
• A definition of the type int i = 0; invokes another type of
constructor, the so-called copy constructor.
• A definition of the kind class variable ; invokes a constructor
class::class()
as a member function of the instance variable. (the so-called
default constructor)
Michael
Hanke
Constructors (cont)
Classes
Constructors
and
Destructors
Summary
• If no constructors are defined in a class, the so-called synthesized
default constructor is automatically defined by the compiler.
• The synthesized default constructor invokes recursively the
default constructors of the data members.
• As soon as at least one constructor is defined in the class, the
default constructor is not available (unless it is explicitely
required by class() = default;)
• Be careful: The synthesized default constructor might not be
what you want! (Shallow vs deep copy)
Michael
Hanke
point Class Constructors
Classes
Constructors
and
Destructors
Summary
• We want something like
Point();
Point(double xx, double yy);
• The default constructor is “do nothing but reserve memory”:
Point() {}
• The next one seems also easy:
Michael
Hanke
point Class Constructors (cont)
Classes
Constructors
• A more efficient way: Use initialization lists:
and
Destructors
Michael
Hanke
Constructors: Intialization Lists
Classes
Constructors
and
Destructors We must use the constructor initializer list to provide values for
Summary
members that are const, reference, or of class type that does not
have a default constructor.
Example:
class ConstRef {
public:
ConstRef(int ii);
private:
int i;
const int ci;
int &ri;
};
Michael
Hanke
Initialization Lists (cont)
Classes
Constructors
and
Destructors
Summary
Correct
ConstRef::ConstRef(int ii): i(ii), ci(ii), ri(i) { }
Errorneous ConstRef::ConstRef(int ii) {
i = ii; // ok
ci = ii; // wrong since ci is const
ri = i; // wrong: ri was never initialized
}
Michael
Hanke
The Copy Constructor
Classes
Michael
Hanke
The Default Copy Constructor
Classes
Constructors
and • The default copy constructor invokes the copy constructors of all
Destructors
Summary
data members.
• For built-in types, this is a simple copy.
• In our example, it is equivalent to:
Point(const Point& Q): x(Q.x), y(Q.y) { }
Note: This is not identical to
Point(const Point& Q) {x = Q.x; y = Q.y; }
Why?
• If the class manages its own dynamic memory (e.g. using new
type[n]), one must most probably define its own copy
constructor!
• Discussion: Should one define one’s own copy constructor?
Michael
Hanke
Remark
Classes
Constructors
and
Destructors
Summary
• In the following, Q is constructed via the copy constructor:
Point P(3.0,5.0);
Point Q = P;
• Compare:
Point P(3.0,5.0), Q;
Q = P;
This case is handled by the copy-assignment constructor! This is
different from the previous one!
Michael
Hanke
Copy Constructor: Efficiency
Classes
Constructors
and
Destructors
Summary
Consider the following ordinary (non-member) function:
const Point negative(const Point P) {
return Point(-P.X(),-P.Y());
}
Michael
Hanke
Efficiency (cont)
Classes
Constructors
and
Destructors
Summary
• Better:
const Point negative(const Point& P) {
return Point(-P.X(),-P.Y());
}
• Note: The return type cannot be const Point&! Why?
• The C++11 and later standards have means to avoid certain
copies of temporary objects (move and move-assignment
constructors).
Michael
Hanke
The Destructor
Classes
Constructors
and
Destructors
Michael
Hanke
Type Conversion
Classes
• What happens in the following situation?
Constructors
and
Destructors double d = 1;
Summary
The constant “1” is int, the variable defined of type double.
• The integer constant is implicitely converted to type double
(1.0) and then assigned.
• In case of the definition
Point P = 1.0;
the constructor Point(1.0) is invoked.
• This way, the constructor includes an implicit type conversion!
• Note: Explicit type conversion (“type casting”) is included in this
mechanism:
Point P,Q;
P = static_cast<Point>(1.0); Q = (Point) 1.0;
Be careful! Avoid explicit type casting!
Michael
Hanke
static Class Members
Classes
Constructors
and
Destructors
• Any member of a class can be static.
Summary
• A static member exists only once for each class. Thus, it is not
bound to a concrete object.
• A static member function does not contain a this pointer. It
can only use static class members.
• Definition of a static member outside of a class body: Omit the
static keyword.
• Static data members must be initialized outside the class (No
constructor will be called!)
• constexpr static data members will be initialized in the class
definition.
Michael
Hanke
Summary
Classes
Constructors
and
Destructors