0% found this document useful (0 votes)
101 views

Lecture 3 Constructor and Destructor

Constructors are member functions that initialize objects of a class. They are automatically called when an object is created. A parameterized constructor can take arguments to initialize object data members to different values. Destructors are used to destroy objects and free memory. They are called implicitly when an object goes out of scope. Classes can be placed in separate header files for reusability across programs by including the header file.

Uploaded by

Gafeer Fable
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

Lecture 3 Constructor and Destructor

Constructors are member functions that initialize objects of a class. They are automatically called when an object is created. A parameterized constructor can take arguments to initialize object data members to different values. Destructors are used to destroy objects and free memory. They are called implicitly when an object goes out of scope. Classes can be placed in separate header files for reusability across programs by including the header file.

Uploaded by

Gafeer Fable
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Al-Farabi University College

Initializing Objects with Constructors


A constructor is a member function whose name is the same
as the class name. The constructor is used to initialize the objects
of its class, i.e. it constructs the values of data members of the
class. The constructor is automatically invoked whenever object of
its associated class is created.

// Instantiating multiple objects of the GradeBook class and using


// the GradeBook constructor to specify the course name
// when each GradeBook object is created.
#include <iostream>
#include <string> // program uses C++ standard string class
using namespace std;
// GradeBook class definition
class GradeBook
{
public:
// constructor initializes courseName with string supplied as argument
GradeBook( string name )
{
setCourseName( name ); // call set function to initialize courseName
} // end GradeBook constructor
// function to set the course name
void setCourseName( string name )
{
courseName = name; // store the course name in the object
} // end function setCourseName
// function to get the course name
string getCourseName()
{
return courseName; // return object's courseName
} // end function getCourseName
// display a welcome message to the GradeBook user
void displayMessage()
{
// call getCourseName to get the courseName
cout << "Welcome to the grade book for\n" <<getCourseName()
<< "!" << endl;
} // end function displayMessage
private:
string courseName; // course name for this GradeBook
}; // end class GradeBook
// function main begins program execution

Page 1
Al-Farabi University College

int main()
{
// create two GradeBook objects
GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );
GradeBook gradeBook2( "CS102 Data Structures in C++" );
// display initial value of courseName for each GradeBook
cout << "gradeBook1 created for course: " <<gradeBook1.getCourseName()
<< "\ngradeBook2 created for course: " <<gradeBook2.getCourseName()
<< endl;
system("pause");
} // end main

The output is

Notes
 The constructor should be declared in the public section.
 The constructor does not have return type, (not even void),
and therefore it cannot return any value.
 There is no need to write any statement to invoke the
constructor function because it is invoked automatically
when the object is created.
 A constructor that accepts no arguments is called the default
constructor.
 If no constructor is defined, then the compiler creates an
implicit constructor.
 The constructor can take arguments like other C++ functions.
This is called parameterized constructor.

Page 2
Al-Farabi University College

Parameterized Constructors
Sometimes we need to initialize the data elements of different
objects with different values when they are created. This can be
done by passing arguments to the constructor function when
objects are created. Such constructor is called parameterized
constructor.
#include <iostream>
using namespace std;
class integer
{
private:
int m, n;
public:
integer(int , int ); //parameterized constructor
void printdata();
};
integer :: integer(int x, int y)
{ m = x; n = y; }
void integer :: printdata()
{
cout << "m = " << m << "\n"
<<"n = " << n << "\n";
}
void main()
{
integer intl(1, 100);
intl.printdata();
system("pause");
}

The output of the above program is

Destructors
Page 3
Al-Farabi University College

A destructor is a member function whose name is the same as the


class name but is preceded by a tilde ~ . The destructor is used to destroy the
objects that have been created by a constructor. For example, the destructor
for the class integer is defined as follows:
Notes
 A destructor never takes any arguments nor does it return
any value.
 A destructor will be invoked implicitly by the compiler upon
exit from the program (or block or function) to free memory
space.
#include <iostream>
using namespace std;
int count = 0;
class alpha
{
public :
alpha ()
{
count++;
cout << "\nObject#" << count << " is created ";
}
~alpha()
{
cout << "\nObject#" << count << " is destroyed ";
count--;
}
};
void main()
{
cout << "\n\nENTER MAIN\n";
alpha A1, A2, A3, A4;
{
cout <<"\n\nENTER BLOCK\n";
alpha A5;
alpha A6;
}
cout << "\n\nRE-ENTER MAIN THEN EXIT PROGRAM";
system("pause");
}
The output of the above program is:

Page 4
Al-Farabi University College

Placing a Class in a Separate File for Reusability


One of the benefits of creating class definitions is that, when
packaged properly, your classes can be reused by other
programmers. For example, you can reuse C++ Standard Library
type string in any C++ program by including the header <string>
// GradeBook.h
// GradeBook class definition in a separate file from main.
#include <iostream>
#include <string> // class GradeBook uses C++ standard string class
using namespace std;
// GradeBook class definition
class GradeBook
{
public:
// constructor initializes courseName with string supplied as argument
GradeBook( string name )
{
setCourseName( name ); // call set function to initialize courseName
} // end GradeBook constructor
// function to set the course name
void setCourseName( string name )
{

Page 5
Al-Farabi University College

courseName = name; // store the course name in the object


} // end function setCourseName
// function to get the course name
string getCourseName()
{
return courseName; // return object's courseName
} // end function getCourseName

// display a welcome message to the GradeBook user


void displayMessage()
{
// call getCourseName to get the courseName
cout << "Welcome to the grade book for\n" << getCourseName()
<< "!" << endl;
} // end function displayMessage
private:
string courseName; // course name for this GradeBook
}; // end class GradeBook

// qasim.cpp
// Including class GradeBook from file GradeBook.h for use in main.
#include <iostream>
#include "GradeBook.h" // include definition of class GradeBook
using namespace std;
// function main begins program execution
int main()
{
// create two GradeBook objects
GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );
GradeBook gradeBook2( "CS102 Data Structures in C++" );
// display initial value of courseName for each GradeBook
cout << "gradeBook1 created for course: " <<
gradeBook1.getCourseName()
<< "\ngradeBook2 created for course: " << gradeBook2.getCourseName()
<< endl;
} // end main

Page 6

You might also like