Thursday, 29 March 2018

To Create A Text File And Write Into It

"Nice One Program"

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{       clrscr();

int a=10;
char ch='a';
double d=10.5;
char cobj[5]="ravi";
ofstream rvobj("rv.txt");
rvobj<<a;
rvobj<<ch;
rvobj<<d;
rvobj<<cobj;
cout<<"\nFile Written";



getch();

}

//File Created will be look like this






And Output:



Monday, 26 March 2018

To Create Friend Function

"Nice One Program"

#include<iostream.h>
#include<conio.h>
class A;                   //must declare first before use in B class
class B{
int b;
public:
B()
{ b=10;
}
friend void sum(A,B);

};
class A
{
int a;
public:
A()
{
a=20;
}
friend void sum(A,B);
};
void sum(A A1,B B1)
{
cout<<"Sum:"<<(A1.a+B1.b);

}
void main()
{         clrscr();
A obj1;
B obj2;
sum(obj1,obj2);
getch();
}










Saturday, 24 March 2018

To Add Own Header File in C++

"Nice One Program"

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

void main()                
{         clrscr();
int a=10,b=20;
int c=sum(a,b);                 //no prototype now create new file with extension .h
cout<<"Sum="<<c;      //again error now include file using ""

getch();
}


FILE WITH .H Exntension Will Have

int sum(int x,int y)
{
return (x+y);
}


Tuesday, 6 March 2018

Program For Copy Constructor

"Nice one Program"

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class point  {
public:          int x,y;
point() {x=0,y=0;        }
point(point &p)
{ x=p.x;
  y=p.y;
}

};
void main()
{clrscr();
point p1,p2;
p1.x=10;
p1.y=20;
p2=p1; //copy constructor called here
cout<<"p2.x="<<p2.x<<endl;
cout<<"p2.y="<<p2.y;
getch();

}



Assignment Operator Overloading ( +=)

"Nice One Program"

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

class rv{ public:
int inch,feet;
rv(){ inch=0,feet=0;

}
void operator +=(rv dis)
{
feet=feet+dis.feet;
inch=inch+dis.inch;
if(inch>=12)
{ feet+=inch/12;
  inch%=12;

}


}};

void main()
{       clrscr();
rv dis1,dis2;
cout<<"Enter dis1: ";
cin>>dis1.feet;
cin>>dis1.inch;
cout<<"\nEnter dis2: ";
cin>>dis2.feet;
cin>>dis2.inch;
dis1+=dis2;

cout<<"\n Value of feet: "<<dis1.feet;
cout<<"\n value of inch:"<<dis1.inch;
getch();

}


Operator Overloading

Adding Two Objects

"Nice One Program"

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

class rv{ public:
int val;
rv(){ val=1;

}
rv operator +(rv ss)
{     cout<<"func called Will returb object\n";
rv temp;
temp.val=val+ss.val;
return temp;
}};

void main()
{       clrscr();
rv obj1,obj2,obj3;

obj3=obj1+obj2;
cout<<"\n Value of obj3: "<<obj3.val;
getch();
}


Operator Overloading:-

Concatination of Two Strings

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<string.h>
class rv{ public:
char str[20];
rv(){ strcpy(str," ");

}
rv(char s[])
{
strcpy(str,s);
}
rv operator +(rv ss) const
{     cout<<"func called\n";
rv temp;
if((strlen(str)+strlen(ss.str))<20)
{   strcpy(temp.str,str);
    strcat(temp.str,ss.str);
}
else
cout<<"Overflow";
return temp;
}
};

void main()
{       clrscr();
rv st1="hello",st2=" ravi",st3;

st3=st1+st2;
cout<<st3.str;
getch();
}