prowessapps.in Constructor Programs in C++

#include< iostream>
using namespace std;
class Test {
 int hr;
 int min;
 public :
 Test() {
  hr = 0;
  min =0 ;
  cout<<"Default Const. "<< endl;
 }
 Test(int a, int b) {
  hr = a;
  min = b;
  cout<<"Param Const. "<< endl;
 }
 Test(Test &a) {
  hr = a.hr;
  min = a.hr;
 }
 ~Test() {
  cout<<"Destructor Called"<< endl;
 }
 void show() {
  cout<< hr<<" : "<< min<< endl;
 }
};
int main() {
 Test t ;
 t.show();
 Test t2(12,30); 
 t2.show();
 Test t3(t);
 t3.show();
 Test t4(t2); 
 t4.show();
 return 0;
}
OUTPUT
Default Const.
0 : 0
Param Const.
12 : 30
0 : 0
12 : 30
Destructor Called
Destructor Called
Destructor Called
Destructor Called

#include< iostream>
using namespace std;
class Deposit{
 int p,t,r,a;
 public :
 void input() {
  cout<<"Enter principle amount";
  cin>>p;
  cout<<"Enter time ";
  cin>>t;
  cout<<"Enter Rate of Interest ";
  cin>>r;
 }
 void display() {
  a= p+((p*r*t)/100);
  cout<<"Total Amount "<< a<< endl;
 }
 Deposit() {
  cout<<"Default Const. called\n";
  p =0;
  t= 0;
  r = 0;
 }
 Deposit(int pr, int ti, int ra) {
  cout<<"param Const. called \n"<<;
  p = pr;
  t= ti;
  r = ra;
 }
 Deposit(Deposit &d){
  cout<<"Copy Const. called\n";
  p = d.p;
  t = d.t;
  r = d.r;
 }
 ~Deposit() {
  cout<<"Destructor called\n";
 }
};
int main() {
 Deposit d;
 d.input();
 d.display();
 Deposit d2(1000,20,3);
 d2.display();
 cout<<"Default Const. copied\n"<<;
 Deposit d3(d);
 d3.display();
 cout<<"Param. Const. copied\n";
 Deposit d4(d2);
 d4.display();
 return 0;
}
OUTPUT
Default Const. called
Enter principle amount 5000
Enter time 2
Enter Rate of Interest 5
Total Amount 5500
param Const. called
Total Amount 1600
Default Const. copied
Copy Const. called
Total Amount 5500
Param Const. copied
Copy Const. called
Total Amount 1600
Destructor called
Destructor called
Destructor called
Destructor called

#include< iostream>
using namespace std;
class Test {
 public:
 Test(string a){
  cout<< a<<" constructor invoked\n";
 }
 void show() {
  Test t("in function");

 }
 ~Test() {
  cout<<"Distructor called"<< endl;
 }
};
Test t3("Before Main");
int main() {
 Test t1("in main");
 t1.show();
 Test t2("After function call");
 return 0;
}
OUTPUT
Before Main constructor invoked
in main constructor invoked
in function constructor invoked
Distructor called
After function call constructor invoked
Distructor called
Distructor called
Distructor called


Get it on Google Play


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved