Bic 20904 Objectives
At the end of this lesson, you will be able to understand
or explain about:
Chapter 3 Constructor and Destructor
Class
1 2
Chapter 3: Class
Lets Recall – chapter 2
Based on the given class diagram, write a program
by using OOP techniques to compute the area of a
box
Rectangle
- length Constructor and Destructor
- width
+ void setLength(double)
+ void setWidth(double)
+ double getLength()
+ double getWidth()
+ double getArea()
Chapter 3: Class Chapter 3: Class
Constructor Constructor
Concept Constructor must be a public member function.
Constructor must have same name with the class name.
A constructor is a member function that is Constructor has no return type.
automatically called when a class object is created. Construct a constructor depending on the member functions.
A class can have more than one constructor.
Purpose Constructors can be written either as inline function or
To initialize value for member data outside the class declaration.
or perform other setup functions such as
print a message to demonstrate when the
constructor executes or object is created. Format
class_name :: class_name (parameterList)
Chapter 3: Class Chapter 3: Class
Example 1 Creating Constructor
Example 1 Creating Constructor
class Circle
{ private: Do we have to call constructor
Constructor for class Circle double radius; just like we call member
class Circle
Have same name with the public: functions using object?
{ private:
class name. void setRadius (double r)
double radius;
Put under public visibility. { radius = r; }
public:
void setRadius (double r)
{ radius = r; } Circle()
{ cout<<“This is a constructor for Circle class”;
Circle()
{ cout<<“This is a constructor for Circle class”; };
void main() NO! Once we created the object for the
{ Circle sphere; class, the constructor will be executed
};
} automatically!!
Chapter 3: Class Chapter 3: Class
Example 2 Creating Constructor Constructor
class Demo
{ public: Output?
Demo()
More than one constructor may be defined for a class.
};
Default Constructor
Demo::Demo()
{ cout<<“Now the default constructor is running.\n”;}
void main()
Constructor without any arguments/parameters
{ cout <<“This is displayed before the object is created.\n”;
Demo demoObj; Example Circle()
cout<<“This is displayed after the object is created.\n”; { radius = 0.0; }
}
Chapter 3: Class Chapter 3: Class
Constructor Constructor
More than one constructor may be defined for a class. More than one constructor may be defined for a class.
Constructor with Parameter(s) Constructor with Parameter(s)
Constructor with arguments/parameters Constructor with arguments/parameters
Circle(double r, double p)
Example Circle(double r) Example { radius = r;
{ radius = r; }
pi = p; }
Chapter 3: Class Chapter 3: Class
Constructor Constructor
Example
Create objects depending on constructors class Student
{ private:
Student(char n[ ],int a, char m[ ])
char name[50];
class Circle { strcpy(name,n);
int age;
{ private: age = a;
char matric[10];
double radius; void main() strcpy(matric,m); }
public:
public: { Circle sphere1; };
Student()
Circle() //#1 Circle sphere2 (2.5); void main()
{ strcpy(name,”lela”);
{ radius = 0.0; } Circle sphere3; { Student DIT;
age = 19;
} Student BIT(“Aspa”,20,”AI950120”);
strcpy(matric,”AI970065”);
Circle(double r) //#2 }
}
{ radius = r;}
}; Which Which constructor?
constructor?
Chapter 3: Class Chapter 3: Class
Destructor
Destructor
Concept Destructor must be a public member function.
Destructor must have same name with the class name.
A destructor is a member function that is automatically Destructor has no return type.
called when a class object is destroyed. Destructor cannot accept arguments, never has a parameter list.
A class can only have one destructor.
Use tilde character ( ~ ).
Purpose
To perform shutdown procedures when
the objects goes out of existence.
Format class_name ::~class_name ()
Chapter 3: Class
Example Creating Destructor
class Demo
{ public:
Demo();
~Demo();
Output?
}; Rosziati Ibrahim and Sapiee Jamel (2006), “ Object-Oriented
Programming Using C++: An Introduction”,McGrawHill Sdn.Bhd, Malaysia
Demo::Demo() Deitel, H.M and Deitel P.J( 2005),” C++ How To Program
{ cout<<“The default constructor is running.\n”;} Fifth Edition”, Pearson Education, Inc., New Jersey.
Demo::~Demo() Koffman, Elliot B. and Wolfgang, Paul A.T (2006),”Objects, Abstraction,
{ cout<<“Now the destructor is running.\n”;}
Data Structures and Design Using C++”, John Wiley and Sons,US.
void main() Gaddis, T. et al (2008), “Starting Out with C++ Early Objects”, Pearson
{ Demo demoObj; Education, Inc.
cout<<“The object now exists, but is about to be destroyed.\n”;
}
18