The document discusses constructors and destructors in C++. It explains that constructors initialize objects and are called automatically when an object is created. Constructors have the same name as the class and do not have a return type. Destructors are called when an object is destroyed to deallocate memory. The document provides an example of a Distance class with overloaded constructors and a member function to add distances defined outside the class.
2. Constructors
? C++ requires a construct call for each object it has created
? This ensure that object is initialized properly before it is used
? If there is no constructor, the compiler provides a default constructor that is, a
constructor with no parameters
? Name of constructor function is same as name of class
3. A counter example
? Data member
? Count
? Member function
? Constructor
? void inc_count()
? int get_count()
4. Automatic initialization
? An object of type Counter is first created, we want its count to be
initialized to 0
? Options are
? set_count() function (call it with an argument of 0)
? zero_count() function, to set count to 0.
? Such functions would need to be executed every time we created a
Counter object
5. Cont.
? A programmer may forget to initialize the object after creating it
? Its more reliable and convenient to cause each object to initialize implicitly when
it is created
? In the Counter class, the constructor Counter() is called automatically whenever a
new object of type Counter is created
? Counter c1, c2;
creates two objects. Constructor is called with each object separately
6. Constructor Name
? First, constructor name must be same as the name of class
? This is one way the compiler knows they are constructors
? Second, no return type is used for constructors
? Why not? Since the constructor is called automatically by the system, theres no
program for it to return anything to; a return value wouldnt make sense
? This is the second way the compiler knows they are constructors
7. Initializer List
? One of the most common tasks a constructor carries out is initializing data members
? In the Counter class the constructor must initialize the count member to 0
? The initialization takes place following the member function declarator but before the function body.
? Initialization in constructors function body
Counter()
{ count = 0; }
this is not the preferred approach
8. Cont.
? Its preceded by a colon.The value is placed in parentheses following the member
data.
Counter() : count(0)
{ }
? If multiple members must be initialized, theyre separated by commas.
? someClass() : m1(7), m2(33), m3(4) initializer list { }
9. Destructors
? Destructor is a function called implicitly when an object is destroyed
? The name of the destructor for a class is the tilde character (~) followed by the
class name
? No arguments and no return type for a destructor
? The most common use of destructors is to deallocate memory that was allocated
for the object by the constructor
10. Validating Data with set Functions
? A set function should validate the value before assigning to private data member
? Set function can return a value or may display a message if invalid data is assign to
object
14. Overloaded Constructors
? Its convenient to be able to give variables of type Distance a
value when they are first created
Distance dist2(11, 6.25);
? which defines an object, and initializes it to a value of 11 for feet
and 6.25 for inches.
? Distance dist1, dist2; then No-argument constructor is
called/invoked (the default constructor)
? Since there are now two constructors with the same name,
Distance(), we say the constructor is overloaded
15. Member Functions Defined Outside the
Class
? Such functions, needs to have a prototype/declaration within the
class
? The function name, add_dist(), is preceded by the class name,
Distance, and a new symbolthe double colon (::).This symbol is
called the scope resolution operator.
? It is a way of specifying what class something is associated with
? In this situation, Distance::add_dist() means the add_dist() member
function of the Distance class
void Distance::add_dist(Distance d2, Distance d3)