SlideShare a Scribd company logo
1 of 20
Constructor, destructor, constant
object, member initialization
Destructor Function
• This member function is called when you
are done with the object.
• It eliminates any dynamically allocated
variables created by the object so that the
memory space allocated becomes free.
• It has no parameters and no return type.
• No overloading is allowed.
Syntax
#include <iostream>
using namespace std;
class Employee{
int empNo;
public:
int getNo(){return empNo;}
void setNo(int number){
empNo=number;
}
Employee(){
cout << "constructor is called"<<endl;
empNo = 0;}
/* This is called automatically when the object is no more needed e.g. when
main() is finished*/
~Employee() {cout << "destructor is called"<<endl;} // Keep screen on to
see this text
};
constructor is called
4
Press any key to continue . . .
destructor is called
#include "employee.h"
main(){
Employee myEmp;
myEmp.setNo(4);
cout<<myEmp.getNo()<<endl;
system("pause");
}
Syntax
#include <iostream>
using namespace std;
class Employee{
int empNo;
public:
int getNo(){return empNo;}
void setNo(int number){
empNo=number;
}
Employee(){
cout << "constructor is called"<<endl;
empNo = 0;}
~Employee();
};
Employee::~Employee() {
cout << "destructor is called"<<endl;
}
•You can write the destructor
outside, as well.
•The compiler should provide
you default constructor and
destructor in case you do not
write them.
Array as data member
• A class may have arrays as data members
#include <iostream>
using namespace std;
class person{
int f[10];
public:
person (int=0);
void printF3(){cout<<f[2]<<endl;
}
};
person :: person(int n){
for (int i=0;i<10;i++)
f[i]=n;
}
main(){
person a(5);
person b;
a.printF3();
b.printF3();
system("pause");
}
What is the output?
Memberwise Initialization
• When a class object is assigned to another
object of the same class, all members are
automatically assigned.
class Example{
private:
int x;
int y;
public:
Example (int a, int b){x=a; y=b;}
};
main(){
Example ex1(3,5);
Example ex2=ex1; // x and y of ex2 are now 3 and 5, respectively.
}
Memberwise Initialization
• Memberwise initialization occurs when:
– Initializing a class object with another
– Passing of a class object as a parameter to a
function e.g. void myFunction1(Example s)
– Returning a class object from a function e.g.
Example myFunction2(int) {
return ex(int);
}
Example
main(){
Example ex1(3,5);
AnotherExample a1;
ex1=a1.myFunction(10);
cout<<ex1.getX()<<endl;
system("pause");
}
#include <iostream>
using namespace std;
class Example{
private:
int x;
int y;
public:
Example (int a, int b){x=a; y=b;}
int getX(){
return x;
}
};
class AnotherExample{
public:
Example myFunction(int a){
return Example(a,a);
}
};
What is the output?
Careful! Two classes are defined:
more to come in “composition”.
Constant Objects
• When an object is created as a constant
object, its data members cannot be
changed.
• Only constant functions can be called for
constant objects.
• Constant Member Function
– A function, which can be called for constant
objects but still cannot change the value of its
data members.
Example
main(){
const Example ex2(20,30);
ex2.setX(100); // ERROR
cout<<ex2.getX()<<endl; // ERROR
cout<<ex2.getMeX()<<endl;
system("pause");
}
#include <iostream>
using namespace std;
class Example{
private:
int x;
int y;
public:
Example (int a, int b){x=a; y=b;}
int getX(){
return x;
}
void setX(int myX){
x=myX;
}
int getMeX() const{
x=x+1; // ERROR
return x;
}
};
Example
#include <iostream>
using namespace std;
class myIntegerClass{
private:
int number;
public:
void store(int) ;
int get() ;
int incrementNumber();
myIntegerClass(int n){
number=n;}
};
int myIntegerClass::incrementNumber(){
return number++;
}
void myIntegerClass::store(int n){
number=n;
}
int myIntegerClass::get() {
return number;
}
main(){
myIntegerClass in(10);
in.store(20);
cout<<in.get()<<endl;
cout<<in.incrementNumber()<<endl;
cout<<in.get()<<endl;
system("pause");
}
What is the output?
Example
#include <iostream>
using namespace std;
class myIntegerClass{
private:
int number;
public:
void store(int) const;
int get() ;
int incrementNumber();
myIntegerClass(int n){
number=n;}
};
int myIntegerClass::incrementNumber(){
return number++;
}
void myIntegerClass::store(int n) const{
number=n;
}
int myIntegerClass::get() {
return number;
}
main(){
const myIntegerClass in(10);
in.store(20);
cout<<in.get()<<endl;
cout<<in.incrementNumber()<<endl;
cout<<in.get()<<endl;
system("pause");
}
What is the output?
Member Initialization
• This is to initialize members without using
assignment.
• For constant, reference and object class
members, you need to initialize instead of
assigning.
#include <iostream>
using namespace std;
class Simple{
private:
int i;
float f;
public:
Simple(int, float);
int getFirst(){return i;}
};
Simple::Simple(int x, float y){
i=x;
f=y;
}
/* OR */
Simple::Simple(int x, float y): i(x), f(y){}
main(){
Simple s(2, 2.3);
cout<<s.getFirst()<<endl;
system("pause");
}
Example
Example
#include <iostream>
using namespace std;
class ConR{
public:
ConR(int);
private:
int i;
const int ci;
};
ConR::ConR(int ii){
i=ii;
ci=ii; // Constant ci cannot make assignment
}
main(){
ConR myC(3);
system("pause");
}
#include <iostream>
using namespace std;
class ConR{
public:
ConR(int);
private:
int i;
const int ci;
};
/* ci is in a member initialization
list.*/
ConR::ConR(int ii): ci(ii){
i=ii;
}
Example
#include<iostream>
using namespace std;
class Date{
private:
int day, month, year;
public:
Date(int d, int m, int y):day(d), month(m), year(y){}
void display(){
cout<<day<<"/"<<month<<"/"<<year<<endl;
}
};
main(){
Date today(10,3,2010);
today.display();
system("pause");
}
Class objects as members of
classes (composition)
• An object of a class can be a member of
another class.
• Recall “Example” and “AnotherExample”
classes in section Memberwise
Initialization.
Date and Student classes
#include<iostream>
using namespace std;
class Date{
private:
int day, month, year;
public:
Date(int d, int m, int y):day(d), month(m), year(y){}
void display(){cout<<day<<"/"<<month<<"/"<<year<<endl;}
};
class Student{
private:
Date birthdate;
int stId;
public:
Student(int, int, int, int);
void print(){cout<<"Student No: "<<stId<<endl<<"Birthdate: "; birthdate.display();}
};
Student::Student(int id, int a, int b, int c):stId(id), birthdate(a,b,c){}
main(){
Student ali(505, 3,7,1980);
ali.print();
system("pause");
}
Nested Classes
• Recall that you can create nested loops and if
statements; likewise, you can define classes within other
classes.
• By this way, you can embed a class definition inside
another class and you cover both definitions in one
include.
For example:
/************Already defined in StudentDateNested.h
#include "date.h"
#include "student.h"
*****************************************************/
#include "StudentDateNested.h"
Example
/*StudentDateNested.h*/
#include<iostream>
using namespace std;
class Student{
private:
class Date{
private:
int day, month, year;
public:
Date(int d, int m, int y):day(d), month(m), year(y){}
void display(){cout<<day<<"/"<<month<<"/"<<year<<endl;}
};
Date birthdate;
int stId;
public:
Student(int, int, int, int);
void print(){cout<<"Student No: "<<stId<<endl<<"Birthdate: "; birthdate.display();}
};
Student::Student(int id, int a, int b, int c):stId(id), birthdate(a,b,c){}
#include "StudentDateNested.h"
main(){
Student ali(505, 3,7,1980);
ali.print();
system("pause");
}

More Related Content

Similar to w10 (1).ppt

File & Exception Handling in C++.pptx
File & Exception Handling in C++.pptxFile & Exception Handling in C++.pptx
File & Exception Handling in C++.pptxRutujaTandalwade
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructorsAkshaya Parida
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Abdul Samee
 
6-Exception Handling and Templates.pptx
6-Exception Handling and Templates.pptx6-Exception Handling and Templates.pptx
6-Exception Handling and Templates.pptxSatyamMishra237306
 
#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And ReferencesHadziq Fabroyir
 
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
JAVA - Design a data structure IntSet that can hold a set of integers-.docxJAVA - Design a data structure IntSet that can hold a set of integers-.docx
JAVA - Design a data structure IntSet that can hold a set of integers-.docxolsenlinnea427
 
For each task, submit your source java code file.(1) Objective Im.pdf
For each task, submit your source java code file.(1) Objective Im.pdfFor each task, submit your source java code file.(1) Objective Im.pdf
For each task, submit your source java code file.(1) Objective Im.pdfdhavalbl38
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++Jay Patel
 

Similar to w10 (1).ppt (20)

File & Exception Handling in C++.pptx
File & Exception Handling in C++.pptxFile & Exception Handling in C++.pptx
File & Exception Handling in C++.pptx
 
Class ‘increment’
Class ‘increment’Class ‘increment’
Class ‘increment’
 
C++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
 
Function C++
Function C++ Function C++
Function C++
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructors
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01
 
Thread
ThreadThread
Thread
 
Function
FunctionFunction
Function
 
6-Exception Handling and Templates.pptx
6-Exception Handling and Templates.pptx6-Exception Handling and Templates.pptx
6-Exception Handling and Templates.pptx
 
Ch7 C++
Ch7 C++Ch7 C++
Ch7 C++
 
srgoc
srgocsrgoc
srgoc
 
#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References#OOP_D_ITS - 3rd - Pointer And References
#OOP_D_ITS - 3rd - Pointer And References
 
Introduction to c ++ part -2
Introduction to c ++   part -2Introduction to c ++   part -2
Introduction to c ++ part -2
 
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
JAVA - Design a data structure IntSet that can hold a set of integers-.docxJAVA - Design a data structure IntSet that can hold a set of integers-.docx
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
 
For each task, submit your source java code file.(1) Objective Im.pdf
For each task, submit your source java code file.(1) Objective Im.pdfFor each task, submit your source java code file.(1) Objective Im.pdf
For each task, submit your source java code file.(1) Objective Im.pdf
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
functions of C++
functions of C++functions of C++
functions of C++
 

Recently uploaded

(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 

Recently uploaded (20)

(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 

w10 (1).ppt

  • 2. Destructor Function • This member function is called when you are done with the object. • It eliminates any dynamically allocated variables created by the object so that the memory space allocated becomes free. • It has no parameters and no return type. • No overloading is allowed.
  • 3. Syntax #include <iostream> using namespace std; class Employee{ int empNo; public: int getNo(){return empNo;} void setNo(int number){ empNo=number; } Employee(){ cout << "constructor is called"<<endl; empNo = 0;} /* This is called automatically when the object is no more needed e.g. when main() is finished*/ ~Employee() {cout << "destructor is called"<<endl;} // Keep screen on to see this text }; constructor is called 4 Press any key to continue . . . destructor is called #include "employee.h" main(){ Employee myEmp; myEmp.setNo(4); cout<<myEmp.getNo()<<endl; system("pause"); }
  • 4. Syntax #include <iostream> using namespace std; class Employee{ int empNo; public: int getNo(){return empNo;} void setNo(int number){ empNo=number; } Employee(){ cout << "constructor is called"<<endl; empNo = 0;} ~Employee(); }; Employee::~Employee() { cout << "destructor is called"<<endl; } •You can write the destructor outside, as well. •The compiler should provide you default constructor and destructor in case you do not write them.
  • 5. Array as data member • A class may have arrays as data members #include <iostream> using namespace std; class person{ int f[10]; public: person (int=0); void printF3(){cout<<f[2]<<endl; } }; person :: person(int n){ for (int i=0;i<10;i++) f[i]=n; } main(){ person a(5); person b; a.printF3(); b.printF3(); system("pause"); } What is the output?
  • 6. Memberwise Initialization • When a class object is assigned to another object of the same class, all members are automatically assigned. class Example{ private: int x; int y; public: Example (int a, int b){x=a; y=b;} }; main(){ Example ex1(3,5); Example ex2=ex1; // x and y of ex2 are now 3 and 5, respectively. }
  • 7. Memberwise Initialization • Memberwise initialization occurs when: – Initializing a class object with another – Passing of a class object as a parameter to a function e.g. void myFunction1(Example s) – Returning a class object from a function e.g. Example myFunction2(int) { return ex(int); }
  • 8. Example main(){ Example ex1(3,5); AnotherExample a1; ex1=a1.myFunction(10); cout<<ex1.getX()<<endl; system("pause"); } #include <iostream> using namespace std; class Example{ private: int x; int y; public: Example (int a, int b){x=a; y=b;} int getX(){ return x; } }; class AnotherExample{ public: Example myFunction(int a){ return Example(a,a); } }; What is the output? Careful! Two classes are defined: more to come in “composition”.
  • 9. Constant Objects • When an object is created as a constant object, its data members cannot be changed. • Only constant functions can be called for constant objects. • Constant Member Function – A function, which can be called for constant objects but still cannot change the value of its data members.
  • 10. Example main(){ const Example ex2(20,30); ex2.setX(100); // ERROR cout<<ex2.getX()<<endl; // ERROR cout<<ex2.getMeX()<<endl; system("pause"); } #include <iostream> using namespace std; class Example{ private: int x; int y; public: Example (int a, int b){x=a; y=b;} int getX(){ return x; } void setX(int myX){ x=myX; } int getMeX() const{ x=x+1; // ERROR return x; } };
  • 11. Example #include <iostream> using namespace std; class myIntegerClass{ private: int number; public: void store(int) ; int get() ; int incrementNumber(); myIntegerClass(int n){ number=n;} }; int myIntegerClass::incrementNumber(){ return number++; } void myIntegerClass::store(int n){ number=n; } int myIntegerClass::get() { return number; } main(){ myIntegerClass in(10); in.store(20); cout<<in.get()<<endl; cout<<in.incrementNumber()<<endl; cout<<in.get()<<endl; system("pause"); } What is the output?
  • 12. Example #include <iostream> using namespace std; class myIntegerClass{ private: int number; public: void store(int) const; int get() ; int incrementNumber(); myIntegerClass(int n){ number=n;} }; int myIntegerClass::incrementNumber(){ return number++; } void myIntegerClass::store(int n) const{ number=n; } int myIntegerClass::get() { return number; } main(){ const myIntegerClass in(10); in.store(20); cout<<in.get()<<endl; cout<<in.incrementNumber()<<endl; cout<<in.get()<<endl; system("pause"); } What is the output?
  • 13. Member Initialization • This is to initialize members without using assignment. • For constant, reference and object class members, you need to initialize instead of assigning.
  • 14. #include <iostream> using namespace std; class Simple{ private: int i; float f; public: Simple(int, float); int getFirst(){return i;} }; Simple::Simple(int x, float y){ i=x; f=y; } /* OR */ Simple::Simple(int x, float y): i(x), f(y){} main(){ Simple s(2, 2.3); cout<<s.getFirst()<<endl; system("pause"); } Example
  • 15. Example #include <iostream> using namespace std; class ConR{ public: ConR(int); private: int i; const int ci; }; ConR::ConR(int ii){ i=ii; ci=ii; // Constant ci cannot make assignment } main(){ ConR myC(3); system("pause"); } #include <iostream> using namespace std; class ConR{ public: ConR(int); private: int i; const int ci; }; /* ci is in a member initialization list.*/ ConR::ConR(int ii): ci(ii){ i=ii; }
  • 16. Example #include<iostream> using namespace std; class Date{ private: int day, month, year; public: Date(int d, int m, int y):day(d), month(m), year(y){} void display(){ cout<<day<<"/"<<month<<"/"<<year<<endl; } }; main(){ Date today(10,3,2010); today.display(); system("pause"); }
  • 17. Class objects as members of classes (composition) • An object of a class can be a member of another class. • Recall “Example” and “AnotherExample” classes in section Memberwise Initialization.
  • 18. Date and Student classes #include<iostream> using namespace std; class Date{ private: int day, month, year; public: Date(int d, int m, int y):day(d), month(m), year(y){} void display(){cout<<day<<"/"<<month<<"/"<<year<<endl;} }; class Student{ private: Date birthdate; int stId; public: Student(int, int, int, int); void print(){cout<<"Student No: "<<stId<<endl<<"Birthdate: "; birthdate.display();} }; Student::Student(int id, int a, int b, int c):stId(id), birthdate(a,b,c){} main(){ Student ali(505, 3,7,1980); ali.print(); system("pause"); }
  • 19. Nested Classes • Recall that you can create nested loops and if statements; likewise, you can define classes within other classes. • By this way, you can embed a class definition inside another class and you cover both definitions in one include. For example: /************Already defined in StudentDateNested.h #include "date.h" #include "student.h" *****************************************************/ #include "StudentDateNested.h"
  • 20. Example /*StudentDateNested.h*/ #include<iostream> using namespace std; class Student{ private: class Date{ private: int day, month, year; public: Date(int d, int m, int y):day(d), month(m), year(y){} void display(){cout<<day<<"/"<<month<<"/"<<year<<endl;} }; Date birthdate; int stId; public: Student(int, int, int, int); void print(){cout<<"Student No: "<<stId<<endl<<"Birthdate: "; birthdate.display();} }; Student::Student(int id, int a, int b, int c):stId(id), birthdate(a,b,c){} #include "StudentDateNested.h" main(){ Student ali(505, 3,7,1980); ali.print(); system("pause"); }