Back To C++ Constructors & Destructors
constructor is a special member function that has the same name as that of class and is used to initialize the datamembers of the class
//program to demonstrate example of class student that contains two public member functions getdata() and showdata() and four data members (rollno,name,clas and marks)program to demonstrate example of class bankaccount with data members ( account_no,name,type,balance) and contains member functions (deposit,withdraw and showdata), class also contains a default constructor #include
#include
#include
class account { int account_no; char name[20]; char type; int balance; public: account() { account_no=1001; strcpy(name,"abcdef"); type='s'; balance=1000; } void deposit(int amount) { balance=balance+amount; cout<<"\n"<
=1000) { balance=balance=amount; cout<<"\n"<