Anda di halaman 1dari 20

12/17/2018 NOTES FOR C++ LANGUAGE PART 4.

docx - Google Docs

 NOTES FOR C++ LANGUAGE PART 4

Back to notes sec on :

How to use constructor to ini alize data member.

let us see an example without constructor then using constructor.

#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

explana on : to have an in al value to data member d of object ‘obj’ we have

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()

demo obj(10),rbj(30); // here constructor is automa cally called

// 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();

output will be:

data=10

data=30

explana on : to have an in al value to data member d of object ‘obj’ we have

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.

What do you mean by parameterized constructor? what happens if we have

parameterized constructor only and try to declare an object such as class-name

obj?

Constructor defini on having parameters in its’ defini on is known as

parameterized constructor.

a special type of constructor which does not contain any argument is created by

compiler itself known as default constructor un l we don’t have our user

defined constructor. default constructor supports declara on of object such as:

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

so defining parameterized constructor only will prevent ourselves to write

statement

class-name obj-name.;

therefore we must defined parameterized as well as non-parameterized constructor

to support class-name obj-name as well as class-name obj(parameter).

if out programming requirement is to declare parameterized object we can omit

defini on of constructor with no argument(default constructor).

#include <iostream.h>

#include <conio.h>

class demo

private:

int d;

public:

demo() // no argument constructor or default constructor will support

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

//specifica on this will support class-name obj-name(value)

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()

demo obj(10),rbj(30),cbj; // here constructor is automa cally called

// note that demo cbj; will not generate compiler error because we have defined

our

// own default constructor

obj.showdata();

rbh.showdata();

cbj.showdata();

getch();

output will be:

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:

demo() // no argument constructor or default constructor will support

//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

cout<<”\n enter a value”:

cin>>d;

demo(int n) // constructor defini on, note name is same as class name and no

return

//specifica on this will support class-name obj-name(value)

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

// default constructor which will ask the user to enter a data

obj.showdata();

rbj.showdata();

cbj.showdata(); // will print the value entered by user

getch();

output will be:

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

data=value entered by user

Write program to demonstrate use of constructor to allocate memory for dynamic

data member./ constructor with default argument value.

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

a=new int [n];

size=n;

void getdata();

void putdata();

void array::getdata()

int m,n;

cout<<”\n enter “<<size <<” values;

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:

array(int m=3,int n=3);

void getdata();

void putdata();

array::array(int m,int n)

int p,q;

a=new int *[m]; // allocate memory for first element of rows

for(p=0;p<m;p++)// allocate memory for all elements of each row

a[p]=new int [n];

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;

cout<<”\n enter “<<size <<” values;

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

allocated for an object is released.

Write characteris cs of constructor and destructor.

characteris cs of constructor:

*. constructor is the first member func on, which is automa cally called at

the me of crea on of each every object of that class.

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

Anda mungkin juga menyukai