Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
70 views

CPP - Unit II - Constructor and Distructor

The document discusses constructors and destructors in C++, including their characteristics and different types. It explains default constructors, parameterized constructors, copy constructors, and constructor overloading. It also covers static class members and how constructors and destructors can be used with static members to track the number of objects created for a class. An example program is provided to demonstrate defining a constructor and destructor with a static member to output the number of existing objects each time an object is created or destroyed.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

CPP - Unit II - Constructor and Distructor

The document discusses constructors and destructors in C++, including their characteristics and different types. It explains default constructors, parameterized constructors, copy constructors, and constructor overloading. It also covers static class members and how constructors and destructors can be used with static members to track the number of objects created for a class. An example program is provided to demonstrate defining a constructor and destructor with a static member to output the number of existing objects each time an object is created or destroyed.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

KG College of Arts and Science

Affiliated to Bharathiar University and Accredited by NAAC


KGiSL Campus, Saravanampatti, Coimbatore, Tamilnadu, India.

Department of Information Technology

Subject Name : C++ Programming

Subject Code :2

Class : I B.Sc. IT ‘A’

Semester : II

Prepared by : Mrs.G.Gokilavani
Assistant Professor
Department of Information Technology
KG College of Arts and Science
Unit II
Constructor and destructor with
static members
Constructor and destructor with static members

• Constructors and Destructors


– Constructors and Destructors
– Characteristics of Constructors and Destructors
– Constructor with Arguments
– Overloading Constructors
– Constructors with default Arguments
– Copy constructors
– Destructors
– Constructor and destructor with static members
Constructor

• A class constructor is a special member


function of a class that is executed whenever
we create new objects of that class.
• A constructor will have exact same name as
the class and it does not have any return type
at all, not even void. Constructors can be very
useful for setting initial values for certain
member variables.
Constructor
• The syntax of defining a constructor function in a class:
class A {
public:
int x; // constructor
A() { // object initialization
}
};
• Types of constructor
– Default constructor
– argument constructor
#include <iostream>
using namespace std;
class construct {
public:
int a, b;
// Default Constructor
construct(){
a = 10;
b = 20;
}
// Argument Constructor
construct(int x,int y){
cout << "x: " << x <<"y: "<< y <<endl;
}
};
int main() {
// Default constructor called automatically when the object is created
construct c;
cout << "a: " << c.a << endl;
cout << "b: " << c.b;
//argument constructor call
construct c1(500,1000);
return 0;
}
Output:
a: 10
b: 20
x: 500 y: 1000
Destructors 

• Destructor is a special class function which destroys the object as soon


as the scope of object ends. The destructor is called automatically by the
compiler when the object goes out of scope.
• The syntax for destructor is same as that for the constructor, the class
name is used for the name of destructor, with a tilde ~ sign as prefix to it.
class A {
public:
// defining destructor for class
~A()
{
// statement
}
};
Destructors 

#include<iostream.h>
#include<conio.h>
class exam{
private:
int sno,mark1,mark2;
public:
exam( ){ //constructor
sno=mark1=mark2=100;
}
void showdata() {
cout<<sno<<mark1<<mark2
}
~exam(){ //Destructor
cout<<"destructo is invoked";
}};
int main(){
exam e;
e.showdata();
exam e1(1,90,90);
clrscr();
e1.showdata();
return 0;
}
Output:
Sno = 100 Mark1 = 100 Mark2 =100
Sno = 100 Mark1 = 100 Mark2 =100
destructor is invoked
Characteristic of constructors and
Destructors
Characteristic of constructors and
Destructors
Characteristic of constructors and
Destructors
Constructors with default Arguments
• A Default constructor is a type of constructor which doesn’t take any argument and has no parameters.
• Example:
#include <iostream>
using namespace std;
class test {
public:
int y, z;
test()
{
y = 7;
z = 13;
}
};
int main()
{
test a;
cout <<"the sum is: "<< a.y+a.z;
return 1;
}
• Output: the sum is: 20
Constructors with default Arguments
•Explanation:
–The above program is a basic demo of a constructor in c++. We have a class test,
with two data members of type int called y and z. Then we have a default
constructor, which assigns 7 and 13 to the variables y and z respectively.
–The above program is a basic demo of a constructor in c++. We have a class test,
with two data members of type int called y and z. Then we have a default
constructor, which assigns 7 and 13 to the variables y and z respectively.
–The main function has an object of class test called a. When this object is created
the constructor is called and the variables y and z are given values. The main
function has a cout statement or a print statement. In this statement, the sum is
printed. With respect to the object of the class, we access the public members of
the class, that is, a.y gives the value of y and the same for z. We display the sum of
y and z.
–The default constructor works this way. When we do not provide a default
constructor, the compiler generates a default constructor which does not operate.
Constructor with Arguments

• Passing of parameters to the constructor is possible. This is done to


initialize the value using these passed parameters. This type of
constructor is called a Constructor with Arguments.
The constructor is defined as follows:
test(int x1)
{
x = x1;
}
• There is a parameter that is passed to the constructor. The value is
passed when the object is created in the main function as shown below.
• test t(10);
• Inside the main function, we create an object of class test and pass the
value of the variable.
Constructor with Arguments
#include <iostream>
using namespace std;  
class test {
public:
int x;
test(int x1)
{
x = x1;
}     
int getX()
{
return x;
}    
};  
int main()
{   
test a(10);
cout << "a.x = " << a.getX() ;
return 0;
}
• Output:
• a.x = 10
Constructor with Arguments
• Explanation
– The above program is a basic demo of a parameterized constructor. We
have a class test, with one data member of type int called x. Then we
have a parameterized constructor, with x1 as the parameter passed
from the main function, while object creation.
– The constructor assigns data member x the value of x1. We have a getX
function next that simply returns the value of x.
– The main function has an object of class test called a. There is a value
associated with this object, this is a parameter.
– test a(10);
– We then print the value of x using the member function getX. We call
the function with respect to the object of the test class called a.
Overloading Constructors

• Overloaded constructors have the same name


(name of the class) but the different number
of arguments. Depending upon the number
and type of arguments passed, the
corresponding constructor is called.
Overloading Constructors
• // C++ program to demonstrate constructor overloading
#include <iostream>
using namespace std;
class Person {
private: int age;
public:
// 1. Constructor with no arguments
Person()
{ age = 20;
}
// 2. Constructor with an argument
Person(int a)
{
age = a;
} int getAge()
{ return age;
}
};
int main()
{
Person person1, person2(45);
cout << "Person1 Age = " << person1.getAge() << endl;
cout << "Person2 Age = " << person2.getAge() << endl;
return 0;
}
• Output
Person1 Age = 20 Person2 Age = 45
Overloading Constructors

• Explanation:
• In this program, we have created a class Person that has a single
variable age.
• We have also defined two constructors Person() and Person(int a):
• When the object person1 is created, the first constructor is called
because we have not passed any argument. This constructor
initializes age to 20.
• When person2 is created, the second constructor is called since we
have passed 45 as an argument. This constructor
initializes age to 45.
• The function getAge() returns the value of age, and we use it to
print the age of person1 and person2.
Copy constructors

• The constructor can accept arguments of any data type


including user-defined data types and an object of own class.
Consider the example:
Statement (a):Statement (b)
Class num Class num
{ {
Private: Private:
---- ----
---- ----
---- ----
Public: Public:
num(num); num(num&);
} }
Copy constructors

• In statement (a) an argument of the constructor is same as that of


its class.
• It is possible to pass reference of object to the constructor. Such
declaration is know as copy constructor.
• The statement (b) is valid and can be used to copy constructor.
• When pass an object by value into a function, a temporary copy of
that object is created. All copy constructors require on argument,
with reference to an object of that class.
• It is possible to declare and initialize one object using reference of
another object.
• Thus, whenever a constructor is called a copy of an object is
created.
Copy constructors
Constructor and destructor with static members

• Every object has its own set of data members. When a


member function is invoked, only copy of data member
of calling object is available to the function.
• Sometimes it is necessary for all the objects to share
data fields which is common for all the objects. If the
member variable is declared as static, only one copy of
such member is created for entire class.
• All objects access the same copy of static variable.
• The static member variable can be used to count
number of objects declared for a particular class.
Constructor and destructor with static
members
#include<iostream.h>
#include<constream.h>
Class man
{
static int no;
Char name;
Int age;
Public:
Man()
{
no++;
cout << “\n Number of objects exist: “<<no;
}
~man()
{
--no;
cout << “\n Number of obect exist : “<<no;
}
};
int man :: no=0;
Void main()
{
Clarscr();
Man A,B, C;
Cout <<“\n Press any key to destroy object “;
getch();
}
Constructor and destructor with static
members
Output
Number of Objects exist: 1
Number of Objects exist: 2
Number of Objects exist: 3
Press any key to destroy object
Number of Objects exist: 2
Number of Objects exist: 1
Number of Objects exist: 0
Constructor and destructor with static members
• In this program, the class man has one static data member no.
The static data member is initialized to zero. Only one copy of
static data member is created and all objects share the same
copy of static data member.
• In function main(), objects A,B, C are declared constructor is
executed and static data member no is increased with one.
• The constructor also displays the value of no on the screen.
• The value of static member shows us the number of objects
present.
• When the user presses a key, destructor is executed, Which
destroys the object. The value of static variable is decreased
in the destructor.
• The value of static member variable shows number of existing
objects.
• Thank you…

You might also like