KEMBAR78
4.Constructor.pptx for oops programming language | PPTX
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Constructors
Constructors
Classes control object initialization by defining one or more
special member functions known as constructors.
The job of a constructor is to initialize the data members of a
class object.
A constructor is run whenever an object of a class type is
created.
Constructors have the same name as the class.
Unlike other functions, constructors have no return type.
Constructors can not be virtual.
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Constructors
•A class can have multiple constructors.
•Like any other overloaded function, the constructors
must differ from each other in the number or types of
their parameters.
•Constructors are the special type of member functions
that initializes the object automatically when it is
created.
•Compiler identifies that the given member function is a
constructor by its name
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Constructors
•A class can have multiple constructors.
•Like any other overloaded function, the constructors
must differ from each other in the number or types of
their parameters.
•Constructors are the special type of member functions
that initializes the object automatically when it is
created.
•Compiler identifies that the given member function is a
constructor by its name
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Constructor - Syntax
class classname {
public:
//Declaring the default constructor.
classname() {
}
};
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Types of constructors
•There are several forms in which a constructor can
take its shape namely:
•Default Constructor
•Parameterized Constructor
•Copy Constructor
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Default constructor
•Classes control default initialization by defining a
special constructor, known as the default constructor.
The default constructor is one that takes no arguments.
•If the class does not explicitly define any constructors,
the compiler will implicitly define the default
constructor.
•The compiler-generated constructor is known as the
synthesized default constructor.
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Default constructor – Example1
int main()
{
math o;
o.add();
return 0;
}
Dr.J.saira banu , Associate Professor, SCOPE, VIT
University
class math
{
private:
int a,b,c;
public:
math()
{
a=0;
b=0;
}
void add()
{
c=a+b;
cout<<"Total : "<<c;
}
};
Default constructor -Example
#include <iostream>
using namespace std;
class Base {
public: int square;
Base(int x = 5) // Default constructor with a default value.
square = x * x;
}
void show() {
cout << "Square of number =" << square << endl;
}
};
int main() {
Base b;
b.show();
return 0; }
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Parameterized constructor
•A parameterized constructor is the one that has
parameters or arguments specified in it.
•We can pass the arguments to constructor function
when object is created.
•A constructor that can take arguments is called
parameterized constructor.
•When a constructor is parameterized, we must pass
the initial values as arguments to the constructor
function when an object is declared.
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Parameterized constructor -Syntax
class classname {
public:
//Declaring the default constructor.
classname( int, int ) {
}
};
Classname objname(list of parameters);
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Parameterized constructor -example
• class math
• {
• private:
• int a,b,c;
• public:
• math(int x,int y)
• {
• a=x;
• b=y;
• }
• void add()
• {
• c=a+b;
• cout<<"Total : "<<c;
• }
• };
Dr.J.saira banu , Associate Professor, SCOPE, VIT
University
• int main()
• {
• math o(10,25);
• o.add();
• return 0;
• }
Copy constructor
• A copy constructor is a member function which initializes an object
using another object of the same class. It is used to declare and
initialize an object from another object.
• The most common form of copy constructor is shown here:
• classname (const classname &obj)
• { // body of constructor }
• Here, obj is a reference to an object that is being used to initialize
another object.
• Copy constructor object creation:
• integer (integer & i) ; (or)
• integer I 2 ( I 1 ) ; (or)
• integer I 2 = I 1 ;
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Copy constructor
• The copy constructor creates an object by initializing it with an object
of the same class, which has been created previously.
• The copy constructor is used to:
• Initialize one object from another of the same type.
• Copy an object to pass it as an argument to a function.
• Copy an object to return it from a function.
• The process of initializing through a copy constructor is known as copy
initialization.
• A reference variable has been used as an argument to the copy
constructor as we cannot pass the argument by value to a copy
constructor
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Copy Constructor Example
• class math
• {
• private:
• int a,b,c;
• public:
• math(int x,int y)
• {
• a=x;
• b=y;
• }
• math(math &x1)
• {
• a=x1.a;
• b=x1.b;
• }
• void add()
• {
• c=a+b;
• cout<<"Total : "<<c<<endl;
• }
• int main()
• {
• math o(10,25);
• math o1(o);
• o.add();
• o1.add();
• return 0;
• }
Dr.J.saira banu , Associate Professor, SCOPE, VIT
University
Destructors
• A destructor is a special member function of a class that is executed
whenever an object of it's class goes out of scope.
• A destructor will have exact same name as the class prefixed with a
tilde (~).
• It can neither return a value nor can it take any parameters.
• Destructor can be very useful for releasing resources before coming
out of the program like closing files, releasing memories etc.
• The destructor is commonly used to "clean up" when an object is no
longer necessary
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Destructors
•Destructors are called when one of the following
events occurs:
•An object allocated using the new operator is explicitly
deallocated using the delete operator.
•A local (automatic) object with block scope goes out of
scope.
•The lifetime of a temporary object ends.
•A program ends and global or static objects exist.
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Destructor syntax
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Destructor Example
Dr.J.saira banu , Associate Professor, SCOPE, VIT University

4.Constructor.pptx for oops programming language

  • 1.
    Dr.J.saira banu ,Associate Professor, SCOPE, VIT University Constructors
  • 2.
    Constructors Classes control objectinitialization by defining one or more special member functions known as constructors. The job of a constructor is to initialize the data members of a class object. A constructor is run whenever an object of a class type is created. Constructors have the same name as the class. Unlike other functions, constructors have no return type. Constructors can not be virtual. Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 3.
    Constructors •A class canhave multiple constructors. •Like any other overloaded function, the constructors must differ from each other in the number or types of their parameters. •Constructors are the special type of member functions that initializes the object automatically when it is created. •Compiler identifies that the given member function is a constructor by its name Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 4.
    Constructors •A class canhave multiple constructors. •Like any other overloaded function, the constructors must differ from each other in the number or types of their parameters. •Constructors are the special type of member functions that initializes the object automatically when it is created. •Compiler identifies that the given member function is a constructor by its name Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 5.
    Constructor - Syntax classclassname { public: //Declaring the default constructor. classname() { } }; Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 6.
    Types of constructors •Thereare several forms in which a constructor can take its shape namely: •Default Constructor •Parameterized Constructor •Copy Constructor Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 7.
    Default constructor •Classes controldefault initialization by defining a special constructor, known as the default constructor. The default constructor is one that takes no arguments. •If the class does not explicitly define any constructors, the compiler will implicitly define the default constructor. •The compiler-generated constructor is known as the synthesized default constructor. Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 8.
    Default constructor –Example1 int main() { math o; o.add(); return 0; } Dr.J.saira banu , Associate Professor, SCOPE, VIT University class math { private: int a,b,c; public: math() { a=0; b=0; } void add() { c=a+b; cout<<"Total : "<<c; } };
  • 9.
    Default constructor -Example #include<iostream> using namespace std; class Base { public: int square; Base(int x = 5) // Default constructor with a default value. square = x * x; } void show() { cout << "Square of number =" << square << endl; } }; int main() { Base b; b.show(); return 0; } Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 10.
    Parameterized constructor •A parameterizedconstructor is the one that has parameters or arguments specified in it. •We can pass the arguments to constructor function when object is created. •A constructor that can take arguments is called parameterized constructor. •When a constructor is parameterized, we must pass the initial values as arguments to the constructor function when an object is declared. Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 11.
    Parameterized constructor -Syntax classclassname { public: //Declaring the default constructor. classname( int, int ) { } }; Classname objname(list of parameters); Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 12.
    Parameterized constructor -example •class math • { • private: • int a,b,c; • public: • math(int x,int y) • { • a=x; • b=y; • } • void add() • { • c=a+b; • cout<<"Total : "<<c; • } • }; Dr.J.saira banu , Associate Professor, SCOPE, VIT University • int main() • { • math o(10,25); • o.add(); • return 0; • }
  • 13.
    Copy constructor • Acopy constructor is a member function which initializes an object using another object of the same class. It is used to declare and initialize an object from another object. • The most common form of copy constructor is shown here: • classname (const classname &obj) • { // body of constructor } • Here, obj is a reference to an object that is being used to initialize another object. • Copy constructor object creation: • integer (integer & i) ; (or) • integer I 2 ( I 1 ) ; (or) • integer I 2 = I 1 ; Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 14.
    Copy constructor • Thecopy constructor creates an object by initializing it with an object of the same class, which has been created previously. • The copy constructor is used to: • Initialize one object from another of the same type. • Copy an object to pass it as an argument to a function. • Copy an object to return it from a function. • The process of initializing through a copy constructor is known as copy initialization. • A reference variable has been used as an argument to the copy constructor as we cannot pass the argument by value to a copy constructor Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 15.
    Copy Constructor Example •class math • { • private: • int a,b,c; • public: • math(int x,int y) • { • a=x; • b=y; • } • math(math &x1) • { • a=x1.a; • b=x1.b; • } • void add() • { • c=a+b; • cout<<"Total : "<<c<<endl; • } • int main() • { • math o(10,25); • math o1(o); • o.add(); • o1.add(); • return 0; • } Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 16.
    Destructors • A destructoris a special member function of a class that is executed whenever an object of it's class goes out of scope. • A destructor will have exact same name as the class prefixed with a tilde (~). • It can neither return a value nor can it take any parameters. • Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc. • The destructor is commonly used to "clean up" when an object is no longer necessary Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 17.
    Destructors •Destructors are calledwhen one of the following events occurs: •An object allocated using the new operator is explicitly deallocated using the delete operator. •A local (automatic) object with block scope goes out of scope. •The lifetime of a temporary object ends. •A program ends and global or static objects exist. Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 18.
    Destructor syntax Dr.J.saira banu, Associate Professor, SCOPE, VIT University
  • 19.
    Destructor Example Dr.J.saira banu, Associate Professor, SCOPE, VIT University