Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Destructor on the other hand is used to destroy the class object.
This Powerpoint presentation covers following topics of C Plus Plus:
Features of OOP
Classes in C++
Objects & Creating the Objects
Constructors & Destructors
Friend Functions & Classes
Static data members & functions
Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Destructor on the other hand is used to destroy the class object.
This Powerpoint presentation covers following topics of C Plus Plus:
Features of OOP
Classes in C++
Objects & Creating the Objects
Constructors & Destructors
Friend Functions & Classes
Static data members & functions
Constructors, Destructors, call in parameterized Constructor, Multiple constructor in a class, Explicit/implicit call, Copy constructor, Dynamic Constructors and call in parameterized Constructor
Constructors, Destructors, call in parameterized Constructor, Multiple constructor in a class, Explicit/implicit call, Copy constructor, Dynamic Constructors and call in parameterized Constructor
Constructor is a special member method which will be called automatically when you create an object of any class.
The main purpose of using constructor is to initialize an object.
http://www.tutorial4us.com/java/java-constructor
Slide 2:
What are the Constructor & destructor ?
Slide 3:
Characteristics of Constructor
Slide 4:
Special CHaracteristics of Destructor
Slide 5:
Similarities
Slide 6:
Dissimilarities
Slides 7:
Default Constructor with example
Slide 8:
Parameterized Constructor
Slide 9:
Copy Constructor with example
Slide 10:
Destructor
Slide 11:
Bibliography
This is an intermediate conversion course for C++, suitable for second year computing students who may have learned Java or another language in first year.
An overview of object oriented programming including the differences between OOP and the traditional structural approach, definitions of class and objects, and an easy coding example in C++. This presentation includes visual aids to make the concepts easier to understand.
Operation “Blue Star” is the only event in the history of Independent India where the state went into war with its own people. Even after about 40 years it is not clear if it was culmination of states anger over people of the region, a political game of power or start of dictatorial chapter in the democratic setup.
The people of Punjab felt alienated from main stream due to denial of their just demands during a long democratic struggle since independence. As it happen all over the word, it led to militant struggle with great loss of lives of military, police and civilian personnel. Killing of Indira Gandhi and massacre of innocent Sikhs in Delhi and other India cities was also associated with this movement.
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
Andreas Schleicher presents at the OECD webinar ‘Digital devices in schools: detrimental distraction or secret to success?’ on 27 May 2024. The presentation was based on findings from PISA 2022 results and the webinar helped launch the PISA in Focus ‘Managing screen time: How to protect and equip students against distraction’ https://www.oecd-ilibrary.org/education/managing-screen-time_7c225af4-en and the OECD Education Policy Perspective ‘Students, digital devices and success’ can be found here - https://oe.cd/il/5yV
We all have good and bad thoughts from time to time and situation to situation. We are bombarded daily with spiraling thoughts(both negative and positive) creating all-consuming feel , making us difficult to manage with associated suffering. Good thoughts are like our Mob Signal (Positive thought) amidst noise(negative thought) in the atmosphere. Negative thoughts like noise outweigh positive thoughts. These thoughts often create unwanted confusion, trouble, stress and frustration in our mind as well as chaos in our physical world. Negative thoughts are also known as “distorted thinking”.
Instructions for Submissions thorugh G- Classroom.pptxJheel Barad
This presentation provides a briefing on how to upload submissions and documents in Google Classroom. It was prepared as part of an orientation for new Sainik School in-service teacher trainees. As a training officer, my goal is to ensure that you are comfortable and proficient with this essential tool for managing assignments and fostering student engagement.
The Indian economy is classified into different sectors to simplify the analysis and understanding of economic activities. For Class 10, it's essential to grasp the sectors of the Indian economy, understand their characteristics, and recognize their importance. This guide will provide detailed notes on the Sectors of the Indian Economy Class 10, using specific long-tail keywords to enhance comprehension.
For more information, visit-www.vavaclasses.com
Ethnobotany and Ethnopharmacology:
Ethnobotany in herbal drug evaluation,
Impact of Ethnobotany in traditional medicine,
New development in herbals,
Bio-prospecting tools for drug discovery,
Role of Ethnopharmacology in drug evaluation,
Reverse Pharmacology.
Unit 8 - Information and Communication Technology (Paper I).pdfThiyagu K
This slides describes the basic concepts of ICT, basics of Email, Emerging Technology and Digital Initiatives in Education. This presentations aligns with the UGC Paper I syllabus.
How to Make a Field invisible in Odoo 17Celine George
It is possible to hide or invisible some fields in odoo. Commonly using “invisible” attribute in the field definition to invisible the fields. This slide will show how to make a field invisible in odoo 17.
3. Constructor
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• add a ;
• Creates the object ‘a’ of types
add and initializes its data
members m and n to zero.
• There is no need to write any
statement to invoke the
constructor function.
• A constructor that accepts
no parameter is called as a
default constructor .
class add
{
int m, n ;
public :
add (void) ;
‐‐‐‐‐‐
};
add :: add (void)
{
m = 0; n = 0;
}
4. Constructor
Characteristics of Constructor:
• They should be declared in the public section.
• They are invoked automatically when the objects
are created.
• They do not have return types, not even void and
they cannot return values.
• They cannot be inherited, though a derived class
can call the base class constructor.
• Like other C++ functions, Constructors can have
default arguments.
• Constructors can not be virtual.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
6. Default Constructor
• Default constructor is
the constructor which
doesn't take any
argument.
• It has no parameter.
• It is also called as zero‐
argument constructor.
#include<iostream>
using namespace std;
class Cube
{
public:
int side;
Cube()
{
side=10;
}
};
int main()
{
Cube c;
cout << c.side;
return 0;
}
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
8. Parameterized Constructor
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
class area
{
int length,breadth;
public:
area(int l,int b)//parameterized constructor.
{
length = l;
breadth = b;
}
void display()
{
cout << "Length of rectangle is:" << length << endl;
cout << "Breadth of rectangle is: " << breadth << endl;
cout << "Area of rectangle is: " << length*breadth;
}
};
9. Parameterized Constructor
• When a constructor has been parameterized,
• area a; // may not work.
• We must pass initial value as arguments to the
constructor function when an object is declared.
This can be done by two ways:
– By calling the constructor explicitly.
– By calling the constructor implicitly.
• The following declaration illustrates above method:
area a = area (5, 6); // explicit call
area a (5, 6); // implicit call
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
11. Constructors with default arguments:
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
#include<iostream>
#include<cmath>
using namespace std;
class power
{
int num;
int power;
int ans;
public :
power (int n = 9, int p = 3); // declaration of constructor
//with default arguments
void show ()
{
cout <<"n"<<num <<" raise to "<<power <<" is " <<ans;
}
};
power :: power (int n,int p )
{
num = n;
power = p;
ans = pow (n, p);
}
int main( )
{
power p1, p2(5);
p1.show ();
p2.show ();
return 0;
}
14. Copy Constructor
• Referencing operator (&) is used to define
referencing variable.
• Ref. variable prepares an alternative (alias) name
for previously defined variable.
• For Example:
int qty = 10; // declared and initialized.
int & qt = qty; // alias variable.
• Any change made in one of the variable causes
change in both the variable.
qt = qt*2; // contents of qt and qty will be 20.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
15. Copy Constructor
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
// Class definition
class TimeSlt
{
int Da, Mo, Ye;
public:
void Disply();
TimeSlt(){}
TimeSlt(int D1,int M1,int Y1); // Constructor with parameters
TimeSlt(TimeSlt &); // Copy constructor
};
void TimeSlt::Display()
{
cout<<Da<<" "<<Mo<<" "<<Ye<<endl;
}
TimeSlt::TimeSlt(int D1,int M1,int Y1)
{
Da = D1;
Mo = M1;
Ye = Y1;
}
TimeSlt::TimeSlt(TimeSlt &tmp)
{
cout<<"Data copied"<<endl;
Da = tmp.Da;
Mo = tmp.Mo;
Ye = tmp.Ye;
}
int main()
{
TimeSlt T1(13,8,1990);
T1.Display();
TimeSlt T2(T1);
T2.Dispaly();
TimeSlt T3 = T1;
T3.Dispaly();
TimeSlt T4;
T4 = T1;
T4.Dispaly();
return 0;
}
16. Copy Constructor
TimeSlt T2 (T1);
• Object T2 is created with one object(T1), the copy
constructor is invoked and data members are initialized.
TimeSlt T3 = T1;
• Object T3 is created with assignment with object T1; copy
constructor invoked, compilers copies all the members of
object T1 to destination object T3 .
TimeSlt T4;
T4 = T1;
• Copy constructor is not executed, member‐to‐member of
T1 are copied to object T4 , assignment statement assigns
the values.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
17. Overloading Constructor
• Like functions, it is also possible to overload constructors.
• In previous examples, we declared single constructors
without arguments and with all arguments.
• A class can contain more than one constructor. This is
known as constructor overloading.
• All constructors are defined with the same name as the
class.
• All the constructors contain different number of arguments.
• Depending upon number of arguments, the compiler
executes appropriate constructor.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
18. Overloading Constructor
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
#include<iostream>
using namespace std;
class perimeter
{
int l, b, p;
public:
perimeter ()
{
cout << "n Enter the value of l and b";
cin >> l >> b;
}
perimeter (int a)
{
l = b = a;
}
perimeter (int l1, int b1)
{
l = l1;
b = b1;
}
perimeter (perimeter &peri)
{
l = peri.l;
b = peri.b;
}
void calculate (void);
};
19. Overloading Constructor
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
void perimeter :: calculate (void)
{
p = 2* (l + b)
cout << p;
}
int main ()
{
perimeter obj, obj1 (2), obj2 (2, 3);
cout<<"n perimeter of rectangle is ";
obj. Calculate ();
cout<<"n perimeter of square is ";
obj1.calculate ();
cout<<"n perimeter of rectangle is ";
obj2.calculate ();
cout<<"n perimeter of rectangle is ";
perimeter obj3 (obj2);
obj3.calculate ();
return 0;
}
20. Dynamic Constructor
• The constructors can also be used to allocate
memory while creating objects.
• This will enable the system to allocate the right
amount of memory for each object when the
objects are not of the same size.
• Allocation of memory to objects at the time of
their construction is known as dynamic
construction of objects.
• The memory is created with the help of the new
operator.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
21. Dynamic Constructor
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
#include <iostream>
#include <string>
using namespace std;
class str
{
char *name;
int len;
public:
str()
{
len = 0;
name = new char[len + 1];
}
str(char *s)
{
len = strlen(s);
name = new char[len + 1];
strcpy(name,s);
}
void show()
{
cout<<"NAME IS:->"<<name<<endl;
}
void join(str a, str b);
};
22. Dynamic Constructor
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
void str :: join(str a, str b)
{
len = a.len + b.len;
name = new char[len + 1];
strcpy(name, a.name);
strcat(name, b.name);
}
int main()
{
char *first="OOPS";
str n1(first),n2("WITH"),n3("C++"),n4,n5;
n4.join(n1,n2);
n5.join(n4,n3);
n1.show();
n2.show();
n3.show();
n4.show();
n5.show();
return 0;
}
23. Destructor
• Destructor is also a ‘special’ member
function like constructor.
• Destructors destroy the class objects created
by constructors.
• The destructors have the same name as their
class, preceded by a tilde (~).
• It is not possible to define more than one
destructor.
• For example : ~Circle();
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
24. Destructor
• The destructor is only one way to destroy the
object.
• They cannot be overloaded.
• A destructor neither requires any argument
nor returns any value.
• It is automatically called when object goes
out of scope.
• Destructor releases memory space occupied
by the objects.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
25. Destructor
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
#include<iostream>
using namespace std;
class Circle //specify a class
{
private :
double radius; //class data members
public:
Circle() //default constructor
{
radius = 0;
}
Circle(double r) //parameterized constructor
{
radius = r;
}
Circle(Circle &t) //copy constructor
{
radius = t.radius;
}
void setRadius(double r); //function to set data
double getArea();
~Circle() //destructor
{}
};
26. Destructor
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
void Circle :: setRadius(double r) //function to set data
{
radius = r;
}
double Circle :: getArea()
{
return 3.14 * radius * radius;
}
int main()
{
Circle c1; //defalut constructor invoked
Circle c2(2.5); //parmeterized constructor invoked
Circle c3(c2); //copy constructor invoked
cout << c1.getArea()<<endl;
cout << c2.getArea()<<endl;
cout << c3.getArea()<<endl;
c1.setRadius (1.2);
cout << c1.getArea()<<endl;
return 0;
}
27. Const Member function
• The member functions of class can be declared as
constant using const keyword.
• The constant function cannot modify any data in
the class.
• The const keyword is suffixed to the function
declaration as well as in function definition.
• If these functions attempt to change the data,
compiler will generate an error message.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
28. Const Member function
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
#include <iostream>
using namespace std;
class ABC
{
int c;
public:
void show(int a, int b) const
{
c = a + b;
cout << " a + b ::" <<c;
}
};
int main()
{
const ABC x;
x.show (5, 7);
return 0;
}
29. Const Member function
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
#include <iostream>
using namespace std;
class ABC
{
int c;
public:
void show(int a, int b) const
{
//c = a + b;
cout << " a + b ::" <<a + b;
}
};
int main()
{
const ABC x;
x.show (5, 7);
return 0;
}
30. Const Objects
• Like constant member function, we can make the object
constant by the keyword const.
• Only constructor can initialize data member variables of
constant objects.
• The data member of constant objects can only be read and
any effect to alter values of variables will generate an error.
• The data member of constant objects are also called as a
read‐only data member.
• The const‐objects can access only const‐functions.
• If constant objects tries to invoke a non‐member function,
an error message will be displayed.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
31. Const Objects
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
#include <iostream>
using namespace std;
class ABC
{
int a;
public:
ABC(int m)
{
a = m;
}
void show() const
{
cout << "a = " << a <<endl;
}
};