0% found this document useful (0 votes)
80 views39 pages

All Oops Programs

The document contains 15 programming problems related to C++ concepts like functions, classes, inheritance, operator overloading etc. Each problem has the code to solve it along with sample input/output. The problems cover basic concepts like prime number checking, palindrome checking, matrix addition etc. as well as OOPs concepts like constructors, static members, inheritance, polymorphism etc.

Uploaded by

RishabhGoyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views39 pages

All Oops Programs

The document contains 15 programming problems related to C++ concepts like functions, classes, inheritance, operator overloading etc. Each problem has the code to solve it along with sample input/output. The problems cover basic concepts like prime number checking, palindrome checking, matrix addition etc. as well as OOPs concepts like constructors, static members, inheritance, polymorphism etc.

Uploaded by

RishabhGoyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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();
}

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

OUTPUT:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

PROGRAM-2

WAP to find whether the given number is palindrome or not.

#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();
}

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)


Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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();
}

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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];
}
}

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
cout<<" Result after addtion is ";
for(i=0;i<n;i++)
{
cout<<endl;
for(j=0;j<n;j++)
{
cout<<c[i][j]<<" ";
}
}
getch();
}

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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();
}

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

10

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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 ";

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

11

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)


cin>>s[i].uid;
cout<<" Enter email ";
gets(s[i].email);
}
getch();
}

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

12

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)


}
void calculator::div()
{
c=a/b;
cout<<" Result after division is "<<c<<endl;
}
void main()
{
clrscr();
cal.getdata();
cal.add();
cal.sub();
cal.mul();
cal.div();
getch();
}

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

14

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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);

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

15

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)


c1.show();
c2.show();
c3.show();
getch();
}

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

16

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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();
}

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

17

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

18

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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();
}

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

19

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

20

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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();

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

21

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)


incdec obj,obj1;
++obj;
--obj1;
obj.out();
obj1.out();
getch();
}

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

22

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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();
}

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

23

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

24

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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:";

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

25

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)


cin>>sub2;
}
void display()
{
cout<<"Marks of subject1 is "<<sub1<<endl;
cout<<"Marks of subject2 is "<<sub2<<endl;
}
};
void main()
{
clrscr();
b b1;
b1.getdata();
b1.getshow();
b1.put();
b1.display();
getch();
}

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

26

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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;
}

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

27

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)


};
void main()
{
clrscr();
counter2 c2;
counter2 c3(50);
c2.decount();
c2.decount();
c2.incount();
c3.decount();
c3.decount();
c3.incount();
getch();
}

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

28

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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;

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

29

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)


cout<<"Marks in sub2 "<<sub2<<endl;
}
class exam:public student
{
float total;
public:
void display()
{
total=sub1+sub2;
putnumber();
putmarks();
cout<<"Total="<<total;
}
};
void main()
{
clrscr();
exam e;
e.getnumber(29);
e.getmarks(98.9,99.9);
e.display();
getch();
}

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

30

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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....";
}
};

class derived1:public base


{
public:
void vfunct()
{
cout<<"\n\n\t\t this is the first derived class virtual function....\n";
}
};

class derived2:public base


{
public:
void vfunct()
{
cout<<"\n\n\t\t this is second derived class virtual function....\n";

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

31

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)


}
};

void main()
{
clrscr();
base *p,b;
derived1 d1;
derived2 d2;
p=&b;
p->vfunct();
p=&d1;
p->vfunct();
p=&d2;
p->vfunct();
getch();
}

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

32

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

33

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)


DC *dptr;
dptr=&derived;
dptr->d=300;
cout<<"dptr is derived type pointer"<<endl;
dptr->show();
cout<<"using type compatibility"<<endl;
((DC*)bptr)->d=400;
((DC*)bptr)->show();
getch();
return 0;
}

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

35

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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();
}

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

36

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

37

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)

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);

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

38

CHANDIGARH UNIVERSITY, GHARUAN (MOHALI)


cout<<ch;
}
evenp.close();
oddp.close();
getch();
}

Output:-

DEPARTMENT OF COMPUTER SCIENCE & COMMUNICATION ENGINEERING

39

You might also like