Search This Blog

Monday, July 4, 2011

Constructor Part-2


#include<iostream.h>
class Bulb
{
int w;
public:
void askinfo()
{
cout<<"Enter the wattage";
cin>>w;
}
void printinfo()
{
cout<<"Wattage is:"<<w<<endl;
}


};
void main()
{
Bulb k;
Bulb g(60);
}
---------------------------------------------------------

#include<iostream.h>
class Bulb
{
int w;
public:
void askinfo()
{
cout<<"Enter the wattage";
cin>>w;
}
void printinfo()
{
cout<<"Wattage is:"<<w<<endl;
}
Bulb()
{
w=0;
}


};
void main()
{
Bulb k;
Bulb g(60);
}
--------------------------------------------------------

#include<iostream.h>
class Bulb
{
int w;
public:
void askinfo()
{
cout<<"Enter the wattage";
cin>>w;
}
void printinfo()
{
cout<<"Wattage is:"<<w<<endl;
}
Bulb()
{
w=0;
}
Bulb(int e)
{
w=e;
}


};
void main()
{
Bulb k;
Bulb g(60);
}


---<<<Part-1---      --------Continue>>>Part-3---


Constructor Part-1


#include<iostream.h>
class Bulb
{
private:
int w;
public:
void askinfo()
{
int x;
cout<<"Enter wattage";
cin>>x;
if(x>=0&&x<=24)
{
w=x;
}
else
{
x=0;
}
}
void printinfo()
{
cout<<"Wattage is:"<<w<<endl;
}
};
void main()
{
Bulb k;
k.printinfo();
}
--------------------------------------------------------------------

#include<iostream.h>
class Bulb
{
int w=0;    //Class level
public:
void askinfo()
{


cout<<"Enter wattage";
cin>>w;
}
void printinfo()
{
cout<<"Wattage is:"<<w<<endl;
}
};
void main()
{
Bulb g,t;
g.printinfo();
t.printinfo();
Bulb k;
k.printinfo();
}
-----------------------------------------------------------------------

#include<iostream.h>
class Bulb
{
int w;
public:
void initialize()
{
w=0;
}
void askinfo()
{


cout<<"Enter wattage";
cin>>w;
}
void printinfo()
{
cout<<"Wattage is:"<<w<<endl;
}
};
void main()
{
Bulb g,t;
g.printinfo();
t.printinfo();
Bulb k;
k.printinfo();
}
-----------------------------------------------------------------------

#include<iostream.h>
class Bulb
{
int w;
public:
Bulb()
{
w=0;
}


void askinfo()
{


cout<<"Enter wattage";
cin>>w;
}
void printinfo()
{
cout<<"Wattage is:"<<w<<endl;
}
};
void main()
{
Bulb g,t;
g.printinfo();
t.printinfo();
Bulb k;
k.printinfo();
}
---------------------------------------------------------

#include<iostream.h>
class Bulb
{
int w;
public:
Bulb()
{
w=0;
}
Bulb(int e)
{
if(e>=0&&e<=240)
{
w=e;
}
else
{
w=0;
}
}


void askinfo()
{


cout<<"Enter wattage";
cin>>w;
}
void printinfo()
{
cout<<"Wattage is:"<<w<<endl;
}
};
void main()
{
Bulb g(60);
g.printinfo();


Bulb k;
k.printinfo();
}


Note:- Practice all and try to understand the output.
------------------ Continue >>>Part-2----









Sunday, July 3, 2011

Method Overriding

Example:-1



#include<iostream.h>
class Movie
{
public:
void start()
{
cout<<"Welcome"<<endl;
}
void interval()
{
cout<<"Interval - Have coffee of Rs. 5/-"<<endl;
}
void end()
{
cout<<"Thank you - come back again"<<endl;
}
};
class jungleBook:public Movie
{
public:
void reelone()
{
cout<<"Mowgli enters the jungle"<<endl;
}
void reeltwo()
{
cout<<"Bagheera saves mowgli"<<endl;
}
};
void main()
{
jungleBook jb;
jb.start();
jb.reelone();
jb.interval();
jb.reeltwo();
jb.end();
}
-----------------------------------
Example:-2



#include<iostream.h>
class Movie
{
public:
void start()
{
cout<<"Welcome"<<endl;
}
void interval()
{
cout<<"Interval - Have coffee for Rs. 5/-"<<endl;
}
void end()
{
cout<<"Thank you - come again"<<endl;
}
};
class JungleBook:public Movie
{
public:
void reelone()
{
cout<<"Mowgli enters the jungle"<<endl;
}
void reeltwo()
{
cout<<"Bagheera saves mowgli"<<endl;
}
void interval()  //Interval method Overrided
{
cout<<"Interval- Have Pepsi for Rs.10/-"<<endl;
}
};
void main()
{
JungleBook jb;
jb.start();
jb.reelone();
jb.interval();
jb.reeltwo();
jb.end();
}
---------------------------------------------------
Example:-3



#include<iostream.h>
class Movie
{
public:
void start()
{
cout<<"Welcome"<<endl;
}
void interval()
{
cout<<"Interval- Have coffee for Rs.5/-"<<endl;
}
void end()
{
cout<<"Thank you come again"<<endl;
}
};
class JungleBook:public Movie
{
public:
void reelone()
{
cout<<"Mowgli enters the jungle"<<endl;
}
void reeltwo()
{
cout<<"Bagheera saves the mowgli"<<endl;
}
void interval()
{
interval();
cout<<"and have Pepsi for Rs.10/-"<<endl;
}
};
void main()
{
JungleBook jb;
jb.start();
jb.reelone();
jb.interval();
jb.reeltwo();
jb.end();
}
-------------------------------------------------------
Example:-4


#include<iostream.h>
class Movie
{
public:
void start()
{
cout<<"Welcome"<<endl;
}
void interval()
{
cout<<"Interval- Have coffee for Rs.5/-"<<endl;
}
void end()
{
cout<<"Thank you come again"<<endl;
}
};
class JungleBook:public Movie
{
public:
void reelone()
{
cout<<"Mowgli enters the jungle"<<endl;
}
void reeltwo()
{
cout<<"Bagheera saves the mowgli"<<endl;
}
void interval()
{
Movie::interval();
cout<<"and have Pepsi for Rs.10/-"<<endl;
}
};
void main()
{
JungleBook jb;
jb.start();
jb.reelone();
jb.interval();
jb.reeltwo();
jb.end();
}


-------------------------------------------------------------------------------

Call by Value / Reference


Call by Value:-


#include<iostream.h>
void lmn(int);
void main()
{
int x;
x=10;
lmn(x);
cout<<"Value is :"<<x<<endl;
}
void lmn(int p)
{
p=50
}
-----------------------------------------------------------
Call by Reference:-



#include<iostream.h>
void lmn(int *);
void main()
{
int x=10;
lmn(&x);
cout<<x<<endl;
}
void lmn(int *p)
{
*p=50;
}



-------------------------------------------------------------------


#include<iostream.h>
void lmn(int &);
void main()
{
int x=10;
lmn(x);
cout<<x<<endl;

}
void lmn(int &p)
{
p=50;
}

-------------------------------------------------------------

Plz go for it..........