Professional Documents
Culture Documents
#include<conio.h>
void main()
clrscr();
int ch;
cin>>ch;
switch(ch)
case 1:
cout<<"Monday";
break;
case 2:
cout<<"Tuesday";
break;
case 3:
DAVIET Page 1
2012
cout<<"Wednesday";
break;
case 4:
cout<<"Thursday";
break;
case 5:
cout<<"Friday";
break;
case 6:
cout<<"Saturday";
break;
case 7:
cout<<"Sunday";
break;
default:
getch();
DAVIET Page 2
2012
DAVIET Page 3
2012
#include<conio.h>
void main()
clrscr();
int n, result=1;
cin>>n;
result=result*i;
getch();
DAVIET Page 4
2012
DAVIET Page 5
2012
Program 3: Write a program to print the list of prime numbers upto any
number.
#include<iostream.h>
#include<conio.h>
void main( )
clrscr();
int n;
cin>>n;
if(i%j==0)
break;
else if(i==j+1)
getch();
DAVIET Page 6
2012
DAVIET Page 7
2012
#include<conio.h>
void main()
clrscr();
int n;
cin>>n;
if (n%i==0)
break;
if (i==n)
else
getch(); }
DAVIET Page 8
2012
DAVIET Page 9
2012
Program 5: Write a program to define class and its member functions inside the
class.
#include<iostream.h>
#include<conio.h>
class square
{private:
int a,b;
public:
void getdata()
cin>>a;
b=a*a;
}};
void main()
{clrscr();
cout<<"\n \n To define class and its member functions inside class \n \n";
square s; s.getdata();
getch(); }
DAVIET Page 10
2012
DAVIET Page 11
2012
Program 6: Write a program to define class and its member functions outside
the class.
#include<iostream.h>
#include<conio.h>
class square
{private:
int a,b;
public:
void getdata(); };
void square::getdata()
cin>>a;
b=a*a;
void main()
{clrscr();
cout<<"\n \n To define class and its member functions outside class \n \n";
square s;
s.getdata();
getch(); }
DAVIET Page 12
2012
DAVIET Page 13
2012
#include<conio.h>
class rectangle
private:
int b,c;
static int a;
public:
void getdata()
cin>>b;
void showdata()
c=a*b;
};
void main()
clrscr();
DAVIET Page 14
2012
cout<<"\n \n SUKRITI MODGIL \n \n";
rectangle obj1;
obj1.getdata();
obj1.showdata();
getch();
DAVIET Page 15
2012
DAVIET Page 16
2012
#include<conio.h>
class rectangle
private:
int b;
int a,c;
public:
rectangle()
b=6;
int getdata()const;
int setdata();
};
return b;
DAVIET Page 17
2012
cout<< " \n \n Enter the value of a: ";
cin>>a;
c=a*b;
return c;
void main()
clrscr();
obj1.getdata();
rectangle obj2;
obj2.setdata();
getch();
DAVIET Page 18
2012
DAVIET Page 19
2012
#include<conio.h>
class A
public:
int a, b;
void getdata()
cin>>a;
};
class B: public A
public:
int b;
void showdata()
b=a*a;
};
DAVIET Page 20
2012
void main()
clrscr();
B obj;
obj.getdata();
obj.showdata();
getch();
DAVIET Page 21
2012
DAVIET Page 22
2012
#include<conio.h>
class A
public:
int a;
void getdata()
cin>>a;
};
class B
public:
int b;
void getdata1()
cin>>b;
};
DAVIET Page 23
2012
{
int c;
public:
void showdata()
c=a*b;
cout<< endl << " The area of rectangle is: " << c << endl;
};
void main()
clrscr();
C c1;
c1.getdata();
c1.getdata1();
c1.showdata();
getch();
DAVIET Page 24
2012
DAVIET Page 25
2012
#include<conio.h>
class Student
public:
int r_no;
char name[20];
void getdata()
cin>>r_no;
cin>>name;
};
public:
int m,m1,m2,m3;
void showdata()
cin>>m1;
cin>>m2;
DAVIET Page 26
2012
cout<<" Marks in mathematics are: "<<endl;
cin>>m3;
};
public:
int m;
void disp()
m=m1+m2+m3;
};
void main()
clrscr();
Display obj;
obj.getdata();
obj.showdata();
obj.disp();
getch();}
DAVIET Page 27
2012
DAVIET Page 28
2012
#include<conio.h>
class rectangle
public:
int a,b,c;
rectangle()
a= 24;
b= 5;
void getdata()
c=a*b;
}};
void main()
clrscr();
rectangle obj;
obj.getdata();
DAVIET Page 29
2012
getch();
DAVIET Page 30
2012
#include<conio.h>
class square
public:
int s;
square(int x)
s=x;
int n;
void showdata()
{n=s*s;
}};
void main()
{ clrscr();
square obj(4);
obj.showdata();
getch(); }
DAVIET Page 31
2012
DAVIET Page 32
2012
#include<conio.h>
class rectangle
int l,b;
public:
rectangle()
void read()
cin>>l;
cin>>b;
void area()
~rectangle()
DAVIET Page 33
2012
cout<<" Destructor invoked \n ";
}};
void main()
clrscr();
rectangle *ptr;
ptr=new rectangle;
ptr->read();
ptr->area();
delete ptr;
getch();
DAVIET Page 34
2012
DAVIET Page 35
2012
#include<conio.h>
class rectangle
{ private:
int l,b;
public:
void showdata()
}};
{}
void main()
clrscr();
rectangle r1(10,20);
r1.showdata();
getch();
DAVIET Page 36
2012
DAVIET Page 37
2012
#include<iostream.h>
#include<conio.h>
class complex
{float real,imag;
public:
complex()
{real=0;
imag=0;
}
complex(float r,float i)
{
real=r;
imag=i;
}
complex operator+(complex c)
{
complex temp;
temp.real=real+c.real;
temp.imag=imag+c.imag;
return temp;
}
void show()
{
cout<<"\n complex number is:"<<real<<"+"<<imag<<"i";
}};
void main()
{
clrscr();
complex c1(2.3,4.5);
complex c2(4.8,3.1);
complex c3;
cout<<"\n value of complex no.1 before overloading:";
c1.show();
cout<<"\n value of comple no. 2 before overloading:";
c2.show();
cout<<"\n addition of complex no. using operator overloading:";
c3=c1+c2;
c3.show();
getch();
}
DAVIET Page 38
2012
DAVIET Page 39
2012
#include<iostream.h>
#include<conio.h>
class student
{
private:
int value;
public:
student()
{
value=1;
}
int getdata()
{
return value;
}
void operator++()
{
value++ ;
}
};
void main()
{
clrscr();
student r1,r2;
cout<<"value of r1 is"<<endl<<r1.getdata()<<endl;
cout<<"value of r2 :"<<endl<<r2.getdata() <<endl;
r1 ++;
r2 ++;
++r2;
cout<<"final value of r1:"<<endl<<r1.getdata() <<endl;
cout<<"final value of r2:"<<endl<<r2.getdata() <<endl;
getch();
}
DAVIET Page 40
2012
DAVIET Page 41
2012
#include<iostream.h>
#include<conio.h>
class complex
{int a,b;
public:
void getvalue()
{cout<<"Enter the value of Complex Numbers a,b:";
cin>>a>>b;
}
complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
}
complex operator-(complex ob)
{complex t;
t.a=a-ob.a;
t.b=b-ob.b;
return(t);
}
void display()
{
cout<<a<<"+"<<b<<"i"<<"\n";
}};
void main()
{clrscr();
complex obj1,obj2,result,result1;
obj1.getvalue();
obj2.getvalue();
result = obj1+obj2;
result1=obj1-obj2;
cout<<"Input Values:\n";
obj1.display();
obj2.display();
cout<<"Result:";
result.display();
result1.display();
getch();
}
DAVIET Page 42
2012
DAVIET Page 43
2012
#include<iostream.h>
#include<conio.h>
class A
{
int a;
float b;
public:
void print()
{
cout<<"A is ==>"<<a;
cout<<"\nb is ==>"<<b;
}
A(int p,float q)
{
a=p;b=q;
}
void operator delete(void *p)
{
cout<<"\nmemory freed.";
delete (p);
}
void * operator new(size_t size)
{
cout<<"memory alloted.\n";
void * p;
return p;
}};
void main()
{
A *a;
clrscr();
a=new A(8,9);
a->print();
delete a;
getch();
DAVIET Page 44
2012
DAVIET Page 45
2012
#include<iostream.h>
#include<conio.h>
class student1
{
int roll,marks;
public:
student1(int a,int b)
{
roll=a;
marks=b;
}
void print()
{
cout<<"\n roll number is"<<roll;
cout<<"\n marks are"<<marks;
}
int getroll()
{
return roll;
}
float getmarks()
{ return marks; }};
class B
{int roll;
float per;
public:
B(student1 s)
{roll=s.getroll();
per=s.getmarks()/100;
}
void print()
{cout<<"roll number is==>"<<roll;
cout<<"\n percentage is==>"<<per;
}};
void main()
{student1 s1(256,86);
B s2=s1;
clrscr();
s2.print();
getch();
}
DAVIET Page 46
2012
DAVIET Page 47
2012
Program 21: Write a program to implement Type Conversion from Defined type
to Basic type.
#include<iostream.h>
#include<conio.h>
class time
{
int hours,mins;
public:
time(int hr,int min)
{
hours=hr;
mins=mm;
}
operator int()
{
Int mnts
mnts=hours*60+mins;
return(mnts);
}
void show()
{
cout<<”time in hours and mins=”<<hours<<”:”<<mins;
};
void main()
{
int minutes;
time t1(3,15);
t1.show();
minutes=t1;
cout<<”time in minutes=”<<minutes;}
getch();
}
DAVIET Page 48
2012
DAVIET Page 49
2012
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"display base"<<endl;
}
virtual void show()
{
cout<<"show base:"<<endl;
}};
class derived:public base
{
public:
void display()
{
cout<<"display derived"<<endl;
}
void show()
{
cout<<"show derived"<<endl;
}};
void main()
{
base b;
derived d;
base *bptr;
clrscr();
cout<<"bptr points to base"<<endl;
bptr=&b;
bptr->display();
bptr->show();
cout<<"bptr points to derived"<<endl;
bptr=&d;
bptr->display();
bptr->show();
getch(); }
DAVIET Page 50
2012
DAVIET Page 51
2012
Program 23: Write a program to store the address and display the value of a
variable using a Pointer.
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int i=5,*ptr;
ptr=&i;
getch();
DAVIET Page 52
2012
DAVIET Page 53
2012
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
for(int i=0;i<5;i++)
cin>>x[i];
for(int j=0;j<5;j++)
getch();
DAVIET Page 54
2012
DAVIET Page 55
2012
Program 25: Write a program to swap two numbers by call by reference method
by using pointers.
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int a,b;
int *p1;
int *p2;
p1=&a;
p2=&b;
a=10;
b=20;
cout<<"a="<<*p1<<"b="<<*p2<<endl;
swap(*p1,*p2);
cout<<"a="<<*p1<<"b="<<*p2;
getch(); }
DAVIET Page 56
2012
DAVIET Page 57
2012
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void display()
{
cout<<”base class display called”;
}};
class derived1:public base
{
void display()
{
cout<<”derived1 display called”;
}};
class derived2:public base
{
void display()
{
cout<<”derived display 2 called”;
}};
void main()
{
base*ptr;
derived1 d1;
derived2 d2;
ptr=&d1;
ptr->display();
ptr=&d2;
ptr->display();
}
getch();
}
DAVIET Page 58
2012
DAVIET Page 59
2012
#include<iostream.h>
#include<conio.h>
class grandparent
int g;
public:
void getdata()
cout<<"enter g\n";
cin>>g;
void showdata()
cout<<"\nvalue of g is:\n";
cout <<g;
};
int p1;
public:
DAVIET Page 60
2012
};
int p2;
public:
};
int c;
public:
};
void main()
clrscr();
child c1;
c1.showdata();
getch();
DAVIET Page 61
2012
DAVIET Page 62
2012
#include<iostream.h>
#include<conio.h>
void main()
int x;
cin>>x;
try
if(x==1)
throw x;
else if (x==0)
throw x;
else if(x==-1)
throw 1.0;
catch(char i)
catch(int i)
DAVIET Page 63
2012
{
catch(float i)
getch();
DAVIET Page 64
2012
DAVIET Page 65
2012
Program 28: Write a program to create a file ‘student.txt’ and store student
information into it.
#include<fstream.h>
#include<conio.h>
#include<process.h>
void main()
{clrscr();
int rno;
char name[25];
int marks;
ofstream file("student.txt");
if(!file)
exit (-1);}
cin>>rno;
cin>>name;
cin>>marks;
cout<<"\n\tCreating file and writing the information onto the file !!!";
file<<rno<<endl;
DAVIET Page 66
2012
DAVIET Page 67
2012
Program 29: Write a program to read the contents of the file ‘student.txt’.
#include<fstream.h>
#include<conio.h>
#include<process.h>
void main()
{clrscr();
int rno;
char name[25];
int marks;
ifstream file("student.txt");
if(!file)
exit(-1);}
file>>rno;
file>>name;
file>>marks;
getch();
DAVIET Page 68
2012
DAVIET Page 69
2012
#include<conio.h>
class base
public:
int a;
void getdata()
cout<<"Enter a number"<<endl;
cin>>a;
void showdata()
cout<<"a="<<" "<<a<<endl;
};
int b;
public:
void getdata()
base::getdata();
DAVIET Page 70
2012
cout<<"Enter another number"<<endl;
cin>>b;
void showdata()
base::showdata();
cout<<"b="<<" "<<b;
};
void main()
clrscr();
base b;
derived d;
d.getdata();
d.showdata();
b=d;
getch();
DAVIET Page 71
2012
DAVIET Page 72
2012
#include<conio.h>
template<class T>
{ T temp;
temp=a+b;
cout<<"\n\tSum is : "<<temp<<endl; }
void main()
{ clrscr();
add(5,11);
add(5.56,51.22);
getch();
DAVIET Page 73
2012
DAVIET Page 74
2012
#include<conio.h>
template<class T>
void show(T a)
void show(int a)
void main()
{ clrscr();
show(22);
show(31.37);
getch();
DAVIET Page 75
2012
DAVIET Page 76