All Oops Programs
All Oops Programs
PROGRAM-1
WAP to print prime numbers between 1 to 100.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j;
cout<<"\n\n\t\t Enter the number:";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=2;j<=i;j++)
{
if(i%j==0)
{
break;
}
}
if(i==j)
{
cout<<"\t\t"<<i;
}
getch();
}
OUTPUT:-
PROGRAM-2
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,a,temp=0;
cout<<"Enter the no. ";
cin>>n;
a=n;
while(a>0)
{
temp=temp * 10;
temp=temp+ a%10;
a=a/10;
}
if(temp==n)
cout<<" Number is palindrome";
else
cout<<" Number is not a plindrome";
getch();
}
PROGRAM-3
WAP to find whether the given string is palindrome or not.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char name[15],rev[15];
cout<<" Enter the string ";
gets(name);
strcpy(rev,name);
strrev(rev);
if(strcmp(rev,name)==0)
cout<<" String is palindrome";
else
cout<<" String is not a palindrome";
getch();
}
Output:-
PROGRAM-4
WAP which takes two n*n matrices where n will be specified by the user.
Write a method which does summation of both matrices and store the result
in third matrix also display the resultant matrix.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5][5],b[5][5],c[5][5];
int n,i,j;
cout<<"Enter no. of row and column ";
cin>>n;
cout<<" Enter the values of 1st matrix";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<" Enter the values of 2nd matrix";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
}
Output:-
PROGRAM-5
Write a program to generate the Fibonacci series up to user specified limit.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,a=0,b=1,c;
cout<<"Enter the limit ";
cin>>n;
if(n==0)
cout<<"wrong input";
else if(n==1)
cout<<a;
else
{ cout<<a<<" "<<b;
for(int i=2;i<n;i++)
{
c=a+b;
cout<<" "<<c;
a=b;
b=c;
}
}
getch();
}
Output:-
10
PROGRAM-6
Define the structure called student having properties like stud_id,
stud_name and email add. Write a program which takes the details of 5
students.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct student
{
char name[20];
int uid;
char email[20];
}s[5];
void main()
{
clrscr();
int i;
cout<<" Enter records of 5 students "<<endl;
for(i=0;i<5;i++)
{
cout<<" enter name of student ";
gets(s[i].name);
cout<<" Enter UID ";
11
Output:-
12
PROGRAM-7
WAP having class calculator, such that the function addition, subtraction are
defined inside a class and multiplication and division are defined outside the
class.
#include<iostream.h>
#include<conio.h>
class calculator
{
int a,b;
float c;
public:
void getdata()
{
cout<<" Enter the values of a and b ";
cin>>a>>b;
}
void add()
{
c=a+b;
cout<<" Result after addition is "<<c<<endl;
}
void sub()
{
c=a-b;
cout<<" Result after subtraction is "<<c<<endl;
}
void mul();
void div();
}cal;
void calculator::mul()
{
c=a*b;
cout<<" Result after multiplication is "<<c<<endl;
DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING
13
Output:-
14
PROGRAM-8
WAP to demonstrate constructor overloading in a class.
#include<iostream.h>
#include<conio.h>
class constructor
{
public:
int a,b,c;
constructor()
{
a=1;
b=2;
c=a+b;
}
constructor(int z)
{
a=z;
c=b-a;
}
constructor(int x,int y)
{
a=x;
b=y;
}
void show()
{
cout<<endl<<a<<endl<<b<<endl<<c<<endl;
}
};
void main()
{
clrscr();
constructor c1,c2(7),c3(2,3);
15
Output:-
16
PROGRAM-9
WAP showing concept of static data member in class name Book.
#include<iostream.h>
#include<conio.h>
class book
{
static int count;
public:
book()
{
count=1;
}
void addbook()
{
count++;
}
void displaybook()
{
cout<<" The no. of books are "<<count;
}
};
int book::count;
void main()
{
clrscr();
book b1,b2;
b1.addbook();
b1.displaybook();
cout<<endl;
b2.addbook();
b2.displaybook();
getch();
}
17
Output:-
18
PROGRAM-10
WAP for addition of complex numbers by overloading binary operator.
#include<iostream.h>
#include<conio.h>
class complex
{
public:
int x,y;
complex()
{
x=7;
y=8;
}
void display()
{
cout<<x<<"+i"<<y;
}
complex operator+(complex b)
{
complex temp;
temp.x=x+b.x;
temp.y=y+b.y;
return temp;
}
};
void main()
{
clrscr();
complex c1,c2,c3;
c3=c1+c2;
c3.display();
getch();
}
19
Output:-
20
PROGRAM-11
WAP to demonstrate the overloading of increment and decrement operator.
#include<conio.h>
#include<iostream.h>
class incdec
{
int a,b;
public:
incdec()
{
a=5;
b=8;
}
void operator ++()
{
a++;
b++;
}
void operator --()
{
a--;
b--;
}
void out()
{
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
}
};
void main()
{
clrscr();
21
Output:-
22
PROGRAM-12
WAP to access the private data of a class by non-member function through
friend.
#include<iostream.h>
#include<conio.h>
class item
{
int x;
int y;
public:
void setdata()
{
x=11;
y=14;
}
friend float sample(item i);
};
float sample(item i)
{
return float(i.x+i.y)/2.0;
}
void main()
{
clrscr();
item a;
a.setdata();
cout<<"mean value="<<sample(a)<<"\n";
getch();
}
23
Output:-
24
PROGRAM-13
WAP to read the derived class data memberssuch as name, roll no and display
it on screen.This program follows single inheritance.
#include<iostream.h>
#include<conio.h>
class a
{
char name[30];
int rollno;
public:
void getdata()
{
cout<<"Enter the name of student:";
cin>>name;
cout<<"Enter the roll no of student:";
cin>>rollno;
}
void put()
{
cout<<"Name is "<<name<<endl;
cout<<"Roll no is "<<rollno<<endl;
}
};
class b:public a
{
int sub1,sub2;
public:
void getshow()
{
cout<<"Enter marks of subject1:";
cin>>sub1;
cout<<"Enter marks of subject2:";
25
Output:-
26
PROGRAM-14
WAP that shows orders of execution of base and derived class constructor
inheritance
#include<iostream.h>
#include<conio.h>
class counter1
{
protected:int count;
public:
counter1()
{
count=4;
}
counter1(int c)
{
count=c;
}
void incount()
{
count++;
cout<<"count is "<<count<<endl;
}
};
class counter2:public counter1
{
public:
counter2():counter1(){}
counter2(int c):counter1(c){}
void decount()
{
count--;
cout<<"count is "<<count<<endl;
}
27
Output:-
28
PROGRAM-15
WAP having three classes Person,Student and exam. The person class is the
base class, Student class is derived from Person and Exam class is derived
from Student.
#include<conio.h>
#include<iostream.h>
class person
{
int rollno;
public:
void getnumber(int);
void putnumber(void);
};
void person::getnumber(int a)
{
rollno=a;
}
void person::putnumber()
{
cout<<"Roll No"<<rollno<<endl;
}
class student:public person
{
protected:
float sub1,sub2;
public:
void getmarks(float,float);
void putmarks(void);
};
void student::getmarks(float x,float y)
{
sub1=x;
sub2=y;
}
void student::putmarks()
{
cout<<"Marks in sub1 "<<sub1<<endl;
29
Output:-
30
PROGRAM-16
WAP to display concept of virtual function.
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void vfunct()
{
cout<<"\n\n\t\t this is base class virtual function....";
}
};
31
void main()
{
clrscr();
base *p,b;
derived1 d1;
derived2 d2;
p=&b;
p->vfunct();
p=&d1;
p->vfunct();
p=&d2;
p->vfunct();
getch();
}
Output:-
32
PROGRAM-17
WAP to demonstrate use of new and delete operator.
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int a=5;
int *p=&a;
int *p1=new int;
*p1=6;
cout<<"First variable is:"<<a<<endl;
cout<<"First variable is:"<<*p<<endl;
cout<<"Second value is:"<<*p1<<endl;
delete p1;
p1=new int;
*p1=8;
cout<<"Assign to second:"<<*p1;
delete p1;
getch();
}
Output:-
33
PROGRAM-18
WAP to show concept of this pointers.
#include<conio.h>
#include<iostream.h>
class BC
{
public:
int b;
void show()
{
cout<<"b="<<b<<endl;
}
};
class DC:public BC
{
public:
int d;
void show()
{
cout<<"b="<<b<<endl;
cout<<"d="<<d<<endl;
}
};
int main()
{
clrscr();
BC *bptr;
BC base;
bptr=&base;
bptr->b=100;
cout<<"bptr points to base object"<<endl;
bptr->show();
DC derived;
bptr=&derived;
bptr->b=200;
cout<<"bptr now points to derived object"<<endl;
bptr->show();
DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING
34
Output:-
35
PROGRAM-19
WAP to show concept of template function.
#include<conio.h>
#include<iostream.h>
template<class T>
void swap(T &x,T &y)
{
T temp=x;
x=y;
y=temp;
}
void fun(int m,int n,float a,float b,char c,char d)
{
cout<<"m and n before swap :"<<m<<" "<<n<<endl;
swap(m,n);
cout<<"m and n after swap :"<<m<<" "<<n<<endl;
cout<<"a and b before swap :"<<a<<" "<<b<<endl;
swap(a,b);
cout<<"a and b after swap :"<<a<<" "<<b<<endl;
cout<<"c and d before swap :"<<c<<" "<<d<<endl;
swap(c,d);
cout<<"c and d after swap :"<<c<<" "<<d<<endl;
}
void main()
{
clrscr();
fun(100,200,10.5,20.5,'x','y');
getch();
}
36
Output:-
37
PROGRAM-20
WAP to store even and odd no in file even.txt and odd.txt resp. and read
no from files and print them.
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
int a[10];
cout<<"Enter 10 interger"<<endl;
for(int i=0;i<10;i++)
cin>>a[i];
ofstream evenoutf("even.txt");
ofstream oddoutf("odd.txt");
for (i=0;i<10;i++)
{
if(a[i]%2==0)
evenoutf<<a[i]<<" ";
else
oddoutf<<a[i]<<" ";
}
evenoutf.close();
oddoutf.close();
ifstream evenp("even.txt");
ifstream oddp("odd.txt");
char ch;
cout<<"Integers in even file"<<endl;
while(!evenp.eof())
{
evenp.get(ch);
cout<<ch;
}
cout<<endl<<"Integers in odd file"<<endl;
while(!oddp.eof())
{
oddp.get(ch);
38
Output:-
39