• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Constant Member Function in C++

What is difference between constructor and destructor

 

ConstructorDestructor
it is executed on creation of objectits job is inita class contain multiple constructorsit can have parametersit is executed before deleting objectits job is deinita class contain only one destructorit cannot have parameters.

 

Constant members in C++

Member of class can be declared as constants

Constant members are of  2 types.

Constant data member

Constant member function

 

Constant data member

The value of constant data member has never changed.

Constant data member must be initialized using constructor list

It is used to defined read only property of object

 

Constant Member Function Syntax

const data type   datamember-name

syntax of init constant data members using construct list

constructor-name(): constant-datamember(value,constant-datamember(value)—-

{

statements;

}

//wap to find area of circle

Example

# include <iostream.h>

class circle

{

float r;

const float pi;

public :

circle() : pi(3.147)

{

r=1.5;

}

void find_area()

{

float area=pi*r*r;

cout<<”\n Area is”<<area;

}};

void main()

{

circle circle1;              // create a object by calling constructor

circle1.find_area();

}

 

 

Constant member function Example

//Finding simple interest :

#inlude<iostream.h>

#include<conio.h>

class simple_interest

{

float amt;

int time;

const float rate;

public :

simple_interest(float a, int l, float r): rate(r)

{

amt=a;

time=t;

}

float find_interest()

{

float si=(amt * rate *time)/100;

cout<<”\n simple interest”<<si;

}

}

void main()

{

simple_interest_cust1(5000,12,1.5);

simple_interest_cust2(7000,24,2.5);

cust1.find_intrest ();

cust2.find_intrest ();

}

 

 

Constant member function in C++

Constant member function is an accessory function which cannot modifying values of data members.

It perform constant operation.

 

syntax :

<return type> function-name(<parameters>)

constant

{

statements;

}

 

Constant member function Example

# incude<iostream.h>

class account

{

int accno;

char name[10];

char name[10];                 //   private data

float balance;

public :

void read(); //modifier(it modifying)

void print_details() const;

};

void accout:: read()

{

cout<<”\n input account no”;

cin>>accno;

cout<<”\n input customer name”;

cin>>name;

cout<<”\n input balance”;

cin>>balance;

}

void account :: deposit(float tamt)

{

balance=balance+tamt;

}

void accout :: print_details() const

{

cout<<”\n account no”<<accno;

count<<”\n customer Name”<<Name1;

cout<<”\n balance”<<balance;

}

void main()

{

account acc1;

acc1.read();

acc1.deposit(3000);

acc1.print_details();

}

o/p :

input account no

 

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Constant Member Function in C++"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.