Search This Blog

Sunday, 23 August 2020

Frioend function without sccope resolution

 #include<iostream>

#include<conio.h>

using namespace std;

class A;

class B

{

int b;

public:

void getval(int x)

{

b=x;

}

putval(void)

{

cout<<"\n Value of B is:"<<b;

}

friend void add(A,B);

};

class A

{

int a;

public:

void getval(int x)

{

a=x;

}

void putval(void)

{

cout<<"Value of A :"<<a;

}

friend void add(A,B);

}

void add(A obj1,B obj2)

{

cout<<"Addition of A and B is : "<<obj1.a+obj2.b;

}

int main();

A a;

B b;

a.getval(100);

b.getval(200);

a.putval();

b.putval();

add(a,b);

getch();

return 0;


Friend function ex1

 #include<iostream>

using namespace std;

class rectangle

{

double length;

public:

friend void printlength(rectangle R);

void setlength(double len);

};

void rectangle::setlength(double len)

{

length=len;

}

void printlength(rectangle rectangle)

{

cout<<"length of rectangle :"<<R.length<<endl;

}

int main()

{

rectangle R;

R.setlength(10.0);

printlength(R);

return 0;

}


Friend function call

 #include<iostream>

#include<conio.h>

using namespace std;

class xyz;

class abc

{

int a;

public:

void getA()

{

cout<<"\n\nEnter the value of A:";

cin>>a;

}

friend void max(abc,xyz);

};

class xyz

{

int x;

public:

void getX()

{

cout<<"\n\nEnter the value of X:";

cin>>x;

}

friend void max(abc,xyz);

};

void max(abc a1,xyz x1)

{

if(a1.a>x1.x)

{

cout<<"\n\nA is gretter";

}

else

{

cout<<"\n\nx is gretter";

}

}

int  main()

{

abc a;

xyz x;

a.getA();

x.getX();

max(a,x);

getch();

}


for loop ex 2

 #include<iostream>

using namespace std;

int main()

{

int a,b;

for(a>0;a<5;a++)

cout<<"value of a :\n "<<  a   <<endl;

for(b>5;b<10;b++)

cout<<"value of b :\n"<< b <<endl;



return 0;

}


For loop ex1

 #include<iostream>

using namespace std;

int main()

{

int a , b , c;

cout<<"enter the number  a : "<<endl;

cin>>a;

cout<<"enter tne another number starter"<<endl;

cin>>b;

cout<<"enter the gretter number series that you have"<<endl;

cin>>c;

for(a>b;a<c;a++)

cout<<"value is : "<<a<<endl;

system("pause");

}


sing for loop find the squares of numbers..

 #include<iostream>

using namespace std;

int main()

{

for(int i=1;i<=10;i++)

cout<<"\nsqares of "<<i<<" "<<"is="<<(i*i)<<endl;

return 0;

}


for loop concept..

 #include<iostream>

#include<conio.h>

using namespace std;

int main()

{

for(int i=1;i<4;i++)

{

for(int j=1;j<4;j++)

{

cout<<" ";

}

cout<<"*";

}

return 0;

}


Find out function FIND THE STRING OR ANY THING USING THIS ..

 #include<iostream>

using namespace std;

int main()

{

string str="good morning";

char findout;

cout<<"string is:"<<str<<endl;

cout<<"enter the charactor you want to find:"<<endl;

cin>>findout;

cout<<"we find the "<<findout<< " "<<"at :"<<str.find(findout);

return 0;

}


Fie handling 2

 #include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
int main()
{
char name[20];
int cost;
ofstream outf("data");           //file name declaration..
cout<<"Enter the item name :";
cin>>name;
cout<<"Enetr the item cost :";
cin>>cost;
outf<<name<<endl;            //output from user..
outf<<cost<<endl;
outf.close();
ifstream inf("data");
inf>>name;
inf>>cost;
cout<<"\n Item name : "<<name;
cout<<"\n Item cost :"<<cost;
inf.close();
getch();
return 0;
}

File handling 1

 #include<iostream>

#include<fstream>

using namespace std;


const char *FILE_NAME ="c://txt_file.txt";

int main()

{

ofstream fout(FILE_NAME);

cout<<"Enter some text::";

char str[100];

cin>>str;                  //write the text to file;;

fout<<str<<endl;

                            //close file

ifstream fin(FILE_NAME);

char ch;

cout<<"Data read from the file:\n ";

while(fin.get(ch)) 

cout<<ch;

fin.close();

                           


return 0;

}


USE of getline function

 #include<iostream>


#include<cstring>

using namespace std;

int main()

{

char name[20];

char sirname[20];

// cin>>name;

// cin>>sirname;


cin.getline(name,20);

cin.getline(sirname,20);

cout<<name<<" loves "<<sirname;

return 0;



}


find the factorial ..

 #include<iostream>

using namespace std;

long factorial (int n)

{

int counter;

long fact =1;

for (int i=1;i<=n;i++)

{

fact=fact*i;

}

return fact;

}

int main()

{

int counter,n;

cout<<"ENTER THE NUMBER:\n";

cin>>n;

cout<<n<<" factorial value is : \n"<<factorial(n)<<endl;

cout<<"\n\n\n\n";a

system("pause");

}


fabonacci series

 #include<iostream>

using namespace std;

int main()

{

int n,a=1,b=2,next,c;

cout<<"enter the no of terms\n";

cin>>n;

cout<<"first "<<n<<" terms of fibonacci series are:-\n";

for(c=0;c<n;c++)

{

if(c<=1)

next = c;

else

{

next=a+b;

a=b;

b=next;

}

cout<<next<<"\n";

}

return 0;

}


Search This Blog

Contact Form

Name

Email *

Message *

Popular Posts