Introducing Classes: Constructor Function
Introducing Classes: Constructor Function
Lecture Two
Introducing Classes
Constructor Function
In real problem, virtually every object requires some sort of initialization.
Constructor function performs the tasks of initialization.
A class’s constructor is called each time an object of that class is created.
A constructor function has the same name as the class of which it is a part
and has no return type.
1
7/16/2014
Destruction Function
This function is called when an object is destroyed.
While working with object, some actions may be performed when an object is
destroyed, e.g., freeing the memory allocated by the object.
Local objects are destroyed when they go out of scope. Global objects are
destroyed when the program ends.
2
7/16/2014
Introducing Inheritance
Inheritance is the mechanism by which one class can inherit the properties of another.
When one class is inherited by another, the class that is inherited is called the base
class. The inheriting class is called the derived class.
Base class represents the most general description of a set of traits. A derived class
inherits those general traits and adds properties that are specific to that class.
All the private elements of the base class remain private to it and are not directly accessible by the
derived class.
Object Pointers
When a pointer to the object is used, the arrow operator (→) is employed rather than
dot (.) operator.
Just like pointers to other types, an object pointer, when incremented, will point to the
next object of its type.
3
7/16/2014
Like structure, by default, all members of a union are public until the private specifier is used.
Like union in C, all data members share the same memory location.
Though union, itself, can have a constructor and destructor; they must not contain any object that
has a constructor or destructor.
4
7/16/2014
An anonymous union tells the compiler that its members will share the same memory location.
Members of an anonymous union act and are treated like normal variable, i.e., the members are
accessed directly, without the dot operator.
Anonymous unions have all the restrictions that apply to the normal unions, plus these additions-
The names of the members of an anonymous union must not conflict with other identifiers within
the same scope.
In-line function
In-line functions are not actually called, rather are expanded in line, at the point of each call. (like
macros in C).
Advantage: in-line function has no overhead associated with the function call and return mechanism,
so much faster than the normal function calls.
Disadvantage: If in-line functions are too large and called too often, program grows larger. Therefore,
only short functions are declared in-line.
#include <iostream> int main(){
using namespace std; if (even(10)) cout << “10 is even.\n”;
inline int even(int x){ return 0;
return !(x%2); }
}
If any inline restriction is violated, the compiler is free to generate a normal function.
5
7/16/2014
When a function is defined within a class declaration, the inline keyword is no longer necessary.
#include <iostream>
using namespace std; int main(){
samp ob1(10, 2), ob2(10, 3);
class samp {
int i, j; if (ob1.divisible()) cout << “10 is divisible by 2\n”;
public: if (ob2.divisible()) cout << “10 is divisible by 3\n”;
samp( int a, int b);
int divisible() { return !(i%j):} return 0;
}; }
samp::samp(int a, int b){
i = a;
j = b;
}
When a function defined inside a class declaration cannot be made into in-line function, it is
automatically made into a regular function..
From the compiler’s point of view, there is no difference between the compact style and the standard
style, however, compact style is commonly used in C++ programs.
The same restriction that apply to a normal in-line functions apply to automatic in-line functions within
a class declaration.