C++ Programming Examples: Operators, Arrays, and Matrices
C++ Programming Examples: Operators, Arrays, and Matrices
#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;
[Link]();
}
switch(sale)
{
case 1
}
}
};
int main()
{
Number n;
[Link]();
[Link]();
return 0;
}
#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;
[Link]();
[Link]();
return 0;
}
#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;
[Link]();
[Link]();
}
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;
[Link]();
[Link]();
}
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;
[Link]();
[Link]();
}
#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;
[Link]();
[Link]();
[Link]();
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;
[Link]();
do
{
cout<<"\nMENU\[Link]\[Link]\[Link]\[Link]\[Link]\nEnter your choice:";
cin>>sw;
switch(sw)
{
case 1:
cout<<"Enter the value you want to push";
cin>>x;
[Link](x);
break;
case 2:
x=[Link]();
cout<<"popped value:"<<x;
break;
case 3:
[Link]();
break;
case 4:
[Link]();
break;
case 5:
exit(0);
default:
cout<<"enter valid choice please";
}
}while(sw!=5);
}
#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;
[Link]();
[Link]();
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;
[Link]();
[Link]();
return 0;
}
#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;
[Link]();
do
{
cout<<"\nMENU\[Link]\[Link]\[Link]\[Link]\nEnter your choice:";
cin>>sw;
switch(sw)
{
case 1:
[Link]();
break;
case 2:
[Link]();
break;
case 3:
[Link]();
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;
[Link]();
[Link]();
cout<<"Enter the element you want to search";
cin>>x;
do
{
cout<<"\nMENU\[Link] search\[Link] search\[Link]\nEnter your choice:";
cin>>sw;
switch(sw)
{
case 1:
p=[Link](x);
if(p==-1)
cout<<"Element do not exist";
else
cout<<"position :"<<p+1;
break;
case 2:
p=[Link](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;
[Link]();
[Link]();
do
{
cout<<"\nMENU\[Link] Sort\[Link] Sort\[Link]\[Link]\nEnter your
choice:";
cin>>sw;
switch(sw)
{
case 1:
[Link]();
break;
case 2:
[Link]();
break;
case 4:
exit(0);
case 3:
cout<<"updated array";
[Link]();
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;
[Link]();
[Link]();
[Link]();
cout<<"transpose of matrix is:\n";
[Link]();
#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;
[Link]();
[Link]();
[Link]();
[Link]();
#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;
[Link]();
[Link]();
//[Link]();
[Link]();
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;
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
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{
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;
[Link]();
}
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;
[Link](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;
[Link](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 main()
{
Volume v;
int l,b,h,r,a,sw;
do
{
cout<<"\nMENU\[Link] vol\[Link] vol\[Link] vol\[Link]\nEnter
your choice:";
cin>>sw;
switch(sw)
{
case 1:
cout<<"side lenght: ";
cin>>a;
cout<<[Link](a);
break;
case 2:
cout<<"lenght of r and h: ";
cin>>r>>h;
cout<<[Link](r,h);
break;
case 3:
cout<<"lenght of l,b and h: ";
cin>>l>>b>>h;
cout<<[Link](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;
[Link]();
[Link]();
}
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;
[Link]();
[Link]();
}
#include<iostream>
using namespace std;
class Item
{
int id,price;
char name[100];
public:
void setValue()
{
cout<<"id : ";
cin>>id;
cout<<"name :";
fflush(stdin);
[Link](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;
}
#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);
[Link](bname,50);
cout<<" Author name :";
fflush(stdin);
[Link](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);
void Item::menu()
{
system("CLS");
while(1)
{
cout<<"\nMENU\[Link]\[Link]\[Link]\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);
[Link](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];
[Link]();
}
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();
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<[Link]();i++)
{
char x=[Link](i);
if(x=='a' || x=='e'|| x=='i' || x=='o' || x=='u')
v++;
else
co++;
}
cout<<v<<endl;
cout<<co;
}
};
int main()
{
Str s;
[Link]();
}
FIND
FUNCTION OF STRING
int pos;
string s1="I am Ishika Goyal";
pos=[Link]("ooo");
cout<<pos;
string s1="soosh";
[Link](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<[Link]();i++)
{
char x=[Link](i);
if(x<='a' || x>='z')
v++;
else
co++;
}
cout<<v<<endl;
cout<<co;
}
};
int main()
{
Str s;
[Link]();
}
#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;
}
int main()
{
Complex c1,c2,c3;
[Link]();
[Link]();
[Link]();
[Link]();
[Link](c1,c2);
cout<<"addition: \n";
[Link]();
}
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;
}
int main()
{
Complex c1,c2,c3,c4;
[Link]();
[Link]();
[Link]();
[Link]();
c4=[Link](c1,c2);
cout<<"addition: \n";
[Link]();
}
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 Maxim::display()
{
cout<<m<<"is largest\n";
}
int main()
{
Maxim i;
[Link]();
max(i);
[Link]();
}
// 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;
[Link](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;
[Link]();
[Link](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);
[Link]();
}
//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;
[Link]();
}
-----------------------------------------------------------------------------------
-----------
#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";
[Link]();
}
//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);
[Link]();
}
#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;
}
};
int main()
{
student s(1001,"Shyam","computer",30000);
[Link]();
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);
[Link]();
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;
[Link]();
};
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;
[Link](5);
[Link]();
//s2=s1;
Sample s2(s1);
[Link]();
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'};
[Link](5,78,"ishika");
[Link]();
#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=[Link];
fee=[Link];
}
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'};
[Link](5,78,3,9);
[Link]();
Sample s2;
s2=s1;
//Sample s2(s1); IMPLICIT COPY CONSTRUCTOR
[Link]();
Sample s3(s1);
//explicit me ese hi call krte h
// Sample s3;
// s3=s1;
[Link]();
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;
[Link]();
}
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;
[Link]();
[Link]();
}
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
#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();
};
int main()
{
Sample s(5,6);
[Link](); //x=5,y=6
-s;
[Link](); //x=-5,y=-6
return 0;
}
class Sample{
int x,y;
public:
Sample(int x,int y)
{
this->x=x;
this->y=y;
}
void operator ++();
void display();
};
int main()
{
Sample s(5,56);
[Link]();
++s;
[Link]();
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();
};
int main()
{
Sample s(5,56);
[Link]();
++s;
Sample s1(89,23);
s1++;
[Link]();
return 0;
}
#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 &);
};
int main()
{
Test t1(89,23);
[Link]();
++t1;
[Link]();
}
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;
}
int main(){
Number n1(45,7,1,2);
Number n2(12,4,8,0);
[Link]();
[Link]();
n1+n2;
[Link]();
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------
#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;
}
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);
[Link]();
[Link]();
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;
[Link]();
[Link]();
m1*m2;
[Link]();
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");
}
int main()
{
Stack s;
int x;
s+1;
s+2;
s+3;
[Link]();
x=-s;
[Link]();
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;
}
int main()
{
Number n1(34,67);
Number n2(1,2);
[Link]();
[Link]();
n1=n1+n2;
[Link]();
return 0;
#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);
[Link]();
}
#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 main()
{
test t;
[Link]();
for(int i=0;i<t.n;i++)
cout<<"\t"<<t[i];
}
#include<iostream>
using namespace std;
class Employee
{
int eid;
string name;
public:
Employee(int eid,string name)
{
this->eid=eid;
this->name=name;
}
int main()
{
Employee ee(1001,"ishika");
cout<<ee;
return 0;
}
#include<iostream>
using namespace std;
class Employee
{
int eid;
string name;
public:
int main()
{
Employee ee;
cin>>ee;
[Link]();
return 0;
}
INHERITANCE
#include<iostream>
using namespace std;
class Person
{
protected:
int id;
char name[100],add[100];
char gender;
};
void display()
{
cout<<endl<<"id: "<<id<<"\nname: "<<name<<"\naddress: "<<add<<"\
nsalary: "<<salary<<"\ndesignation: "<<desgn;
}
};
int main()
{
Employee
e; //object of
derived class
[Link](); //derived class object invoke derived
class member function
[Link]();
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 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;
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
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;
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
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;
}
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;
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;
}
};
int main()
{
Result r;
[Link]();
[Link]();
[Link]();
[Link]();
}
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;
[Link]();
// XYZ x;
// [Link]();
[Link]::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);
[Link]();
[Link]();
}
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;
}
};
void display()
{
cout<<"\t Internal Marks1: "<<Is1<<"\t Internal Marks2: "<<Is2;
}
};
int main()
{
Result r;
[Link]();
[Link]();
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);
[Link](name,20);
cout<<"Enter Age: ";
cin>>age;
cout<<"Enter Section: ";
cin>>sec;
}
void display()
{
cout<<"\troll no: "<<rno<<"\tname: "<<name<<"\tAge: "<<age<<"\
tSection: "<<sec;
}
};
void display()
{
Student::display();
cout<<"\t Marks1: "<<s1<<"\t Marks2: "<<s2<<"\t Marks3: "<<s3;
}
};
void display()
{
Marks::display();
cout<<"\tTotal Marks: "<<ttl<<" \tAverge Marks: "<<avg;
}
};
int main()
{
Result r;
[Link]();
[Link]();
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);
[Link](name,20);
cout<<"Enter Age: ";
cin>>age;
cout<<"Enter Section: ";
cin>>sec;
}
void display()
{
cout<<"\troll no: "<<rno<<"\tname: "<<name<<"\tAge: "<<age<<"\
tSection: "<<sec;
}
};
void display()
{
Student::display();
cout<<"\t Marks1: "<<s1<<"\t Marks2: "<<s2<<"\t Marks3: "<<s3;
}
};
int main()
{
Marks m;
[Link]();
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------------------
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;
}
};
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);
[Link]();
}
#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);
[Link](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();
}
class linear
{
int *a,flag,i,x,n;
public:
linear()
{
flag=0;
}
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;
[Link]();
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;
[Link]();
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>
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;
int main()
{
sum(10,20);
sum(13.6,34.7);
sum('a','c'); 196
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------
#include<iostream>
using namespace std;
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>
int main()
{
int n,arr[20];
cout<<"Enter size: ";
cin>>n;
ssort(arr,n);
display(arr,n);
cout<<"\n";
float ar[20];
cout<<"Enter size: ";
cin>>n;
ssort(ar,n);
display(ar,n);
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------INCOMPLETE STACK CLASS USING CLASS TEMPLATE
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\[Link]\[Link]\[Link]\[Link]\[Link]";
cout<<"\nenter your choice:";
cin>>sw;
switch(sw)
{
case 1:
cout<<"enter value: ";
cin>>v;
[Link](v);
break;
case 2:
v=[Link]();
if(v!=-1)
cout<<"poped element: "<<v;
break;
case 3:
v=[Link]();
cout<<"Peak element: "<<v;
break;
case 4:
[Link]();
break;
case 5:
exit(0);
}
}while(sw!=5);
return 0;
}
to read data--->
int id,string ename,int salary;
ifstream fin("e:/[Link]");
fin>>id<<enmae>>salary; //left to
right
[Link]();
---------------=-------------------------------------------------------------
#include<iostream>
using namespace std;
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\[Link]\[Link]\[Link]\[Link]\[Link]";
cout<<"\nenter your choice:";
cin>>sw;
switch(sw)
{
case 1:
[Link](a.v);
break;
case 2:
cout<<"poped element: "<<[Link]();;
break;
case 3:
cout<<"Peak element: "<<[Link]();
break;
case 4:
[Link]();
break;
case 5:
exit(0);
}
}while(sw!=5);
}
int main()
{
int sw,v;
cout<<"\nmenu\[Link] int\[Link] char\[Link] double\[Link]\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);
}
Stack<int> a;
return 0;
}
//[Link] 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;
ofstream fout("d:/[Link]",ios::app);
fout<<rno<<"\t"<<name<<"\t"<<fee<<"\t"<<course;
[Link]();
}
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
int rno,fee;
string name,course;
ifstream fin("d:/[Link]");
fin>>rno>>name>>fee>>course;
[Link]();
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;
ifstream fin("d:/new_file.txt");
fin>>r>>n>>f;
[Link]();
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;
[Link]("d:/[Link]",ios::app);
fout<<"\t"<<loc;
[Link]();
[Link]("d:/new_file.txt",ios::app);
fout<<"\t"<<loc;
[Link]();
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;
void read()
{
string rec;
ifstream fin("d:/[Link]");
while(getline(fin,rec))
cout<<rec<<endl;
[Link]();
}
int main()
{
int n;
cout<<"Enter no. of record: ";
cin>>n;
write(n);
read();
return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
string line;
cout<<"Enter the string: ";
//getline(cin,line);
cin>>line;
fstream fout;
[Link]("d:/[Link]",ios::out);
for(int i=0;line[i]!='\0';i++)
[Link](line[i]);
[Link]();
fstream file;
[Link]("d:/[Link]",ios::in) ;
while([Link]()==0)
{
char c=[Link]();
cout<<c;
}
[Link]();
}
-------------------------------------------------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;
[Link]("d:/endOfFile",ios::app);
fout<<id<<"\t\t"<<marks<<endl;
[Link]();
}
string irecord;
ifstream fin("d:/endOfFile");
while(getline(fin,irecord))
{
cout<<irecord<<endl;
}
while([Link]()==0)
cout<<[Link]();
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";
[Link]("d:/[Link]",ios::out);
file<<n<<"\t"<<name;
[Link]();
string val;
[Link]("d:/[Link]" ,ios::in);
while([Link]()==0)
{
val=[Link]();
cout<<[Link]();
}
//cout<<1;
[Link]();
}
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int n=5;
int word=0,line=0;
fstream file;
[Link]("d:/[Link]",ios::out);
file<<n<<"\t"<<n<<"\n"<<n;
[Link]();
[Link]("d:/[Link]",ios::in);
while([Link]()==0)
{
int r=[Link]();
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;
}
int main()
{
//int n=5;
string s;
fflush(stdin);
getline(cin,s);
int word=0,line=0;
fstream file;
[Link]("d:/[Link]",ios::out);
//file<<s<<"\n"<<s; //word
0 and line 1
for(int i=0;s[i]!='\0';i++)
[Link](s[i]);
[Link]();
[Link]("d:/[Link]",ios::in);
while([Link]()==0)
{
char r=[Link]();
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;
[Link]("d:/[Link]",ios::in);
while([Link]()==0)
{
char v=[Link]();
int i=v;
if(v=='\t')
s++;
cout<<v;
}
cout<<"\n alph "<<a<<" digits: "<<d<<" space: "<<s;
[Link]();
}
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int n=5;
fstream f;
// [Link]("d:/[Link]",ios::out);
// f<<n<<"\t"<<n;
// [Link]();
[Link]("d:/[Link]",ios::in);
fstream file;
[Link]("d:/[Link]",ios::out);
while([Link]()==0)
{
char ch=[Link]();
//f>>ch;
file<<ch;
}
[Link]();
[Link]();
//[Link]()
}
seekg
#include<iostream>
#include<fstream> //not
working correctly
using namespace std;
int main()
{
char s[100];
cout<<"enter string: ";
//fflush(stdin);
[Link](s,100);
int i;
fstream f;
[Link]("d:/[Link]",ios::out|ios::app|ios::ate);
for(i=0;s[i]!='\0';i++)
[Link](s[i]);
[Link]();
[Link]("d:/[Link]",ios::in);
[Link](3,ios::beg);
while([Link]()==0)
{
char c=[Link]();
cout<<c;
}
[Link]();
return 0;
}
no. of bytes/ tellg
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
fstream f;
[Link]("d:/[Link]",ios::app| ios::ate|ios::in);
int x=[Link]();
cout<<"no. of bytes: "<<x;
return 0;
}
//NOT WORKING (
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
fstream f;
[Link]("d:/[Link]",ios::in|ios::ate);
int x=[Link]();
cout<<"no. of bytes: "<<x;
[Link]();
[Link]("d:/[Link]",ios::out);
[Link](3,ios::beg);
f<<"so what?";
[Link]();
cout<<"\n";
[Link]("d:/[Link]",ios::in);
[Link](0);
while([Link]()==0)
{
char c=[Link]();
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);
[Link](a,20);
fstream file;
[Link]("d:/[Link]",ios::out);
for(i=0;a[i]!='\0';i++)
[Link](a[i]);
int x=[Link]();
[Link]();
[Link]("d:/[Link]",ios::in);
for(i=1;i<x+1;i++)
{
[Link](-i,ios::end);
char c=[Link]();
cout<<c;
}
[Link]();
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;
fstream f;
[Link]("d:/[Link]",ios::app|ios::out);
[Link]((char*)&rno,sizeof(rno));
[Link]((char*)(&name),sizeof(name));
[Link]((char*)(&fee),sizeof(fee));
[Link]((char*)(&course),sizeof(course));
[Link]();
[Link]("d:/[Link]",ios::in);
[Link]((char*)&r,sizeof(r));
[Link]((char*)&n,sizeof(n));
[Link]((char*)&ee,sizeof(ee));
[Link]((char*)&c,sizeof(c));
[Link]();
cout<<r<<n<<ee<<c;
}