Tag: C++
Function Overloading Concept
by Binod on Apr.03, 2011, under C & C++
#include
#include
class binod {
int x,y,a;
public:
int area(int){
cout<<”Enter length of square:”<
cin>>x;
a=x*x;
return a;
}
int area(int,int){
cout<<<”Enter length and breadth of rectangle:”<
cin>>x>>y;
a=x*y;
return a;
}
void showdata(){cout<<”Area=”<
};
main(){
clrscr();
binod b;
b.area(1);
b.showdata();
b.area(1,1);
b.showdata();
getch();
return 0;
}
Friend Function concept
by Binod on Apr.03, 2011, under C & C++
#include<iostream.h>
#include<conio.h>
class beta;
class alpha{
int a;
public:
alpha(){a=5;}
friend int add(alpha, beta);
};
class beta{
int b;
public:
beta(){b=5;}
friend int add(alpha, beta);
};
int add(alpha a, beta b){
int x=a.a;int y=b.b;
int sum=x+y;
return sum;
}
main(){
clrscr();
alpha aa;
beta bb;
int s=add(aa,bb);
cout<<”Sum=”<<s;
getch();
return 0;
}
Containership concept
by Binod on Apr.03, 2011, under C & C++
#include<iostream.h>
#include<conio.h>
class base{
public:
void showdata(){cout<<”Base Class OP”<<endl;}
};
class container{
base b;
public:
void showdata(){
cout<<”Container Class OP”<<endl;
b.showdata();
}
};
main(){
clrscr();
container a;
a.showdata();
getch();
return 0;
}
Fibonacci series using OOP concept
by Binod on Apr.03, 2011, under C & C++
#include<iostream.h>
#include<conio.h>
class fibo{
int n,temp;
public:
void getdata(){
cout<<”Enter the no of series to display:”;
cin>>n;
}
void showdata(){
int a=0,b=1;
cout<<a<<”\t”<<b;
for(int i=3;i<=n;i++){
temp=a+b;
cout<<”\t”<<temp<<”\t”;
a=b;
b=temp;
}
}
};
main(){
clrscr();
fibo b;
b.getdata();
b.showdata();
getch();
return 0;
}
Constructor Overloading Concept
by Binod on Apr.03, 2011, under C & C++
#include<iostream.h>
#include<conio.h>
class binod{
int x,y; //private
public:
binod(){cout<<”Default Constructor called”<<endl;} //default constructor
binod(int a,int b){ //overloaded constructor
int m=a;int n=b;
cout<<”M=”<<m<<endl<<”N=”<<n<<endl;
}
void getdata(){
cout<<”Enter x and y:”;
cin>>x>>y;
binod(x,y);
}
void showdata(){
cout<<”X=”<<x<<endl<<”Y=”<<y;
}
};
main(){
clrscr();
binod b;
b.getdata();
b.showdata();
binod(0,0); //calling of overloaded constructor
getch();
return 0;
}
Prefix And Postfix addition in Complex Number
by Binod on Apr.03, 2011, under C & C++
#include<iostream.h>
#include<conio.h>
class complexNumber{
int real,imaginary;
public:
complexNumber(){}
complexNumber(int x, int y){real=x;imaginary=y;}
void getdata(){
cout<<”Enter a real part: “;
cin>>real;//takes real numbr from user
cout<<”Enter a Imaginary part: “;
cin>>imaginary;//takes imaginary numbr from user
}
void showdata(){
if(imaginary<0) //imaginary negative
cout<<real<<”"<<imaginary<<”i”<<endl;
else if(imaginary==0) //no imaginary
cout<<real<<endl;
else if(real==0) //no real part
cout<<imaginary<<”i”<<endl;
else
cout<<real<<”+”<<imaginary<<”i”<<endl;
}
complexNumber operator ++(){ //prefix
++real;
++imaginary;
return *this;
}
complexNumber operator ++(int x){ //postfix
complexNumber temp;
temp.real=real++;
temp.imaginary=imaginary++;
return temp;
}
};
main(){
clrscr();
complexNumber a,b,c,d,e,f;
a.getdata();
cout<<”The Imaginary Number A is:”;a.showdata();
b.getdata();
cout<<”The Imaginary Number B is:”;b.showdata();
c=++b;
cout<<”Object C <- B by Prefix Operator:”;c.showdata();
cout<<”Increment of object B:”;b.showdata();
d=a++;
cout<<”Assigned value of Postfix in D <- A:”;d.showdata();
cout<<”Value at object A:”;a.showdata();
getch();
return 0;
}
Complex Number Addition (a+ib)+(x-iy)
by Binod on Apr.03, 2011, under C & C++
#include<iostream.h>
#include<conio.h>
class complexNumber{
int real,imaginary;
public:
void getdata(){
cout<<”Enter a real part: “;
cin>>real;//takes real numbr from user
cout<<”Enter a Imaginary part: “;
cin>>imaginary;//takes imaginary numbr from user
}
void showdata(){
if(imaginary<0) //imaginary negative
cout<<real<<”"<<imaginary<<”i”<<endl;
else if(imaginary==0) //no imaginary
cout<<real<<endl;
else if(real==0) //no real part
cout<<imaginary<<”i”<<endl;
else
cout<<real<<”+”<<imaginary<<”i”<<endl;
}
complexNumber add(complexNumber b){ //adding by passing object
real+=b.real;
imaginary+=b.imaginary;
return *this;
}
};
main(){
clrscr();
complexNumber a,b,c;
a.getdata();
cout<<”The Imaginary Number is:”;
a.showdata();
b.getdata();
cout<<”The Imaginary Number is :”;b.showdata();
c=a.add(b);
cout<<”The sum of two imaginary number is:”;c.showdata();
getch();
return 0;
}
Complex Number (a+ib) in C++
by Binod on Apr.03, 2011, under C & C++
To create a class name complexNumber with two data member real and imaginary, include two member functions to take I/P and give O/P.
Source Code:
#include<iostream.h>
#include<conio.h>
class complexNumber{
int real,imaginary;
public:
void getdata(){
cout<<”Enter a real part: “;
cin>>real;//takes real numbr from user
cout<<”Enter a Imaginary part: “;
cin>>imaginary;//takes imaginary numbr from user
}
void showdata(){
if(imaginary<0) //imaginary negative
cout<<real<<”"<<imaginary<<”i”<<endl;
else if(imaginary==0) //no imaginary
cout<<real<<endl;
else if(real==0) //no real part
cout<<imaginary<<”i”<<endl;
else //imaginary positive
cout<<real<<”+”<<imaginary<<”i”<<endl;
}
};
main(){
clrscr();
complexNumber a,b,c,d;
a.getdata();
cout<<”The Imaginary Number is:”;a.showdata();
b.getdata();
cout<<”The Imaginary Number is :”;b.showdata();
c.getdata();
cout<<”The Imaginary Number is :”;c.showdata();
d.getdata();
cout<<”The Imaginary Number is :”;d.showdata();
getch();
return 0;
}
Concatinate two string without using string.h
by Binod on Apr.03, 2011, under C & C++
To Concatinate two string we first find out the total sum of two individual string and add them, then we copy the first string to temp variable[string] then we start to append the next string to temp variable from the length of first string till the total length.
Source Code:
#include<iostream.h>
#include<conio.h>
class binod{
int i,j,counta,countb,totl;
char a[10],b[10],newstr[20];
public:
void getdata();
void countlen();
void addstr();
void clrstr();
void showdata();
};
void binod::getdata(){
cout<<”Enter first string: “;
cin>>a;
cout<<”Enter second string: “;
cin>>b;
}
void binod::countlen(){
counta=0;
for(i=0;a[i]!=’\0′;i++){
newstr[i]=a[i];
counta++;
}
countb=0;
for(i=0;b[i]!=’\0′;i++){
countb++;
}
}
void binod::addstr(){
totl=0,j=0;
totl=counta+countb;
for(i=counta;i<totl;i++){
newstr[i]=b[j];
j++;
}
}
void binod::clrstr(){
for(i=0;i<20;i++){newstr[i]=’ ‘;}
}
void binod::showdata(){
cout<<endl<<”The Concatinated string is: “<<newstr;
}
main()
{
clrscr();
binod k;
k.clrstr();
k.getdata();
k.countlen();
k.addstr();
k.showdata();
getch();
return 0;
}
Find the length of a given string without using string.h.
by Binod on Apr.03, 2011, under C & C++
We know that each string has ‘\0’ at the end of it in the memory array. So to find the length, we read individual character of the string until it encounters ‘\0’.
Source Code:
#include<iostream.h>
#include<conio.h>
class binod{
int i,count;
char sen[10];
public:
void getdata(){
cout<<”Enter the string: “;
cin>>sen;
}
void showlen();
};
void binod::showlen(){ //For Counting length
count=0;
for(i=0;sen[i]!=’\0′;i++){
count++;
}
cout<<”The length of “<<sen<<” is: “<<count;
}
main(){
clrscr();
binod k;
k.getdata();
k.showlen();
getch();
return 0;
}