class oops

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 88

INPUT 2 NO.

AND OPEAROR THEN SWITCH CASE

#include<iostream>
using namespace std;

class Operator
{
int a,b,sw;
char c;
public:
void setValue(){
cout<<"Enter the 1st value:";
cin>>a;
cout<<"Enter the 2nd value:";
cin>>b;
cout<<"Enter the operation you want to perform:";
cin>>c;
oprat(c);
}
void oprat(char d)
{
switch(d)
{
case '+' :cout<<"ADDITION OF "<<a<<"and "<<b<<" is:"<<a+b;
break;
case '-' : cout<<"SUBTRACTION OF "<<a<<"and "<<b<<"
is:"<<a-b;
break;
case '*' : cout<<"MULTIPLICATION OF "<<a<<"and "<<b<<"
is:"<<a*b;
break;
case '/' : cout<<"DIVIDION OF "<<a<<"and "<<b<<" is:"<<a/b;
break;
default : cout<<"INVALID OPERATOR";
}
}
};

int main()
{
Operator p;
p.setValue();
}

sales commission using


switch-------
#include<iostream>
using namespace std;
class Number{
private:
int sale,sw;
public:
void setValue()
{
cout<<"Enter the value of sale:";
cin>>sale;
}
void com()
{

switch(sale)
{
case 1
}
}

};

int main()
{
Number n;
n.setValue();
n.max();
return 0;
}

max among three nested


if---------

#include<iostream>
using namespace std;
class Number{
private:
int a,b,c,max;
public:
void setValue()
{
cout<<"Enter the 1st value:";
cin>>a;
cout<<"Enter the 2nd value:";
cin>>b;
cout<<"Enter the 3rd value:";
cin>>c;
}
void maxI()
{
if(a>b)
{
if(a>c)
cout<<"MAX IS : "<<a;
else
cout<<"MAX IS : "<<c;
}
else
{
if(b>c)
cout<<"MAX IS : "<<b;
else
cout<<"MAX IS : "<<c;
}
}
};
int main()
{
Number n;
n.setValue();
n.maxI();
return 0;
}

SUM OF FIRST THREE NUMBER------

#include<iostream>
using namespace std;

class Number{
int n,sum,t;
public:
void setValue()
{
cout<<"Enter the number: ";
cin>>n;
}
void sum()
{
sum=0;
t=1;
while(t<=n)
{
sum=sum+t;
t++;
}
cout<<"SUM OF FIRST " <<n<< "NUMBERS IS: "<<sum;
}
};
int main(){
Number c;
c.setValue();
c.sum();
}

FACTORIAL---------------

#include<iostream>
using namespace std;

class Number{
int n,fact,t;
public:
void setValue()
{
cout<<"Enter the number: ";
cin>>n;
}
void fact()
{
fact=0;
t=1;
while(t<=n)
{
fact=fact*t;;
t++;
}
cout<<"FACTORAIL OF " <<n<< "NUMBERS IS: "<<fact;
}
};
int main(){
Number c;
c.setValue();
c.sum();
}

REVERSE

#include<iostream>
using namespace std;

class Number
{
int n,revi,rem;
public:
void setValue();
void rev();
};

void Number::setValue()
{
cout<<"Enter no.: ";
cin>>n;
}

void Number::rev()
{
revi=0;
while(n>0)
{
rem=n%10;
n=n/10;
revi=revi*10+rem;
}
cout<<revi;
}

int main()
{
Number s;
s.setValue();
s.rev();
}

ARRRAY ELEMENT EVEN OR ODD

#include<iostream>
#include<stdlib.h>
using namespace std;
#define max 5
class Addition
{
int a[max],i,sum,n;
public:
void readValue()
{
cout<<"Enter the size of array:";
cin>>n;
cout<<"Enter the elements:";
for(i=0;i<n;i++)
cin>>a[i];
}

void even()
{
cout<<"even elments of array are:";
for(i=0;i<n;i++)
if(a[i]%2==0)
cout<<a[i]<<"\t";
cout<<"\n";
}

void odd()
{
cout<<"odd elments of array are:";
for(i=0;i<n;i++)
if(a[i]%2!=0)
cout<<a[i]<<"\t";
cout<<"\n";
}
};

int main()
{
Addition a;
a.readValue();
a.even();
a.odd();
return 0;
}

STACK

#include<iostream>
#include<stdlib.h>
//#include<process.h>
using namespace std;
#define max 5 //preprocessor
//connst int max=5

class Stack
{
int top; //instance variable
int stack[max];
public:
void init()
{
top=-1;
}

void push(int x)
{
if(top==max-1)
cout<<"OVERFLOW";
else
stack[++top]=x;
}

int pop()
{
int p;
if(top==-1)
{
cout<<"UNDERFLOW";
return -999;
}

else
{
p=stack[top--];
return p;
}
}

void peek()
{
cout<<"value at top is :"<<stack[top];
}

void display()
{
int i;
for(i=top;i>=0;i--)
cout<<stack[i]<<endl;
}
};

int main()
{
int x,sw;
Stack s;
s.init();
do
{
cout<<"\nMENU\n1.push\n2.pop\n3.peek\n4.display\n5.exit\nEnter your choice:";
cin>>sw;
switch(sw)
{
case 1:
cout<<"Enter the value you want to push";
cin>>x;
s.push(x);
break;
case 2:
x=s.pop();
cout<<"popped value:"<<x;
break;
case 3:
s.peek();
break;
case 4:
s.display();
break;
case 5:
exit(0);
default:
cout<<"enter valid choice please";

}
}while(sw!=5);
}

AVG MARKS STUDENT

#include<iostream>
#include<stdlib.h>
using namespace std;

class Marks
{
int a[10],i,avg,n;
public:
void readValue()
{
n=10;
cout<<"Enter the MARKS :";
for(i=0;i<n;i++)
cin>>a[i];
}

void avgi()
{
avg=0;
for(i=0;i<n;i++)
{
avg=avg+a[i];
}
avg=avg/10;
cout<<"average is: "<<avg;
}
};

int main()
{
Marks m;
m.readValue();
m.avgi();
return 0;
}

MIN
MAX ELEMENT OF ARRAY

#include<iostream>
#include<stdlib.h>
using namespace std;
#define maxi 5

class Number
{
int a[maxi],i,min,n,max;
public:
void readValue()
{
cout<<"Enter the size of array:";
cin>>n;
cout<<"Enter the elements:";
for(i=0;i<n;i++)
cin>>a[i];
}
void minMax()
{
min=a[0];
for(i=1;i<n;i++)
{
if(min>a[i])
min=a[i];
}
cout<<"MIN ELEMENT OF ARRAY IS:"<<min<<endl;

max=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
}
cout<<"MAX ELEMENT OF ARRAY IS:"<<max;
}
};

int main()
{
Number n;
n.readValue();
n.minMax();
return 0;
}

deleting inserting in array

#include<iostream>
#include<stdlib.h>
using namespace std;
#define maxi 5

class Number
{
int a[maxi],i,min,n,max;
public:
void Size()
{
cout<<"Enter the size of array:";
cin>>n;
}
void readValue()
{
cout<<"Enter the elements:";
for(i=0;i<n;i++)
cin>>a[i];
}

int ls(int x)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==x)
return i;
}
return -1;
}
void deletei()
{
int x,p;
cout<<"Enter the element you want to delete:";
cin>>x;
p=ls(x);
if(p==-1)
cout<<"Element do not exist";
else
{
if(p<n-1)
{
for(i=p;i<=n;i++)
a[i]=a[i+1];
}
n=n-1;
}
}
void display()
{
for(i=0;i<n;i++)
cout<<a[i];
}

};

int main()
{
Number n;
int sw;
n.Size();
do
{
cout<<"\nMENU\n1.Insert\n2.Delete\n3.display\n4.exit\nEnter your choice:";
cin>>sw;
switch(sw)
{
case 1:
n.readValue();
break;
case 2:
n.deletei();
break;
case 3:
n.display();
break;
case 4:
exit(0);
default:
cout<<"enter valid choice please";

}
}while(sw!=4);
}

linear
and binary seach in array

#include<iostream>
#include<stdlib.h>
using namespace std;
#define maxi 5

class Number
{
int a[maxi],i,min,n,max;
public:
void Size()
{
cout<<"Enter the size of array:";
cin>>n;
}
void readValue()
{
cout<<"Enter the elements:";
for(i=0;i<n;i++)
cin>>a[i];
}

int ls(int x)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==x)
return i;
}
return -1;
}
int bs(int x)
{
int mid,low,high;
while(low<=high)
{
mid=(low+high)/2;
if(x==a[mid])
return mid;
else if(x<a[mid])
high=mid-1;
else
low=mid+1;
}
return -1;
}
};

int main()
{
Number n;
int sw,x,p;
n.Size();
n.readValue();
cout<<"Enter the element you want to search";
cin>>x;
do
{
cout<<"\nMENU\n1.linear search\n2.binary search\n3.exit\nEnter your choice:";
cin>>sw;
switch(sw)
{
case 1:

p=n.ls(x);
if(p==-1)
cout<<"Element do not exist";
else
cout<<"position :"<<p+1;
break;
case 2:
p=n.bs(x);
if(p==-1)
cout<<"Element do not exist";
else
cout<<"position :"<<p+1;
break;
case 3:
exit(0);
default:
cout<<"enter valid choice please";

}
}while(sw!=3);
}

SELECTION
AND BUBBLE SORT

#include<iostream>
#include<stdlib.h>
using namespace std;
#define maxi 5

class Number
{
int a[maxi],i,min,n,max;
public:
void Size()
{
cout<<"Enter the size of array:";
cin>>n;
}
void readValue()
{
cout<<"Enter the elements:";
for(i=0;i<n;i++)
cin>>a[i];
}
void ss()
{
int i,j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
void display()
{
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
}

void ls()
{
int i,j;
for(i=0;i<n-1;i++)
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}

};

int main()
{
Number n;
int sw,x,p;

n.Size();
n.readValue();
do
{
cout<<"\nMENU\n1.Selection Sort\n2.Bubble Sort\n3.display\n4.exit\nEnter your
choice:";
cin>>sw;
switch(sw)
{
case 1:
n.ss();
break;
case 2:
n.ls();
break;
case 4:
exit(0);
case 3:
cout<<"updated array";
n.display();
break;
default:
cout<<"enter valid choice please";

}
}while(sw!=4);
}

TRANSPOSE OF MATRIX

#include<iostream>
using namespace std;
class Matrix
{
int i,j,a[20][20],r,c;
public:
void setValue()
{
cout<<"Enter the no. of rows";
cin>>r;
cout<<"Enter the no. of column";
cin>>c;
}
void readValue()
{
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>a[i][j];
}

void display()
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<a[i][j]<<"\t";
cout<<"\n";
}
}
void transpose()
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<a[j][i]<<"\t";
cout<<"\n";
}
}
};

int main()
{
Matrix m;
m.setValue();
m.readValue();
m.display();
cout<<"transpose of matrix is:\n";
m.transpose();

ROW WISE ADDITION IN MATRIX

#include<iostream>
using namespace std;
class Matrix
{
int i,j,a[20][20],r,c;
public:
void setValue()
{
cout<<"Enter the no. of rows";
cin>>r;
cout<<"Enter the no. of column";
cin>>c;
}
void readValue()
{
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>a[i][j];
}

void display()
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<a[i][j]<<"\t";
cout<<"\n";
}
}
void adRow()
{
cout<<"Row wise addition\n";
for(i=0;i<r;i++)
{
int sum=0;
for(j=0;j<c;j++)
{
cout<<a[i][j]<<"\t";
sum=sum+a[i][j];
if(j<c-1)
cout<<"+\t";
else
{
cout<<"=";
cout<<sum;
}

}
cout<<"\n";
}
}
};

int main()
{
Matrix m;
m.setValue();
m.readValue();
m.display();
m.adRow();

COLUMN WISE ADDITION IN MATRIX

#include<iostream>
using namespace std;
class Matrix
{
int i,j,a[20][20],r,c;
public:
void setValue()
{
cout<<"Enter the no. of rows";
cin>>r;
cout<<"Enter the no. of column";
cin>>c;
}
void readValue()
{
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>a[i][j];
}

void display()
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<a[i][j]<<"\t";
cout<<"\n";
}
}
void adColumn()
{
cout<<"Column wise addition\n";
display();
for(j=0;j<c;j++)
{
int sum=0;
for(i=0;i<r;i++)
sum=sum+a[i][j];
cout<<sum<<"\t";
}
}
};

int main()
{
Matrix m;
m.setValue();
m.readValue();
//m.display();
m.adColumn();

TYPESS OF MATRIX

#include<iostream>
using namespace std;
class Matrix
{
int i,j,a[20][20],r,c;
public:
void setValue()
{
cout<<"Enter the no. of rows";
cin>>r;
cout<<"Enter the no. of column";
cin>>c;
}
void readValue()
{
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>a[i][j];
}

void display()
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<a[i][j]<<"\t";
cout<<"\n";
}
}

void diagonal()
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==j)
{
if(a[i][j]!=0)
{
cout<<"It is not diagonal matrix\
n";
return;
}
}
}
}
cout<<"It is diagonal matrix\n";
}
void skew()
{
int d[20][20];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
d[i][j]=a[j][i];
}

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(d[i][j]!=-a[i][j])
{
cout<<"not skewed matrix\n";
return;
}
}
}
cout<<"\n skewed matrix\n";
}

void symmetric()
{
int d[20][20];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
d[i][j]=a[j][i];
}

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(d[i][j]!=a[i][j])
{
cout<<"not skew symmetry matrix";
return;
}
}
}
cout<<"\n skew symmetry matrix";
}
};

int main()
{
Matrix m;
m.setValue();
m.readValue();
m.display();
m.diagonal();
m.skew();
m.symmetric();

ADDITION AND
MULTIPLY OF MATRIX

#include<iostream>
using namespace std;
class Matrix
{
int i,j,a[20][20],r,c,sw;
public:
void menu()
{
do{

cout<<"enter details of 1st matrix";


setValue();
readValue();
cout<<"enter details of 2nd matrix";
setValue();
readValue();
cout<<"\n1.ADDITION\n2.MULTIPLY\n3.Display\n4.exit\nEnter your
choice:";
cin>>sw;
switch(sw)
{
case 1:add();
break;
case 2:mul();
break;
case 3:
exit(0);
}
}while(sw!=3);
}
void setValue()
{
cout<<"Enter the no. of rows";
cin>>r;
cout<<"Enter the no. of column";
cin>>c;
}
void readValue()
{
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>a[i][j];
}

void display()
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<a[i][j]<<"\t";
cout<<"\n";
}
}
void add()
{
int d[20][20];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
d[i][j]=a[i][j]+b[i][j];
}

};

int main()
{
Matrix m;
m.menu();
}

INLINE FUNCTION

#include<iostream>
using namespace std;
inline int max(int a,int b)
{

return (a>b?a:b);
}

int main()
{
int a,b;
cout<<"Enter two numbers"<<endl;
cin>>a>>b;
cout<<(a>b?a:b);
return 0;
}

REFERENCE CALL

#include<iostream>
using namespace std;

class Operation
{
public:
void swap(int &x,int &y)
{
int temp=x;
x=y;
y=temp;
}
void swap1(int x,int y)
{
int temp=x;
x=y;
y=temp;
}
};

int main()
{
Operation p;
int a,b;
cout<<"Enter two numbers"<<endl;
cin>>a>>b;
cout<<"using call by reference"<<endl;
p.swap(a,b);
cout<<a<<endl;
cout<<b<<endl;
cout<<"using call by value"<<endl;
p.swap1(a,b);
cout<<a<<endl;
cout<<b<<endl;
}

DEFAULT ARGUMENT

#include<iostream>
using namespace std;

class Addition{
public:
void add(int ,int);

};
void Addition::add(int a,int b=5)
{
cout<<"sum is:"<<a+b;
}

int main()
{
Addition ad;
int a;
cout<<"enter no.: ";
cin>>a;
ad.add(a);

OVERLOADING FUNCTION
#include<iostream>
#include<process.h>
#define pi 3.14
using namespace std;

class Volume{
float area;
public:
int vol(int);
float vol(int,int);
int vol(int,int,int);
};

int Volume::vol(int a)
{
return a*a;
}
float Volume::vol(int r,int h)
{
float area;
area=pi*r*h;
return area;
}

int Volume::vol(int l,int b,int h)


{
return l*b*h;
}

int main()
{
Volume v;
int l,b,h,r,a,sw;
do
{
cout<<"\nMENU\n1.cube vol\n2.cylinder vol\n3.rect vol\n4.exit\nEnter
your choice:";
cin>>sw;

switch(sw)
{
case 1:
cout<<"side lenght: ";
cin>>a;
cout<<v.vol(a);
break;
case 2:
cout<<"lenght of r and h: ";
cin>>r>>h;
cout<<v.vol(r,h);
break;
case 3:
cout<<"lenght of l,b and h: ";
cin>>l>>b>>h;
cout<<v.vol(l,b,h);
break;
case 4:
exit(0);
}
}while(sw!=4);

QUEUE

#include<iostream>
#include<stdlib.h>
#define SIZE 5
using namespace std;
class queue
{
private:
int f,ch,i,a,r;
int queue[SIZE];
public:
int overflow();
int underflow();
void init();
void enq();
void deq();
void peek();
void display();
void setvalue();
};

void queue::setvalue()
{

cout<<"CHOICES:\n1)ENQUEUE\n2)DEQUEUE\n3)PEEK\n4)DISPLAY\n5)EXIT"<<endl;
do
{
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
enq();
break;
case 2:
deq();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
default:
cout<<"Invalid choice";
}
}while(1);
}
void queue::init()
{
f=-1;
r=-1;
}
int queue::underflow()
{
if(f==-1&&r==-1)
return 1;
else
return 0;
}
int queue::overflow()
{
if(r==SIZE-1)
return 1;
else
return 0;
}
void queue::enq()
{
if(overflow())
{
cout<<"Queue is full"<<endl;
return;
}
cout<<"Enter the element to be pushed: ";
cin>>a;
queue[++r]=a;
if(f==-1)
f=0;
}
void queue::deq()
{
if(underflow())
{
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Dequeue element is: "<<queue[f]<<endl;
queue[f++];
if(f==r)
f=r=-1;
}
void queue::peek()
{
if(underflow())
{
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Top element is: "<<queue[f]<<endl;

}
void queue::display()
{
if(underflow())
{
cout<<"Queue is empty"<<endl;
return;
}
for(i=f;i<=r;i++)
{
cout<<queue[i]<<endl;
}
}
int main()
{
queue s;
s.init();
s.setvalue();
}

PRIME NUMBER
OR NOT

#include<iostream>
using namespace std;

class Number
{
int n;
public:
void setValue();
void checkPrime();
};

void Number::setValue()
{
cout<<"Enter no.: ";
cin>>n;
}

void Number::checkPrime()
{
for(int i=2;i<n;i++)
if(n%i==0)
{
cout<<"not prime no.";
return;
}
cout<<"Prime no.";
return;

int main()
{
Number s;
s.setValue();
s.checkPrime();
}

ARRAY OF OBJECT - ITEMS AVG PRICE

#include<iostream>
using namespace std;
class Item
{
int id,price;
char name[100];

public:
void setValue()
{
cout<<"id : ";
cin>>id;
cout<<"name :";
fflush(stdin);
cin.getline(name,100);
cout<<"price : ";
cin>>price;
}

int getPrice()
{
return price;
}
};

int main()
{
Item t[20];
int n,total=0;
double avg;
cout<<" enter no of items : ";
cin>>n;
for(int i=0;i<n;i++)
{
t[i].setValue();
cout<<endl;
}

for(int i=0;i<n;i++)
total=total+t[i].getPrice();
avg=total/n;
cout<<"avg is : "<<avg;
}

ARRAY OF OBJECT--BOOKS DETAILS WHOSE PRICE> AVG

#include<iostream>
using namespace std;

class Book
{
int id,price;
char bname[50],aname[50];

public:
void setValue()
{
cout<<"id : ";
cin>>id;
cout<<" book name :";
fflush(stdin);
cin.getline(bname,50);
cout<<" Author name :";
fflush(stdin);
cin.getline(aname,50);
cout<<"price : ";
cin>>price;
}

void display()
{
cout<<id<<"\t"<<bname<<"\t"<<aname<<"\t"<<price<<endl;
}

int getPrice()
{
return price;
}
};

int main()
{
Book t[20];
int n,total=0;
double avg;
cout<<" enter no of items : ";
cin>>n;
for(int i=0;i<n;i++)
{
t[i].setValue();
cout<<endl;
}

for(int i=0;i<n;i++)
total=total+t[i].getPrice();
avg=total/n;
for(int i=0;i<n;i++)
{
if(t[i].getPrice()>avg)
t[i].display();
}
}

ARRAY
OF OBJECT -- PROJECT ON ITEMS-----------------------------------

#include<iostream>
#include<process.h>
#include<string.h>
using namespace std;

class Item
{

int id,price,sw;
char name[100];
string usn,pwd;
string pass="ishika";
string user="ishika";
public:
void login();
void menu();
void setValue();
int getPrice();
void display();
void displayMenu();
};

void Item::login()
{
cout <<"enter username: ";
getline(cin,usn);

cout<<"enter password: ";


getline(cin,pwd);

if((user.compare(usn)) && pwd.compare("ishika")) //return 0 when


equal
{
cout<<"\ninvalid username and password";
exit(0);
}
else
menu();

void Item::menu()
{
system("CLS");
while(1)
{
cout<<"\nMENU\n1.insert\n2.display\n3.exit\nenter your switch: ";
cin>>sw;

switch(sw)
{
case 1: setValue();
break;
case 2: displayMenu();
break;
case 3: exit(0);
}
}
}

void Item::setValue()
{
cout<<"id : ";
cin>>id;
cout<<"name :";
fflush(stdin);
cin.getline(name,100);
cout<<"price : ";
cin>>price;
}
int Item :: getPrice()
{
return price;
}

void Item::display()
{
cout<<"\t"<<id<<"\t"<<name<<"\t"<<price<<"\n";
}

void Item::displayMenu()
{
int x;
while(1)
{
system("CLS");
cout<<"\n1].display\n2].back\n3].exit\nenter your choice: ";
cin>>x;
switch(x)
{
case 1: display();
break;
case 2: menu();
break;
case 3: exit(0);
}
}
}

int main()
{
Item t[20];
t.login();
}

STATIC FUNCTION
TYPE ( static mamber function)

#include<iostream>
using namespace std;

class Student
{
static int id;
int rno;
static char clg[30];
char name[10],course[15];

public:
void setValue()
{
rno=++id;
cout<<"enter name: ";
cin>>name;
cout<<"enter course: ";
cin>>course;
}

void display()
{
cout<<endl<<"rno: "<<rno;
cout<<endl<<"name: "<<name;
cout<<endl<<"course: "<<course;
cout<<endl<<"clg: "<<clg;
}
};
int Student::id; //this
is necessary chahiye aapko value initial ni krni ho
char Student:: clg[]="Banasthali";
//if wanted to start id with zero still need to define it(write int
Student::id)

int main()
{
Student s[3];
for(int i=0;i<3;i++)
s[i].setValue();
for(int i=0;i<3;i++)
s[i].display();

cout<<Student::id; //error as id is in private session


part of Student
//declare it in public part first
return 0;
}

STATIC
MEMBER FUNCTION

#include<iostream>
using namespace std;

class Student
{
static int c;
public:
static void count()
{
c++;
}
static void display()
{
cout<<endl<<"count: "<<c;
}
};

int Student::c;
int main()
{
Student s;
Student::count();
Student::display();
Student::count();
Student::display();
Student::count();
Student::display();
}
quesssssssssssssssssssssssssssssssss

#include<iostream>
#include<string>
using namespace std;

class Employee
{
int id,salary;
string name,desi;

public:
void set_Value();
void display();

};
void Employee::set_Value()
{
cout<<"Enter employee id:";
cin>>id;
cout<<"Enter employee name:";
fflush(stdin);
getline(cin,name);
cout<<"Enter employee designation:";
fflush(stdin);
getline(cin,desi);
cout<<"Enter employee salary:";
cin>>salary;
}

void Employee::display()
{

STRING

#include<iostream>
#include<string>
using namespace std;

class Str
{
string s1;
int v=0;
int co=0;
public:
void count()
{

cout<<"enter string:";
fflush(stdin);
getline(cin,s1);
for(int i=-0;i<s1.length();i++)
{
char x=s1.at(i);
if(x=='a' || x=='e'|| x=='i' || x=='o' || x=='u')
v++;
else
co++;
}
cout<<v<<endl;
cout<<co;
}

};

int main()
{
Str s;
s.count();
}

FIND
FUNCTION OF STRING

int pos;
string s1="I am Ishika Goyal";
pos=s1.find("ooo");
cout<<pos;

REPLACE FUNCTION OF STRING

string s1="soosh";
s1.replace(1,2,"as");
cout<<s1;

NO OF
UPPER AND LOWER ELEMENT IN STRING

#include<iostream>
#include<string>
using namespace std;

class Str
{
string s1;
int v=0;
int co=0;
public:
void count()
{

cout<<"enter string:";
fflush(stdin);
getline(cin,s1);
for(int i=-0;i<s1.length();i++)
{
char x=s1.at(i);
if(x<='a' || x>='z')
v++;
else
co++;
}
cout<<v<<endl;
cout<<co;
}

};

int main()
{
Str s;
s.count();
}

STATIC ---CUBE OF NUMBER

#include<iostream>
using namespace std;

class Calculate
{
public:
static int cube(int x)
{
return x*x*x;
}
};

int main()
{
int c=Calculate::cube(3);
cout<<c;
}

PASSING OBJECT AS
PARAMETER----ADDITION OF TWO COMPLEX NUMBER

#include<iostream>
using namespace std;

class Complex
{
int x,y;
public:
void setValue();
void display();
void add(Complex,Complex);
};

void Complex::setValue()
{
cout<<"Enter the real part: ";
cin>>x;
cout<<"Enter the imag part: ";
cin>>y;
}

void Complex::display()
{
cout<<x<<"+"<<y<<"i"<<endl;
}

void Complex::add(Complex c1,Complex c2)


{
x=c1.x+c2.x;
y=c1.y+c2.y;
}

int main()
{
Complex c1,c2,c3;
c1.setValue();
c1.display();
c2.setValue();
c2.display();
c3.add(c1,c2);
cout<<"addition: \n";
c3.display();
}

RETURING OBJECT TO
CALLING FUNCTION---ADDD OF COMPLEX NO

#include<iostream>
using namespace std;

class Complex
{
int x,y;
public:
void setValue();
void display();
Complex add(Complex,Complex);
};

void Complex::setValue()
{
cout<<"Enter the real part: ";
cin>>x;
cout<<"Enter the imag part: ";
cin>>y;
}

void Complex::display()
{
cout<<x<<"+"<<y<<"i"<<endl;
}

Complex Complex::add(Complex c1,Complex c2)


{
Complex temp;
//x=c1.x+c2.x;
temp.x=c1.x+c2.x; //y=c1.y+ c2.y;
temp.y=c1.y+c2.y; //return temp;
return temp;
}

int main()
{
Complex c1,c2,c3,c4;
c1.setValue();
c1.display();
c2.setValue();
c2.display();
c4=c3.add(c1,c2);
cout<<"addition: \n";
c4.display();
}

FRIEND
FUNCTION

#include<iostream>
using namespace std;

class Maxim
{
int x,y,m;
public:
void setValue();
void display();
friend void max(Maxim &);
};

void Maxim::setValue()
{
cout<<"1st value: ";
cin>>x;
cout<<"2nd value: ";
cin>>y;
}

void max(Maxim &ma)


{
if(ma.x>ma.y)
ma.m=ma.x;
else
ma.m=ma.y;

void Maxim::display()
{
cout<<m<<"is largest\n";
}

int main()
{
Maxim i;
i.setValue();
max(i);
i.display();
}
// FRIEND
CLASS
#include<iostream>
using namespace std;
class A{
int x;
public:
A()
{
x=10;
}
friend class B;
};

class B
{
public:
void display(A &t)
{
cout<<"Value of x: "<<t.x;
}
};

int main()
{
A a1;
B b1;
b1.display(a1);
return 0;
}

METHOD 2

#include<iostream>
using namespace std;

class A{
int x;
public:
void read()
{
x=10;
}
friend class B;
};

class B
{
public:
void display(A &t)
{
cout<<"Value of x: "<<t.x;
}
};

int main()
{
A a1;
B b1;
a1.read();
b1.display(a1);
return 0;
}
//PARAMETERIZED
CONSTRUCTOR

#include<iostream>
using namespace std;

class student
{
int x,y;
public:
student(int ,int);
void display()
{
cout<<endl<<"x: "<<x<<" y: "<<y;
}
};

student::student(int x,int y)
{
this->x=x;
this->y=y;
}

int main()
{
student s(5,6);
s.display();
}

//DEFAULT CONSTRUCTOR

class student
{
int x;
public:
student();
void display()
{
cout<<endl<<"x: "<<x;
}
};

student::student()
{
cout<<"enter value of x: ";
cin>>x;
}

int main()
{
student s;
s.display();
}

-----------------------------------------------------------------------------------
-----------
#include<iostream>
using namespace std;

class student
{
int x;
public:
student();
void display()
{
cout<<endl<<"x: "<<x;
}
};

student::student()
{
cout<<"enter value of x: ";
cin>>x;
cout<<x;
}

int main()
{
student s;
cout<<"hiii";
s.display();
}

//PRIVATE DEFAULT
CONSTRUCTOR-------------------------------------------------------------
#include<iostream>
using namespace std;

class student
{
int x,y;
student()
{
cout<<"enter value of y: ";
cin>>y;

}
public:
student(int);
void display()
{
cout<<endl<<"x: "<<x<<" y:"<<y;
}
};

//student::student();

student::student(int x)
{
student();
this->x=x;
//cout<<y;

int main()
{
student s(7);
s.display();
}

//PRIVATE CONSTRUCTOR THROUGH FRIEND CLASS

#include<iostream>
using namespace std;

class student
{
int x,y;
student()
{
cout<<"constructor of a "<<endl;

}
public:
friend class b;
};

class b
{
public:
b()
{
student s;
cout<<"constructor of b"<<endl;
};

};

int main()
{
b b1;
return 0;
}

//PARAMETERIZED
AND DEFAULT CONSTRUCTOR TOGETHER

#include<iostream>
#include<string.h>
using namespace std;

class student
{
int rno,fee;
char name[50],cou[50];
public:
student(){};
student(int,char[],char[],int);
void display()
{
cout<<endl<<"rno: "<<rno<<" name: "<<name<<" course: "<<cou<<" fee:
"<<fee;
}

};

student::student(int rno,char n[],char c[],int fee)


{
this->rno=rno;
strcpy(name,n);
strcpy(cou,c);
this->fee=fee;
}

int main()
{
student s(1001,"Shyam","computer",30000);
s.display();
student s1;
}

-----------------------------------------------------------------------------------
---------------------------
DOUBTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT--------------------
#include<iostream>
using namespace std;

class student
{
int x,y;
student()
{
//cout<<"enter value of y: ";
//cin>>y;
y=10;

}
public:
student(int);
void display()
{
cout<<endl<<y;
//cout<<endl<<"x: "<<x<<" y:"<<y;
}
};

//student::student();

student::student(int x)
{
student();
//this->x=x;
//cout<<y;

int main()
{
student s(7);
s.display();
return 0;
}

#include<iostream>
using namespace std;

class student
{
int x,y;
student()
{
cout<<"enter value of y "<<endl;
cin>>y;

}
public:
//student(int);
friend class b;

void display()
{
cout<<endl<<"x: "<<x<<" y:"<<y;
}
};

class b
{
int x;
public:
b()
{
student s;
cout<<"enter value of x: "<<endl;
cin>>x;
s.display();
};
friend class student;

};

//cls'cstudent::student();

/*student::student(int x)
{
student();
this->x=x;
//cout<<y;
}*/

int main()
{
b b1;
return 0;

========================================COPY
CONSTRUCTOR========================================================================
======================

#include<iostream>
using namespace std;

class Sample
{
int id;
public:
void init(int x)
{
id=x;
}
void display()
{
cout<<endl<<"ID = "<<id;
}
};

int main()
{
Sample s1;
s1.init(5);
s1.display();

//s2=s1;
Sample s2(s1);
s2.display();
return 0;
}

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------------
#include<iostream>
#include<string.h>
using namespace std;

class Sample
{
int id,fee;
char name[50];
public:
void init(int x,int y,char a[])
{
id=x;
fee=y;
strcpy(name,a);
}
void display()
{
cout<<endl<<"ID = "<<id;
cout<<endl<<"FEE = "<<fee;
cout<<endl<<"NAME = "<<name;
}
};

int main()
{
Sample s1,s2;
//int a[7]={'i','s','h','i','k','a'};
s1.init(5,78,"ishika");
s1.display();

s2=s1; //Sample s2(s1);


IMPLICIT COPY CONSTRUCTOR
s2.display();
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------------------------------------------------------------------------

#include<iostream>
#include<string>
using namespace std;

class Sample
{
int id,fee,x,y;
static int count;
public:
Sample()
{
count++;
}
void init(int x,int y,int a,int b)
{

this->id=x;
this->fee=y;
this->x=a;
this->y=b;
}
void display()
{
cout<<endl<<endl<<"for "<<count<<" Smaple";
cout<<endl<<"ID = "<<id;
cout<<endl<<"FEE = "<<fee;
cout<<endl<<"x = "<<x;
cout<<endl<<"y = "<<y;
}
Sample(Sample &t)
{
id=t.id;
fee=t.fee;
}
void adisplay()
{
cout<<endl<<endl<<"for "<<count<<" Smaple";
cout<<endl<<"ID = "<<id;
cout<<endl<<"FEE = "<<fee;
cout<<endl<<"y = "<<y;
}
};

int Sample::count=0;

int main()
{
Sample s1;
int a[7]={'i','s','h','i','k','a'};
s1.init(5,78,3,9);
s1.display();
Sample s2;
s2=s1;
//Sample s2(s1); IMPLICIT COPY CONSTRUCTOR
s2.display();

Sample s3(s1);
//explicit me ese hi call krte h
// Sample s3;
// s3=s1;
s3.adisplay();
return 0;
}
OUTPUT-
for 1 Smaple
ID = 5
FEE = 78
x = 3
y = 9

for 2 Smaple
ID = 5
FEE = 78
x = 3
y = 9

for 2 Smaple
ID = 5
FEE = 78
y = 0

DESTRUCTOR

#include<iostream>
using namespace std;

class Test{
int count=0;
public:
Test()
{
count++;
cout<<endl<<count<<". Constructor called";
}
~Test()
{
cout<<endl<<count<<". Destructor called";
}

void display()
{
cout<<endl<<"YOU HAVE LEARNT";
}
};

int main()
{
Test t;
t.display();
}

Constructor called
YOU HAVE LEARNT
Destructor called
-----------------------------------------------------------------------------------
---------------------------------------------------------------------

#include<iostream>
using namespace std;

class Test{
static int count;
public:
Test()
{
count++;
cout<"\n"<<count<<". Constructor called";
}
~Test()
{
cout<<endl<<count<<". Destructor called";
count--;
}

void display()
{
cout<<endl<<"YOU HAVE LEARNT";
}
};

int Test::count;

int main()
{
Test t1,T2;
cout<<"\n hii"; // koii
mthod jise t2 useless destory ho jae plee--ni hota ese kabhi
Test t3;
t1.display();
t3.display();

}
1. Constructor called
2. Constructor called
hii
3. Constructor called
YOU HAVE LEARNT
YOU HAVE LEARNT
3. Destructor called
2. Destructor called
1. Destructor called

FUNCTION
OVERLOADING

1.) uniary through member function

#include<iostream>
using namespace std;

class Sample{
int x,y;6
public:
Sample(int x,int y)
{
this->x=x;
this->y=y;
}
void operator -();
void display();
};

void Sample::operator -()


{
x=-x;
y=-y;
}
void Sample::display()
{
cout<<"x: "<<x<<" y: "<<y<<endl;
}

int main()
{
Sample s(5,6);
s.display(); //x=5,y=6
-s;
s.display(); //x=-5,y=-6
return 0;
}

PREFIX INCREAMENT without


returing
#include<iostream>
using namespace std;

class Sample{
int x,y;
public:
Sample(int x,int y)
{
this->x=x;
this->y=y;
}
void operator ++();
void display();
};

void Sample::operator ++()


{
++x;
++y;
}
void Sample::display()
{
cout<<"x: "<<x<<" y: "<<y<<endl;
}

int main()
{
Sample s(5,56);
s.display();
++s;
s.display();
return 0;
}
prefix and postfix with
returning
#include<iostream>
using namespace std;

class Sample{
int x,y;
public:
Sample(){
}
Sample(int x,int y)
{
this->x=x;
this->y=y;
}
Sample operator ++();
Sample operator ++(int);
void display();
};

Sample Sample::operator ++()


{
++x;
++y;
return *this;
}
Sample Sample::operator ++(int)
{
x++;
y++;
return *this;
}
void Sample::display()
{
cout<<"x: "<<x<<" y: "<<y<<endl;
}

int main()
{
Sample s(5,56);
s.display();
++s;
Sample s1(89,23);
s1++;
s1.display();
return 0;
}

UNIARY FUNCTION THROUGH


FRIEND FUNCTION

#include<iostream>
using namespace std;

class Test
{
int x,y;
public:
Test(int x,int y)
{
this->x=x;
this->y=y;
}
void display();
friend void operator ++(Test &);
};

void Test:: display()


{
cout<<endl<<"x: "<<x<<" y: "<<y;
}
void operator ++(Test &t)
{
++t.x;
++t.y;
}

int main()
{
Test t1(89,23);
t1.display();
++t1;
t1.display();
}
BINARY OPERATOR THROUGH MEMEBER FUNCTION

#include<iostream>
using namespace std;

class Number
{
int a,b,c,d;
public:
Number(int,int,int,int);
void operator +(Number);
void display();
};
Number::Number(int a,int b,int c,int d)
{
this->a=a;
this->b=b;
this->c=c;
this->d=d;
}

void Number :: display()


{
cout<<endl<<"a: "<<a<<" b:"<<b<<" c:"<<c<<" d:"<<d;
}

void Number:: operator +(Number t)


{
a=a+t.a;
b=b+t.b;
c=c+t.c;
d=d+t.d;
}

int main(){
Number n1(45,7,1,2);
Number n2(12,4,8,0);
n1.display();
n2.display();
n1+n2;
n1.display();
}

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------
#include<iostream>
using namespace std;

class Number
{
int a,b,c,d;

public:
Number(int,int,int,int);
Number operator +(Number &);
void display();
};
Number::Number(int a,int b,int c,int d)
{
this->a=a;
this->b=b;
this->c=c;
this->d=d;
}

void Number :: display()


{
cout<<endl<<"a: "<<a<<" b:"<<b<<" c:"<<c<<" d:"<<d;
}

Number Number:: operator +(Number &t)


{
a=a+t.a;
b=b+t.b;
c=c+t.c;
d=d+t.d;
return *this;
}

int main(){
Number n1(45,7,1,2);
Number n2(12,4,8,0);
Number n3(2,5,9,1);
Number n4(1,2,3,4);
n1.display();
n2.display();
n3=n1+n2+n3+n4;
n3 .display();
}

3*3 matrix
#include<iostream>
using namespace std;

class Matrix
{
int a[3][3];
public:
Matrix()
{
int i,j;
printf("Enter the elements of 3*3 matrix: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
}

void display();
void operator*(Matrix);
};
void Matrix:: display()
{
int i,j;
printf("The elements of 3*3 matrix: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d\t\t",a[i][j]);
printf("\n");
}
printf("\n\n\n");
}
void Matrix::operator*(Matrix m)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
a[i][j]=a[i][j]*m.a[i][j];
}
}

int main()
{
Matrix m1,m2;
m1.display();
m2.display();
m1*m2;
m1.display();
return 0;

STACK
THROUGH FUNCTION OVERLOADING

#include<iostream>
using namespace std;

class Stack{
int top,a[10],i;
public:
Stack()
{
top=-1;
}
void operator +(int);
int operator -();
void display();
};
void Stack::display()
{
for(i=top;i>=0;i--)
{
printf("%d\n",a[i]);
}
printf("\n");
}

void Stack::operator +(int x)


{
a[++top]=x;
}
int Stack::operator -()
{
int x=a[top];
top--;
return x;
}

int main()
{
Stack s;
int x;
s+1;
s+2;
s+3;
s.display();
x=-s;
s.display();

BINARY
THROUGH FRIEND FUNCTION AND RETURNING SOMETHING

#include<iostream>
using namespace std;

class Number{
int x,y;
public:
Number(int,int);
void display();
//friend void operator +(Number &,Number &);
friend Number operator +(Number,Number);
};

Number::Number(int a,int b)
{
x=a;
y=b;
}
void Number::display()
{
cout<<endl<<"x: "<<x<<" y: "<<y;
}

//void operator +(Number &n1,Number &n2)


//WORKEDD
//{
// n1.x=n1.x+n2.x;
// n1.y=n1.y+n2.y;
//}

Number operator +(Number n1,Number n2)


//RETURNING WORKING
{
n1.x=n1.x+n2.x;
n1.y=n1.y+n2.y;
return n1;
}

int main()
{
Number n1(34,67);
Number n2(1,2);
n1.display();
n2.display();
n1=n1+n2;
n1.display();
return 0;

() OPERATOR THROUGH OVERLOADING

#include<iostream>
using namespace std;

class test
{
int a,b;
public:
void display();
void operator ()(int ,int );
};

void test::display()
{
cout<<endl<<"a: "<<a<<" b: "<<b;
}
void test::operator()(int x,int y)
{
a=x;
b=y;
}

int main()
{
test t1;
t1(10,20);
t1.display();
}

[] OPERATOR THROUGH OVERLOADING

#include<iostream>
using namespace std;

class test
{
int a[10];
public:
int n;
test()
{
cout<<"Enter size of array:";
cin>>n;
}
void setValue(){
int i;
for(i=0;i<n;i++)
cin>>a[i];
}

int operator [](int i)


{
return a[i];
}
};

int main()
{
test t;
t.setValue();
for(int i=0;i<t.n;i++)
cout<<"\t"<<t[i];
}

CASCADING THE STREAM INSERTION OPERATOR(<<)

#include<iostream>
using namespace std;

class Employee
{
int eid;
string name;

public:
Employee(int eid,string name)
{
this->eid=eid;
this->name=name;
}

friend ostream & operator<<(ostream &,Employee);


};

ostream & operator <<(ostream & ram,Employee e)


{
ram<<e.eid<<"\t"<<e.name;
return ram;
}

int main()
{
Employee ee(1001,"ishika");
cout<<ee;
return 0;
}

CASCADING THE STREAM INSERTION OPERATOR(>>)

#include<iostream>
using namespace std;

class Employee
{
int eid;
string name;

public:

friend istream &operator>>(istream &,Employee &);


void display()
{
cout<<"\t"<<"eid: "<<eid<<" name: "<<name;
}
};

istream& operator>>(istream & in,Employee &e)


{
cout<<"Enter eid: ";
in>>e.eid;
cout<<"Enter name: ";
in>>e.name;
return in;
}

int main()
{
Employee ee;
cin>>ee;
ee.display();
return 0;
}

INHERITANCE

#include<iostream>
using namespace std;

class Person
{
protected:
int id;
char name[100],add[100];
char gender;

};

class Employee: public Person


{
int salary;
char desgn[100];
public:
void readData()
{
cout<<"Enter id: ";
cin>>id;
cout<<"Enter name: ";
cin>>name;
cout<<"Enter address: ";
cin>>add;
cout<<"Enter salary: ";
cin>>salary;
cout<<"Enter designation";
cin>>desgn;
}

void display()
{
cout<<endl<<"id: "<<id<<"\nname: "<<name<<"\naddress: "<<add<<"\
nsalary: "<<salary<<"\ndesignation: "<<desgn;
}
};
int main()
{
Employee
e; //object of
derived class
e.readData(); //derived class object invoke derived
class member function
e.display();
return 0;
}

-----------------------------------------------------------------------------------
---------------------------------------PARATMETERIZED
INHERTIANCE------------------------------------------------------------------------
-------------------

#include<iostream>
#include<string.h>
using namespace std;

class Person
{
protected:
int id;
char name[100],add[100];
char gender;
void read(int id,char n[],char ad[])
{
this->id=id;
strcpy(name,n);
strcpy(add,ad);
}

};

class Employee: public Person


{
int salary;
char desgn[100];
public:
void readData(int id,char n[], char add[],int s,char d[])
{
read(id,n,add);
salary=s;
strcpy(desgn,d);
}
void display()
{
cout<<endl<<"id: "<<id<<"\nname: "<<name<<"\naddress: "<<add<<"\
nsalary: "<<salary<<"\ndesignation: "<<desgn;
}
};
int main()
{
Employee e;
e.readData(45,"ISHIKA","TONK",345,"MANAGER");
e.display();
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------MULTILEVEL
INHERITANCE------------------------------------------------------------------------
---------------------------
#include<iostream>
using namespace std;

class A{
protected:
int a;
public:
void setA()
{
cout<<endl<<"Enter value of a :";
cin>>a;
}
void dispA(){
cout<<"value of a: "<<a;
}
};

class B: public A
{
protected:
int b;
public:
void setB()
{
cout<<endl<<"Enter value of b: ";
cin>>b;
}
void dispB()
{
cout<<endl<<"value of b: "<<b;
}
};

class C: public B
{
protected:
int c,p;
public:
void setC()
{
cout<<endl<<"Enter the value of c: ";
cin>>c;
}

void dispC()
{
cout<<endl<<"value of c: "<<c;
}

void cal_product()
{
p=a*b*c;
cout<<endl<<"product of a,b and c: "<<p;
}
};

int main()
{
C c;
c.setA();
c.dispA();
c.setB();
c.dispB();
c.setC();
c.dispC();
c.cal_product();
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------
#include<iostream>
using namespace std;

class A{
protected:
int a;
public:
void setA()
{
cout<<endl<<"Enter value of a :";
cin>>a;
}
void dispA(){
cout<<"value of a: "<<a;
}
};

class B: public A
{
protected:
int b;
public:
void setB()
{
cout<<endl<<"Enter value of b: ";
cin>>b;
}
void dispB()
{
cout<<"value of b: "<<b;
}
};

class C: public B
{
protected:
int c,p;
public:
void setC()
{
cout<<endl<<"Enter the value of c: ";
cin>>c;
}

void dispC()
{
cout<<endl<<"value of c: "<<c;
}

void cal_product()
{
p=a*b*c;
cout<<endl<<"a: "<<a<<" b: "<<b<<" c: "<<c;
//ye glt h tarika
cout<<endl<<"product of a,b and c: "<<p;
}
};

int main()
{
A a;
B b;
C c;
a.setA();
a.dispA();
b.setB();
b.dispB();
c.setC();
c.dispC();
c.cal_product();
}

-----------------------------------------------------------------------------------
-----------------------------------
STAFF------------------------------------------------------------------------------
--------------------
#include<iostream>
#include<process.h>
#include<string.H>
using namespace std;

class Staff
{
int id,salary;
char name[100],depa[100],des[100];
public:
void setValue()
{
cout<<endl<<"Enter id: ";
cin>>id;
cout<<"Enter name: ";
cin>>name;
cout<<"Enter department: ";
cin>>depa;
cout<<"Enter designation: ";
cin>>des;
cout<<"Enter salary: ";
cin>>salary;
cout<<endl;
}
void display()
{
cout<<endl<<"id: "<<id<<"\tname: "<<name<<"\tdepartment:
"<<depa<<"\tdesignation"<<des<<"\tsalary: "<<salary;
}

void sort(int n,Staff x[])


{
Staff temp;
for(int i=0;i<(n-1);i++)
for(int j=i+1;j<n;j++)
if(strcmp(x[i].name,x[j].name)>0)
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
};

int main()
{
Staff s[10];
int ch,i;
i=0;
do
{
cout<<"MENU\n1.) insert information\n2.)display record\n3.)sort record
by name\n4.)exit\nEnter your choice:\n\n\n";
cin>>ch;

switch(ch)
{
case 1: cout<<"Enter record of employee:\n";
s[i].setValue();
i++;
break;

case 2: cout<<"\n\t\tRECORDS OF EMPLOYEES\n";


for(int j=0;j<i;j++)
s[j].display();
break;

case 3: cout<<"sorted accordingly names\n";


s[0].sort(i,s);

for(int j=0;j<i;j++)
s[j].display();
break;

case 4: cout<<"exited";
exit(0);
default: cout<<"Enter correct choice \n";
}
}while(ch!=4);
return 0;
}

---------------------------------------------------------------------------------
STUDENT-RESULT
INHERITANCE------------------------------------------------------------------------
--------------------
#include<iostream>
using namespace std;

class Student
{
protected:
int rno,marks1,marks2,marks3;
char name[20],branch[20];
public:
void setValue()
{
cout<<"Enter roll no: ";
cin>>rno;
cout<"Enter name: ";
cin>>name;
cout<<"Enter branch: ";
cin>>branch;
cout<<"Enter marks of 1st subject: ";
cin>>marks1;
cout<<"Enter marks of 2nd subject: ";
cin>>marks2;
cout<<"Enter marks of 3rd subject: ";
cin>>marks3;
}

void disp()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<branch<<"\t"<<marks1<<"\
t"<<marks2<<"\t"<<marks3;
}

};

class Result: public Student


{
int tmarks,avMarks;
public:
void totalMarks()
{
tmarks=marks1+marks2+marks3;
}
void avgMarks()
{
avMarks=(marks1+marks2+marks3)/3;
}
void display()
{
disp();
cout<<"\t"<<"total marks: "<<tmarks<<"\t"<<" averge marks:
"<<avMarks<<endl;
}
};

int main()
{
Result r;
r.setValue();
r.avgMarks();
r.totalMarks();
r.display();
}

FUNCTION OVERRIDDING

#include<iostream>
using namespace std;

class XYZ
{
public:
void display()
{
cout<<"\nI am in class XYZ";
}
};
class ABC: public XYZ
{
public:
void display()
{
//XYZ::DISPLAY()
cout<<"\nI am in class ABC";
}
};

int main()
{
ABC a;
a.display();
// XYZ x;
// x.display();
a.XYZ::display();
return 0;
}

LINEAR SEARCH

#include<iostream>
using namespace std;

class Linear
{
private:
int *a=new int[100];
int n,x,flag=0,f;
public:
Linear(int n,int x)
{
cout<<"Enter elements of array: \n";
for(int i=0;i<n;i++)
cin>>a[i];
this->n=n;
this->x=x;
}

void search()
{
for(int i=0;i<n;i++)
{
if(x==a[i])
{
flag=1;
f=i;
}
}
}

void ans()
{
if(flag==0)
cout<<"\nElement do not found";
else
{
cout<<"Element is founded at "<<f+1<< "th position";
}
}
};

int main()
{
Linear l(5,7);
l.search();
l.ans();
}

inter , extra------------
#include<iostream>
using namespace std;
class Student
{
protected:
int rno;
char branch[10];
public:
void setValue()
{
cout<<"Enter Roll number: ";
cin>>rno;
cout<<"Enter branch: ";
cin>>branch;
}
void display()
{
cout<<"\troll no: "<<rno<<"\tbranch: "<<branch;
}
};

class Internal:virtual public Student


{
protected:
int Is1,Is2;
public:
void setValue()
{
// Student::setValue();
cout<<"Enter marks for 1st: ";
cin>>Is1;
cout<<"Enter marks for 2nd: ";
cin>>Is2;
}

void display()
{
cout<<"\t Internal Marks1: "<<Is1<<"\t Internal Marks2: "<<Is2;
}
};

class External: virtual public Student


{
protected:
int Es1;
public:
void setValue()
{
// Student::setValue();
cout<<"Enter External marks: ";
cin>>Es1;
}
void display()
{
cout<<"\t External Marks1: "<<Es1;
}
};

class Result: public External,public Internal


{
int total;
public:
void tTotal()
{
Student::setValue();
Internal::setValue();
External::setValue();
total=Is1 + Is2 + Es1;
}
void display()
{
Student::display();
Internal::display();
External::display();
cout<<"\t Total marks: "<<total;
}
};

int main()
{
Result r;
r.tTotal();
r.display();
return 0;
}

STUDENT->MARKS->RESULT

#include<iostream>
using namespace std;

class Student
{
protected:
int rno,age;
char name[20],sec;
public:
void setValue()
{
cout<<"Enter Roll number: ";
cin>>rno;
cout<<"Enter name: ";
fflush(stdin);
cin.getline(name,20);
cout<<"Enter Age: ";
cin>>age;
cout<<"Enter Section: ";
cin>>sec;

}
void display()
{
cout<<"\troll no: "<<rno<<"\tname: "<<name<<"\tAge: "<<age<<"\
tSection: "<<sec;
}
};

class Marks:public Student


{
protected:
int s1,s2,s3;
public:
void setValue()
{
Student::setValue();
cout<<"Enter marks for 1st: ";
cin>>s1;
cout<<"Enter marks for 2nd: ";
cin>>s2;
cout<<"Enter marks for 3rd: ";
cin>>s3;
}

void display()
{
Student::display();
cout<<"\t Marks1: "<<s1<<"\t Marks2: "<<s2<<"\t Marks3: "<<s3;
}
};

class Result: public Marks


{
int ttl,avg;
public:
void setValue()
{
Marks::setValue();
ttl=s1+s2+s3;
avg=ttl/3;
}

void display()
{
Marks::display();
cout<<"\tTotal Marks: "<<ttl<<" \tAverge Marks: "<<avg;
}
};

int main()
{
Result r;
r.setValue();
r.display();
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------

INHERITANCE CONSTUCTOR

DAFAULT CONSTRUCTOR
#include<iostream>
using namespace std;

class Student
{
protected:
int rno,age;
char name[20],sec;
public:
Student()
{
cout<<"Enter Roll number: ";
cin>>rno;
cout<<"Enter name: ";
fflush(stdin);
cin.getline(name,20);
cout<<"Enter Age: ";
cin>>age;
cout<<"Enter Section: ";
cin>>sec;

}
void display()
{
cout<<"\troll no: "<<rno<<"\tname: "<<name<<"\tAge: "<<age<<"\
tSection: "<<sec;
}
};

class Marks:public Student


{
protected:
int s1,s2,s3;
public:
Marks():Student()
//or just simply write Marks()
{
//Student();
cout<<"Enter marks for 1st: ";
cin>>s1;
cout<<"Enter marks for 2nd: ";
cin>>s2;
cout<<"Enter marks for 3rd: ";
cin>>s3;
}

void display()
{
Student::display();
cout<<"\t Marks1: "<<s1<<"\t Marks2: "<<s2<<"\t Marks3: "<<s3;
}
};

int main()
{
Marks m;
m.display();
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------------------

PARAMETERIZED

#include<iostream>
#include<string.h>
using namespace std;

class Student
{
protected:
int rno,age;
char name[20],sec;
public:
Student(int x,int y,char sec,char *n)
{
rno=x;
age=y;
strcpy(name,n);
this->sec=sec;
}
void display()
{
cout<<"\troll no: "<<rno<<"\tname: "<<name<<"\tAge: "<<age<<"\
tSection: "<<sec;
}
};

class Marks:public Student


{
protected:
int s1,s2,s3;
public:
Marks(int x,int y,char sec,char *n,int m1,int m2,int m3) :
Student(x,y,sec,n)
{
s1=m1;
s2=m2;
s3=m3;
}

void display()
{
Student::display();
cout<<"\t Marks1: "<<s1<<"\t Marks2: "<<s2<<"\t Marks3: "<<s3;
}
};

int main()
{
Marks m(2,20,'A',"Tanisha",23,56,7);
m.display();
}

DYNAMIC MEMORY ALLOCATION

#include<iostream>
using namespace std;

class stu
{
int x,y=10;
public:
stu(int a)
{
x=a;
}
int sum()
{
return x+y;
}
};

int main()
{
stu* s=new stu(10);
//int x=
cout<<"sum: "<<s->sum();
}
----------------------------------------------------
#include<iostream>
using namespace std;

class stu
{
char name[20];
public:
void set()
{
cout<<"Enter name: ";
fflush(stdin);
cin.getline(name,20);
}
void display()
{
cout<<"\n"<<name;
}
};

int main()
{
int n;
stu* s=new stu[10];
cout<<"Enter no. student: ";
cin>>n;

for(int i=0;i<n;i++)
s[i].set();

for(int i=0;i<n;i++)
s[i].display();
}

---------------------------------------------------------linear search using new


operator
#include<iostream>
using namespace std;

class linear
{
int *a,flag,i,x,n;
public:
linear()
{
flag=0;
}

void set(int n,int x)


{
a=new int[n];
for(i-0;i<n;i++)
cin>>a[i];

this->x=x;
this->n=n;
}

int search()
{
for(i=0;i<n;i++)
{
if(x==a[i])
{
flag=1;
break;
}
}
return flag;
delete[] a;
}
~linear()
{

}
};
int main()
{
linear *l=new linear;
l->set(5,6);
int x=l->search();
if(x==1)
cout<<"found";
else
cout<<"not found";
delete l;
}

---------------------------------------------------------------------------4*4
Matrix min element

#include<iostream>
using namespace std; //not working

class matrix
{
int **a,flag,i,x,n;
public:
matrix(int n)
{
this->n=n;
*a=new int[n];
for(i=0;i<n;i++)
a[i]=new int[n];
}

void set()
{
cout<<"Enter elements: ";

for(i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
}

int search()
{
int min=a[0][0];
for(i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]<min)
min=a[i][j];
}
}
return min;
delete[] a;
}
~matrix()
{

}
};
int main()
{
matrix *l=new matrix(3);
l->set();
cout<<"MIN: "<<l->search();
delete l;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------

TYPECASTING

#include<iostream>
using namespace std;

class A
{
public:
void display()
{
cout<<"\nI am in class A";
}
};

class B: public A
{
public:
void display()
{
cout<<"\nI am in class B";
}
};

int main()
{
B b; // B *btr=&b
; btr->display // I AM IN CLASS B
A a; //// B
*btr=&a //SHOW ERROR
A *atr;
atr=&b;
a.display();
atr->display(); //I AM IN
CLASS A
}

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------VIRTUAL FUNCTION

#include<iostream>
using namespace std;

class A
{
public:
void virtual display()
{
cout<<"\nI am in class A";
}
};

class B: public A
{
public:
void display()
{
cout<<"\nI am in class B";
}
};

int main()
{
B b;
A a;
A *atr;
atr=&b;
a.display();
atr->display();
}

-----------------------------------------------------------------------------------
------------------------------------------------PURE VIRTUAL FUNCTION

#include<iostream>
using namespace std;
class A
{
public:
void virtual display( )=0; // virtual function

};
class B: public A
{
public:
void display( ) // Overriding function
{
cout<<"Manjeet loves Massala Dosa";
}
};
main()
{
B b; //derived class object
A *aptr; //Base class pointer
aptr=&b; // base class Pointer pointing to derived class objects
aptr->display(); // calls derived class "display()" function
return 0;
}

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

TEMPLATE

#include<iostream>
using namespace std;

template<class T>

void display(T a,T b)


{
cout<<"\n"<<a<<"\t"<<b;
}

void display(int a,int b)


{
cout<<"\n normal "<<a<<"\t"<<b;
}

int main()
{
display(10,29);
display(2.2,5.8);
display('a','b'); //b
display("ishika","tanisha"); //ishika
}

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------------------------------------------
SUM USING CLASS TEMPLATE
#include<iostream>
using namespace std;

template <class T>


void sum(T x,T y)
{
cout<<x<<" "<<" + "<<y<<"="<<x+y<<endl;
}

int main()
{
sum(10,20);
sum(13.6,34.7);
sum('a','c'); 196
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------

MAX USING CLASS TEMPLATE

#include<iostream>
using namespace std;

template <class T>


void maxi(T x,T y)
{
cout<<"max is: ";
cout<<(x>y?x: y);
cout<<endl;
}

int main()
{
maxi(10,20);
maxi(13.6,34.7);
maxi('a','c');
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------------------

SELECTION SORT

#include<iostream>
using namespace std;
template <class man>
void ssort(int n, man x[] )
{
for(int i=0;i<n-1;i++)
{
int min= i;
for(int j=i+1;j<n;j++)
{
if(x[min]>x[j])
min=j;
}
if(min!=i)
{
man temp=x[i];
x[i]=x[min];
x[min]=temp;
}
}
}

int main()
{
int x[]={1,2,3,4,5};
double y[]={1.2,4.5,7.3};
ssort(5,x);
for(int i=0;i<5;i++)
cout<<x[i]<<" ";
cout<<endl;
ssort(3,y);
for(int i=0;i<3;i++)
cout<<y[i]<<" ";
cout<<endl;
}
OR

#include<iostream>
using namespace std;

template<class t>

void ssort(t a[],int n)


{
int min,j;
for(int i=0;i<(n-1);i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[min]>a[j])
min=j;
}
if(min!=i)
{
t temp=a[min];
a[min]=a[i];
a[i]=temp;
}
}
}
template<class t>
void display(t ar[],int n)
{
for(int i=0;i<n;i++)
cout<<"\t"<<ar[i];
}

int main()
{
int n,arr[20];
cout<<"Enter size: ";
cin>>n;

cout<<"Enter elements: ";


for(int i=0;i<n;i++)
cin>>arr[i];

ssort(arr,n);
display(arr,n);
cout<<"\n";

float ar[20];
cout<<"Enter size: ";
cin>>n;

cout<<"Enter elements: ";


for(int i=0;i<n;i++)
cin>>ar[i];

ssort(ar,n);
display(ar,n);
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------INCOMPLETE STACK CLASS USING CLASS TEMPLATE

STACK USING CLASS TEMPLATE


#include<iostream>
using namespace std;

template <class T>


class Stack
{
int top;
T a[10];

public:
Stack()
{
top=-1;
}

void push(T x)
{
if(top>10)
cout<<endl<<"full space!!!!";
else
{
a[++top]=x;
}
}

T pop()
{
if(top==-1)
{
cout<<"empty!!!";
return -1;
}
return a[top--];
}

T peek()
{
return a[top];
}

void display()
{
cout<<endl;
for(int i=top;i>=0;i--)
cout<<"\n"<<a[i]<<" ";
}
};

int main()
{
int sw,v;
Stack<int> a;
do
{
cout<<"\nmenu\n1.push\n2.pop\n3.peek\n4.display\n5.exit";
cout<<"\nenter your choice:";
cin>>sw;
switch(sw)
{
case 1:
cout<<"enter value: ";
cin>>v;
a.push(v);
break;

case 2:
v=a.pop();
if(v!=-1)
cout<<"poped element: "<<v;
break;

case 3:
v=a.peek();
cout<<"Peak element: "<<v;
break;

case 4:
a.display();
break;

case 5:
exit(0);

default: cout<<"invalid choice!";

}
}while(sw!=5);
return 0;
}

ofstream->write in file (out mode) std::cout (have


default mode)
ifstream->read in file (have
default mode)

fstream->both (do not default mode)

ofstream fout("emp txt",std::cout) or ofstream fout("emp txt")

to read data--->
int id,string ename,int salary;
ifstream fin("e:/emp.txt");
fin>>id<<enmae>>salary; //left to
right
fin.close();

//we must know the datatype of coming data like int

---------------=-------------------------------------------------------------
#include<iostream>
using namespace std;

template <class T>


class Stack
{
int top;
T a[10];

public:
T v;
Stack()
{
top=-1;
}

void push(T x)
{
if(top>10)
cout<<endl<<"full space!!!!";
else
{
a[++top]=x;
}
}

T pop()
{
if(top==-1)
{
cout<<"empty!!!";
return -1;
}
return a[top--];
}

T peek()
{
return a[top];
}

void display()
{
cout<<endl;
for(int i=top;i>=0;i--)
cout<<"\n"<<a[i]<<" ";
}

void getdata()
{
cout<<"enter value: ";
cin>>v;
}
};

void swi(Stack a)
{
do
{
cout<<"\nmenu\n1.push\n2.pop\n3.peek\n4.display\n5.exit";
cout<<"\nenter your choice:";
cin>>sw;
switch(sw)
{
case 1:

a.push(a.v);
break;

case 2:
cout<<"poped element: "<<a.pop();;
break;

case 3:
cout<<"Peak element: "<<a.peek();
break;

case 4:
a.display();
break;

case 5:
exit(0);

default: cout<<"invalid choice!";

}
}while(sw!=5);
}
int main()
{
int sw,v;
cout<<"\nmenu\n1.for int\n2.for char\n3.for double\n4.exit\nenter your
choice: ";
cin>>sw;
switch(sw)
{
//int v
case 1: Stack<int> s;
swi(s);
break;

case 2: Stack<char> p;
swi(p);
break;

case 3: Stack<double> d;
swi(d);
break;

case 4:
exit(0);

default: cout<<"invalid choice!";

}
Stack<int> a;

return 0;
}
//192.168.22.199 mount D: "D:\"

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------------FILE HANDLING
write only
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
int n;
int rno,fee;
string name,course;

cout<<"Enter rno: ";


cin>>rno;
cout<<"Enter name: ";
cin>>name;
cout<<"Enter fee: ";
cin>>fee;
cout<<"Enter course: ";
cin>>course;

ofstream fout("d:/1stfile.txt",ios::app);
fout<<rno<<"\t"<<name<<"\t"<<fee<<"\t"<<course;
fout.close();
}

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
int rno,fee;
string name,course;

ifstream fin("d:/1stfile.txt");
fin>>rno>>name>>fee>>course;
fin.close();

cout<<endl<<rno<<"\t"<<name<<"\t"<<fee<<"\t"<<course;
}
BOTH

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
int rno,fee,r,f;
string name,n;

cout<<"Enter rno: ";


cin>>rno;
cout<<"Enter name: ";
cin>>name;
cout<<"Enter fee: ";
cin>>fee;

ofstream fout("d:/new_file.txt" ,ios::app);


fout<<rno<<"\t"<<name<<"\t"<<fee;
fout.close();

ifstream fin("d:/new_file.txt");
fin>>r>>n>>f;
fin.close();

cout<<endl<<r<<"\t"<<n<<"\t"<<f;

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string loc;
cout<<"enter location: ";
cin>>loc;

ofstream fout;

fout.open("d:/1stfile.txt",ios::app);
fout<<"\t"<<loc;
fout.close();
fout.open("d:/new_file.txt",ios::app);
fout<<"\t"<<loc;
fout.close();

RECORD
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

void write(int n)
{
int eid,sal;
string name;

for(int i=0;i<n;i++)
{
cout<<"Enter eid: ";
cin>>eid;
cout<<"Enter name: ";
cin>>name;
cout<<"Enter salary: ";
cin>>sal;

fstream fout("d:/record.txt",ios::out | ios::app);


fout<<eid<<"\t"<<name<<"\t"<<sal<<endl;
fout.close();
}
}

void read()
{
string rec;
ifstream fin("d:/record.txt");
while(getline(fin,rec))
cout<<rec<<endl;
fin.close();
}

int main()
{
int n;
cout<<"Enter no. of record: ";
cin>>n;
write(n);
read();
return 0;
}

EOF CHARACTER BY CHARATER

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
string line;
cout<<"Enter the string: ";
//getline(cin,line);
cin>>line;

fstream fout;
fout.open("d:/strin.txt",ios::out);

for(int i=0;line[i]!='\0';i++)
fout.put(line[i]);
fout.close();

fstream file;
file.open("d:/strin.txt",ios::in) ;
while(file.eof()==0)
{
char c=file.get();
cout<<c;
}
file.close();
}

-------------------------------------------------END OF
FI;E-------------------------
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
int n;
cout<<"Enter no. of student: ";
cin>>n;
int id,marks;
for(int i=0;i<n;i++)
{
cout<<"Enter id";
cin>>id;
cout<<"Enter marks: ";
cin>>marks;

ofstream fout;
fout.open("d:/endOfFile",ios::app);
fout<<id<<"\t\t"<<marks<<endl;
fout.close();
}
string irecord;
ifstream fin("d:/endOfFile");
while(getline(fin,irecord))
{
cout<<irecord<<endl;
}

while(fin.eof()==0)
cout<<fin.eof();
cout<<1;

return 0;
}
001111 end of file
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
fstream file;
int n=5;
string name="ishika";
file.open("d:/ishika.txt",ios::out);
file<<n<<"\t"<<name;
file.close();

string val;
file.open("d:/ishika.txt" ,ios::in);
while(file.eof()==0)
{
val=file.get();
cout<<file.eof();
}
//cout<<1;
file.close();
}

-------------------------------------------------------LINE AND WORD

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
int n=5;
int word=0,line=0;
fstream file;
file.open("d:/my.txt",ios::out);
file<<n<<"\t"<<n<<"\n"<<n;
file.close();

file.open("d:/my.txt",ios::in);
while(file.eof()==0)
{
int r=file.get();
if(r=='\t')
word++; //idhr hum cout<<r ni
likh skte as r include space
if(r=='\n') //so this will print ascii
value of \n and \t
line++; //
}
cout<<"words: "<<word<<" lines: "<<line;
}

------------------------------------------------------------words and lines on


string character by character
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
//int n=5;
string s;
fflush(stdin);
getline(cin,s);
int word=0,line=0;
fstream file;
file.open("d:/my.txt",ios::out);
//file<<s<<"\n"<<s; //word
0 and line 1
for(int i=0;s[i]!='\0';i++)
file.put(s[i]);
file.close();

file.open("d:/my.txt",ios::in);
while(file.eof()==0)
{
char r=file.get();
if(r==' ')
word++; //idhr hum cout<<r ni
likh skte as r include space
if(r=='\n') //so this will print ascii
value of \n and \t
line++;
}
cout<<"words: "<<word<<" lines: "<<line;
}

----------------------------------------------------------alphbats,digits and
space------------------------------------------------------------------------------
---------------
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
int a=0,s=0,d=0;
fstream file;
file.open("d:/record.txt",ios::in);
while(file.eof()==0)
{
char v=file.get();
int i=v;

if((i>=65 && i<=90) || (i<=97 && i<122))


a++;
<
if(i>=47 && i<=58)
d++;

if(v=='\t')
s++;
cout<<v;
}
cout<<"\n alph "<<a<<" digits: "<<d<<" space: "<<s;

file.close();
}

----------------------------------------------------------------ONE TO ANOTHER FILE

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
int n=5;
fstream f;
// f.open("d:/record.txt",ios::out);
// f<<n<<"\t"<<n;
// f.close();

f.open("d:/record.txt",ios::in);
fstream file;
file.open("d:/radhe.txt",ios::out);

while(f.eof()==0)
{
char ch=f.get();
//f>>ch;
file<<ch;
}
f.close();
file.close();

//f.open()
}

seekg
#include<iostream>
#include<fstream> //not
working correctly
using namespace std;

int main()
{
char s[100];
cout<<"enter string: ";
//fflush(stdin);
cin.getline(s,100);
int i;
fstream f;

f.open("d:/learing.txt",ios::out|ios::app|ios::ate);
for(i=0;s[i]!='\0';i++)
f.put(s[i]);
f.close();

f.open("d:/learing.txt",ios::in);
f.seekg(3,ios::beg);
while(f.eof()==0)
{
char c=f.get();
cout<<c;
}
f.close();
return 0;
}
no. of bytes/ tellg

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
fstream f;
f.open("d:/ishika.txt",ios::app| ios::ate|ios::in);
int x=f.tellg();
cout<<"no. of bytes: "<<x;
return 0;
}

//NOT WORKING (
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
fstream f;
f.open("d:/ishika.txt",ios::in|ios::ate);
int x=f.tellg();
cout<<"no. of bytes: "<<x;
f.close();

f.open("d:/ishika.txt",ios::out);
f.seekp(3,ios::beg);
f<<"so what?";
f.close();

cout<<"\n";
f.open("d:/ishika.txt",ios::in);
f.seekg(0);
while(f.eof()==0)
{
char c=f.get();
cout<<c;
}
return 0;
}

REVERSE
PRINT OF FILE

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
char a[20],i;
cout<<"Enter the String: ";
fflush(stdin);
cin.getline(a,20);

fstream file;
file.open("d:/rev.txt",ios::out);

for(i=0;a[i]!='\0';i++)
file.put(a[i]);

int x=file.tellp();
file.close();

file.open("d:/rev.txt",ios::in);
for(i=1;i<x+1;i++)
{
file.seekg(-i,ios::end);
char c=file.get();
cout<<c;
}
file.close();
return 0;
}

WRITE()
READ() FUNCTION

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
int rno,fee,r,ee;
string name,course,n,c;

cout<<"Enter rno: ";


cin>>rno;
cout<<"Enter name: ";
cin>>name;
cout<<"Enter fee: ";
cin>>fee;
cout<<"Enter course: ";
cin>>course;

fstream f;
f.open("d:/HII.txt",ios::app|ios::out);
f.write((char*)&rno,sizeof(rno));
f.write((char*)(&name),sizeof(name));
f.write((char*)(&fee),sizeof(fee));
f.write((char*)(&course),sizeof(course));
f.close();

f.open("d:/HII.txt",ios::in);
f.read((char*)&r,sizeof(r));
f.read((char*)&n,sizeof(n));
f.read((char*)&ee,sizeof(ee));
f.read((char*)&c,sizeof(c));
f.close();

cout<<r<<n<<ee<<c;
}

You might also like