2. Objectives
To learn how to create objects with new
To learn the use of destructor
To study constructor initializer list
To study accessor and mutator methods
To see the working of Default copy constructor and
Member-wise Copy
To learn the scope of variables
3. A Simple Program – Object Creation
class Circle
{
private:
double radius;
public:
Circle()
{ radius = 5.0; }
double getArea()
{ return radius * radius * 3.14159; }
};
Object InstanceC1
: C1
radius: 5.0
void main()
{
Circle c1;
Circle *c2 = new Circle();
cout<<“Area of circle = “<<c1.getArea()<<c2.getArea();
}
Allocate memory
for radius
Object InstanceC2
: C2
radius: 5.0
Allocate memory
for radius
4. Destructor
A destructor is a member function with the same
name as its class prefixed by a ~ (tilde).
Destructors are usually used to deallocate memory
and do other cleanup for a class object and its class
members when the object is destroyed.
A destructor is called for a class object when that
object passes out of scope or is explicitly deleted.
5. Destructor
A destructor takes no arguments and has no return
type.
Destructors cannot be declared const, volatile, const
or static. A destructor can be declared virtual or pure
virtual.
If no user-defined destructor exists for a class and one
is needed, the compiler implicitly declares a destructor.
This implicitly declared destructor is an inline public
member of its class.
6. Destructor - Example
class Circle
{
private:
double radius;
public:
Circle();
Circle(double);
double getArea();
~Circle();
};
Circle::Circle()
{ radius = 1.0; }
Circle::Circle(double rad)
{ radius = rad; }
double Circle::getArea()
{ return radius * radius * 3.14; }
Circle::~Circle() { cout<<“Destructor called”; }
void main()
{
Circle c1;
Circle *c2 = new Circle(10.7);
cout<<“Area of circle = “<<c1.getArea();
cout<<“Area of circle = “<<c2.getArea();
}
Class
declaration
Class
Implementation
8. Member Initialization List
One task of the Constructor is to initialize data members
Example: for class Circle
Circle(){ radius = 1.0; }
But it is not preferred approach. The preferred approach is
Circle(): radius(1.0)
{ }
If multiple members be to initialize, separate them by
commas. The result is the initializer list (also known as
member-initialization list)
Circle(): radius(1.0), area(0.0)
{ } Member
Initialization List
9. Reasons for Constructor Initializers
In the initializer list, members initialized with given
values before the constructor even starts to execute
This is important in some situations e.g. the initializer
list is the only way to initialize const member data
and references
However, actions more complicated than simple
initialization must be carried out in the constructor
body, as with ordinary functions.
10. Accessor (getter) and Mutator (Setter) Functions
Colloquially, a get function is referred to as a getter (or
accessor), and a set function is referred to as a setter (or
mutator).
A get function has the following header:
returnType getPropertyName(){ }
A set function has the following header:
void setPropertyName(dataType propertyValue){ }
11. Example
class VehiclePart{
private:
int modelno; int partno; float cost;
public:
VehiclePart():modelno(0),partno(0),cost(0.0)
{ }
void setModelNo(int mn){ modelno = mn; }
void setPartNo(int pn){ partno = pn; }
void setCost(float c){ cost = c; }
void setVehDetail(int mn, int pn, float c)
{ modelno = mn; partno = pn; cost = c; }
int getModelNo(){ return modelno; }
int getPartNo(){ return partno; }
float getCost(){ return cost; }
void showVehDetail()
{ cout<<modelno<<partno<<cost; }
};
Mutator
Functions
Accessor
Functions
13. Default copy constructor
A type of constructor that is used to initialize an
object with another object of the same type is
known as default copy constructor.
It is by default available in all classes
syntax is ClassName(ClassName &Variable)
15. Memberwise Copy
Another different format has exactly the same
effect, causing object to be copied member-by-
member into other object.
Remember, object1 = object2 will not invoke the
copy constructor. However, this statement is legal
and simply assigns the values of object2 to object1,
member-by-member. This is the task of the
overloaded assignment operator (=).
18. The Scope of Variables
Global variables are declared outside all functions
and are accessible to all functions in its scope. The
scope of a global variable starts from its declaration
and continues to the end of the program.
Local variables are defined inside functions. The
scope of a local variable starts from its declaration
and continues to the end of the block that contains
the variable.
19. The Scope of Variables
The data fields are declared as variables and are accessible to
all constructors and functions in the class.
Data fields and functions can be declared in any order in a
class. For example, all the following declarations are the same:
class Circle
{
public:
Circle();
Circle(double);
private:
double radius;
public:
double getArea();
double getRadius();
void setRadius(double);
};
(a) (b)
class Circle
{
private:
double radius;
public:
double getArea();
double getRadius();
void setRadius(double);
public:
Circle();
Circle(double);
};
class Circle
{
public:
Circle();
Circle(double);
double getArea();
double getRadius();
void setRadius(double);
private:
double radius;
};
(c)
20. The Scope of local variables in a class
Local variables are declared and used inside a
function locally.
In a class, if a local variable has the same name as
a data field, the local variable takes precedence
and the data field with the same name is hidden.