CAT | C++
Constructor and Destructor Order
The process of creating and deleting objects in C++ is not a trivial task. Every time an instance of a class is created the constructor method is called. The constructor has the same name as the class and it doesn’t return any type, while the destructor’s name it’s defined in the same way, but with a ‘~’ in front:
class String
{
public:
String() //constructor with no arguments
:str(NULL),
size(0)
{
}
String(int size) //constructor with one argument
:str(NULL),
size(size)
{
str = new char[size];
}
~String() //destructor
{
delete [] str;
};
private:
char *str;
int size;
}
Even if a class is not equipped with a constructor,
the compiler will generate code for one, called the
implicit default constructor. This will typically
call the default constructors for all class members,
if the class is using virtual methods it is used to
initialize the pointer to the virtual table, and,
in class hierarchies, it calls the constructors of
the base classes.
#include <iostream.h>
#include <conio.h>
class temp
{
int *t;
int size;
public:
temp()
{t=new int[1];
size=1;}
temp(int m)
{size=m;
t=new int[size];}
void get()
{
for (int i=0;i<=size-1;i++)
{
cout<<”enter elements=”;
cin>>t[i];
}
}
void display()
{
for (int i=0;i<=size-1;i++)
{
cout<<endl<<t[i];
}
}
};
void main()
{
clrscr();
temp t1;
t1=temp(5);
t1.get();
t1.display();
getch();
}
12
Decuse of setw and endl to create manipulator currency….
0 Comments | Posted by payalbharuka in C++
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
class number
{
public:
int n;
void get()
{cout<<”input number=”;cin>>n;}
void display()
{cout<<”number given is=”<<n;}
};
ostream & currency(ostream &out)
{
out<<”\n”;
out<<”rs.”;
return out;
}
void main()
{
number N;
clrscr();
N.get();
N.display();
cout<<currency<<N.n<<endl;
cout<<endl<<setw(5)<<N.n<<endl;
cout<<setw(15)<<N.n<<endl;
getch();
}
# include <iostream.h>
#include <conio.h>
void swap(int*,int*);
void main()
{
int a=20,b=30;
clrscr();
cout <<endl<<”a=”<<a<<endl<<”b=”<<b;
swap(&a,&b);
cout <<endl<<”after swapping=”<<endl<<”a=”<<a<<endl<<”b=”<<b;
getch();
}
void swap (int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
#include <conio.h>
class temp
{
int a;
public:
static int c;
void get(int t)
{
a=t;
c++;
}
void display()
{
cout << a;
}
static void show()
{
cout << c;
}
};
int temp :: c;
void main()
{
temp t1,t2,t3;
clrscr();
t1.get(20);
cout << "\n";
t1.display();
cout << "\n";
temp :: show();
t2.get(12);
cout << "\n";
t2.display();
cout << "\n";
temp :: show();
temp :: c=30;
cout << "\n";
temp :: show();
getch();
}
![[del.icio.us]](http://www.itshala.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.itshala.com/wp-content/plugins/bookmarkify/digg.png)
![[dzone]](http://www.itshala.com/wp-content/plugins/bookmarkify/dzone.png)
![[Facebook]](http://www.itshala.com/wp-content/plugins/bookmarkify/facebook.png)
![[Google]](http://www.itshala.com/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://www.itshala.com/wp-content/plugins/bookmarkify/linkedin.png)
![[Twitter]](http://www.itshala.com/wp-content/plugins/bookmarkify/twitter.png)
![[Windows Live]](http://www.itshala.com/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://www.itshala.com/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.itshala.com/wp-content/plugins/bookmarkify/email.png)
