Constructor | Destructor |
it is executed on creation of objectits job is inita class contain multiple constructorsit can have parameters | it is executed before deleting objectits job is deinita class contain only one destructorit cannot have parameters. |
Member of class can be declared as constants Constant members are of 2 types. Constant data member Constant member function
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
const data type datamember-name syntax of init constant data members using construct list constructor-name(): constant-datamember(value,constant-datamember(value)---- { statements; }
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
#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 is an accessory function which cannot modifying values of data members. It perform constant operation.
<return type> function-name(<parameters>) constant { statements; }
# 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
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.