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;
}

Saturday, November 5, 2011

Class with constructor

// Use of constructor
# include<iostream>
#include<conio.h>

using namespace std;

class integer{
int m,n;
public:
integer(int,int);

void display( void){

cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
}
};

integer :: integer(int x,int y){
m=x;n=y;
}

int main(){
integer int1(0,111);    //constructor called implicitly
integer int2=integer(28,75);    //Constructor called explicitly

cout<<endl<<"Object1:"<<endl;
int1.display();

cout<<endl<<"Object 2"<<endl;
int2.display();
getch();

return 0;
}

Friend function

// Friend function
#include<iostream>
#include<conio.h>

using namespace std;

class B;
class A{
private:
int a;
public:
void geta(){
cout <<" Enter a no.  :";
cin >> a;
}
void puta(){
cout <<" a=" <<a;
}
friend int max(A,B);
};

class B {
private:
int b;
public:
void getb(){
cout<<" Enter a no.  :";
cin >> b;
}
void putb(){
cout<<" b="<<b;
}
friend int max(A,B);
};

int max (A obj1, B obj2){
return(obj1.a>obj2.b ? obj1.a:obj2.b);
}

main(){
A obj1;
B obj2;
obj1.geta();
obj2.getb();
cout<<"MAX="<<max(obj1,obj2);
getch();
return 0;
}

Static member function

// Static mambre fn.
//static membr fn. is a class member fn.It could only access other static data member, or static member fn.
//objects of that class can acces static mamber fn. It is accessed only by that class.
#include<iostream>
#include<conio.h>

using namespace std;

class A{
private:
int a;
static int c;
public:
void geta(){
     cout<<" Enter a no.";
     cin>>a;
     c++;
     }
void showa(){
     cout<<"a="<<a;
     }
    
static void show_count(){
cout <<" Total object created="<<c;
}
};

int A::c;

main(){
A obj1;
obj1.geta();
A obj2;
obj2.geta();
obj1.showa();
A::show_count();
getch();
return 0;
}

Sample bank program

// Bank accounting system
#include<iostream>

using namespace std;

class bank{
private:
int acno;
char name[30];
int bal;
char actype;
public:
void deposit(int);
int withdraw (int);
void opac();
void show_info();
};

void bank::opac(){
cout << "Enter A/C no.:";
cin >> acno;
cout <<"Enter name:";
cin >> name;
cout <<"Enter opening balance:";
cin >> bal;
cout <<"Enter account type (s/l/c)";
cin >> actype;
}

void bank::show_info(){
cout<<"A/C no :"<<acno<<endl;
cout<<"Name :"<<name<<endl;
cout<<"A/C type :"<<actype<<endl;
cout<<"Balance :"<<bal<<endl;
}

void bank::deposit(int amt){
bal+=amt;
}

int bank:: withdraw(int amt){
if(actype=='l'){
cout<<"Withdraw is not possible";
return 0;
}
else if (actype=='c'){
    if(bal-amt<200){
    cout<<"Minimum balance reached"<<endl;
    return 0;
    }
    else{
    bal=bal-amt;
    return(bal);
    }
    }
    else if(actype=='s'){
        if(bal-amt<500){
        cout<<"Minimum balance reached!"<<endl;
        return 0;
        }
        else{
        bal=bal-amt;
        return(bal);
        }
    }
}

main(){
bank a;
int ch=0,amt;
cout<<"opening your account:"<<endl;
a.opac();

do{
    cout<<"1. Show account details:"<<endl;
    cout<<"2. withdraw from account:"<<endl;
    cout<<"3. deposite to account :"<<endl;
    cout<<"4. Exit "<<endl;
    cout<<"Enter your choice:"<<endl;
    cin>>ch;
if(ch==1)
a.show_info();
else if(ch==2){
cout<<" Enter amount to withdraw :";
cin>> amt;
cout<<"Rs."<<a.withdraw(amt)<<endl;
}
else if(ch==3){
cout << "Enter amount to deposite :";
cin >>amt;
a.deposit(amt);
}
}while(ch!=4);
return 0;
}