#include <iostream.h>
#include <conio.h>
class demo
private:
int d;
public:
void setdata(int n)
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 1/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
d=n;
void showdata()
cout<<”\n data=”<<d;
};
void main()
demo obj;
obj.setdata(10);
obj.showdata();
getch();
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 2/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
to explicitly call the member func on ‘setdata ‘ which will assign value 10 to
‘d’. member func on showdata will print value of data member ‘d’ as 10.
#include <iostream.h>
#include <conio.h>
class demo
private:
int d;
public:
demo(int n) // constructor defini on, note name is same as class name and no
return //specifica on
d=n;
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 3/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
void showdata()
cout<<”\n data=”<<d;
};
void main()
// note that demo qbj; will generate compiler error because compiler has stopped
crea ng
// default constructor (constructor with no argument) and we don’t have our own
// default constructor
obj.showdata();
rbh.showdata();
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 4/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
getch();
data=10
data=30
to explicitly call the member func on ‘setdata ‘ which will assign value 10 to
‘d’. member func on showdata will print value of data member ‘d’ as 10.
obj?
parameterized constructor.
a special type of constructor which does not contain any argument is created by
class-name obj-name;
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 5/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
statement
class-name obj-name.;
#include <iostream.h>
#include <conio.h>
class demo
private:
int d;
public:
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 6/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
//class-name obj-name;
d=0;
demo(int n) // constructor defini on, note name is same as class name and no
return
d=n;
void showdata()
cout<<”\n data=”<<d;
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 7/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
};
void main()
// note that demo cbj; will not generate compiler error because we have defined
our
obj.showdata();
rbh.showdata();
cbj.showdata();
getch();
data=10
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 8/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
data=30
data=0
Write a program using constructor to automa cally start taking user input.
#include <iostream.h>
#include <conio.h>
class demo
private:
int d;
public:
//class-name obj-name;
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 9/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
cin>>d;
demo(int n) // constructor defini on, note name is same as class name and no
return
d=n;
void showdata()
cout<<”\n data=”<<d;
};
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 10/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
void main()
demo obj(10),rbj(30),cbj;
// constructor with one argument is called automa call by obj and rbj and their
data
//members are ini alized with 10 and 30. cbj generates automa cally call to
user defined
obj.showdata();
rbj.showdata();
getch();
data=10
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 11/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
data=30
example:
#include<iosteream.h>
#include<conio.h>
class array
private:
int *a;
int size;
public:
array(int n=5)
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 12/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
size=n;
void getdata();
void putdata();
void array::getdata()
int m,n;
for(m=0;m<size;m++)
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 13/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
cin>>a[m];
void array::putdata()
int m,n;
cout<<”\nvalues are “;
for(m=0;m<size;m++)
cout<<a[m];
void main()
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 14/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
array c1(4),c2;
c1.getdata();
c2.getdata();
c1.putdata();
c2.putdata();
getch();
Class and double dimension dynamic array using constructor for memory alloca on.
example:
#include<iosteream.h>
#include<conio.h>
class array
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 15/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
private:
int **a;
int rsize,csize;
public:
void getdata();
void putdata();
array::array(int m,int n)
int p,q;
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 16/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
rsize=m;
csize=n;
void array::getdata()
int m,n;
for(m=0;m<rsize;m++)
for(n=0;n<csize;n++)
cin>>a[m][n];
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 17/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
void array::putdata()
int m,n;
cout<<”\nvalues are\n “;
for(m=0;m<rsize;m++)
for(n=0;n<csize;n++)
cout<<a[m][n];
void main()
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 18/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
array c1(2,2),c2;
c1.getdata();
c2.getdata();
c1.putdata();
c2.putdata();
getch();
What is destructor?
destructor is a member func on defined in public area with same name as class
name but preceded by ~( lde). destructor is called automa cally when memory
characteris cs of constructor:
*. constructor is the first member func on, which is automa cally called at
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 19/20
12/17/2018 NOTES FOR C++ LANGUAGE PART 4.docx - Google Docs
https://docs.google.com/document/d/118jaIRnn05cRA7Gl-yXXoC1cB0GolRHPEPTmIz2i6XY/edit 20/20