Saturday, November 12, 2011

// Simple constructor2
#include<iostream>
#include<conio.h>



using namespace std;

class  A{
int a;
public:
void geta(){
cout <<"Enter no.";
cin >>a;
}
void showa(){
cout<<"A="<<a;
}

A(){        //Defult constructor
a=0;
}
A (int x){    // Parameterized constructor
a=x;
}
};

main(){
A obj;// At the time of object creating constructor
        // will called automatically
        obj.showa();
        A obj2(10);    // Parameterized constructor will call
        cout<<endl;
        obj2.showa();
getch();
return 0;
}

Sunday, November 6, 2011

Computer science question paper of burdwan univercity


If you requested for password then type 'avishek'

complex addition using + operator overloading

//Complex addition using Overloading + operator

#include<iostream>
#include<conio.h>

using namespace std;

class complex
{
    float x;
    float y;
public:
    complex(){};
    complex(float real,float i){
    x=real;
    y=i;
    }
    complex operator+(complex);
    void display();
};

complex complex :: operator+(complex c)
{
    complex temp;
    temp.x=x+c.x;
    temp.y=y+c.y;
    return(temp);
}

void complex :: display()
{

    cout << x << "+" <<y<<"i"<<endl;
}

main(){
    complex c1(2.5,3.5),c2(4.7,3.4),c3;
    c3=c1+c2;
   
    cout <<"C1=";c1.display();
    cout <<"c2=";c2.display();
    cout <<"C3=";c3.display();
   
    getch();
    return 0;
}
Output of above program

Destructor

Out put of destructor
// Destructor
# include<iostream>

# include<conio.h>

using namespace std;


class alpha{
      static int count;
public:
alpha(){
count++;
cout<<endl<<"No. of object ctreated"<<count;
}

~alpha(){
cout<<endl<<"No. of object destructed"<<count;
count--;
}
};

int alpha::count=0;

int main(){
cout<<endl<<"Enter main"<<endl;
alpha a1,a2,a3,a4;

{
cout<<endl<<"Enter block one"<<endl;
alpha a5;
}

{
cout<<endl<<"enter block 2"<<endl;
alpha a6;
}

cout <<endl<<"Re enter main"<<endl;
getch();
return (0);
}

Copy constructor

Out put of the program
// Copy constructor
# include<iostream>
# include<conio.h>

using namespace std;
class code
{
int id;
public:
code(){}    //Blank constructor
code(int a){
id=a;
}
code(code & x){
id=x.id;
}
int display(){
     return(id);
}
};

main(){

code A(100);    // object a is created and initilized
code B(A);    // copy constructor called
code C=A;
code D;
D=A;

cout<<endl<<"id of A:"<<A.display();
cout<<endl<<"id of B:"<<B.display();
cout<<endl<<"id of C:"<<C.display();
cout<<endl<<"id of D:"<<D.display();
getch();
return 0;
}