You are on page 1of 76

2012

Program 1: Write a program to illustrate Switch-Case Statement.


#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int ch;

cout<<"\n \n Sukriti Modgil \n \n";

cout<<"\n \n ECE-2C \n 259/11 \n \n";

cout<<"\n \n Defining Switch case statement \n \n";

cout<<"\n \n Enter any number \n \n";

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:

cout<<"There are only seven days in a week";

getch();

DAVIET Page 2
2012

DAVIET Page 3
2012

Program 2: Write a program to calculate the factorial of any number.


#include<iostream.h>

#include<conio.h>

void main()

clrscr();

cout<<"\n Sukriti Modgil \n";

cout<<"\n ECE 259/11 \n";

cout<<"\n Factorial \n";

int n, result=1;

cout<<"\n \n Enter the number of which factorial is to be calculated \n \n";

cin>>n;

for(int i=1; i<=n; i++)

result=result*i;

cout<<"\n \n The factorial of given number is \n"<<result;

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

cout<<"\n \n Sukriti Modgil \n \n";

cout<<"\n \n 259/11 ECE-2C \n \n";

int n;

cout<<"\n \n Enter any number \n \n";

cin>>n;

for(int i=2;i<n; i++)

for(int j=2; j<i; j++)

if(i%j==0)

break;

else if(i==j+1)

cout<< i <<" ";

getch();

DAVIET Page 6
2012

DAVIET Page 7
2012

Program 4: Write a program to check whether a number is prime or not.


#include<iostream.h>

#include<conio.h>

void main()

clrscr();

cout<<"\n Sukriti Modgil \n";

cout<<"\n ECE 259/11 \n";

cout<<"\n PRIME NUMBER \n";

int n;

cout<<" \n \n Enter the number which is to be checked for prime \n";

cin>>n;

for(int i=2; i<n; i++)

if (n%i==0)

break;

if (i==n)

cout<<" \n \n The given number is prime. \n";

else

cout<<"\n \n The given number is not prime. \n";

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

{cout<<"Enter the side of square : ";

cin>>a;

b=a*a;

cout<<"Area of square is: "<<b;

}};

void main()

{clrscr();

cout<<"\n \n Sukriti Modgil \n \n";

cout<<"\n \n ECE-2C \n 259/11 \n \n";

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

{cout<<"Enter the side of square : ";

cin>>a;

b=a*a;

cout<<"Area of square is: "<<b;

void main()

{clrscr();

cout<<"\n \n Sukriti Modgil \n \n";

cout<<"\n \n ECE-2C \n 259/11 \n \n";

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

Program 7: Write a program to define static member functions.


#include<iostream.h>

#include<conio.h>

class rectangle

private:

int b,c;

static int a;

public:

void getdata()

cout<<" \n \n Enter the breadth of rectangle \n \n ";

cin>>b;

void showdata()

c=a*b;

cout<<" \n \n Area of rectangle is: \n \n "<<c;

};

int rectangle :: a=6 ;

void main()

clrscr();

DAVIET Page 14
2012
cout<<"\n \n SUKRITI MODGIL \n \n";

cout<<"\n \n ECE-2C 259/11 \n \n";

cout<<" \n \n Demonstrating Static Member Functions \n \n ";

rectangle obj1;

obj1.getdata();

obj1.showdata();

getch();

DAVIET Page 15
2012

DAVIET Page 16
2012

Program 8: Write a program to define constant member functions.


#include<iostream.h>

#include<conio.h>

class rectangle

private:

int b;

int a,c;

public:

rectangle()

b=6;

int getdata()const;

int setdata();

};

int rectangle :: getdata()const

cout<< " \n \n b is: "<<b;

return b;

int rectangle :: setdata()

DAVIET Page 17
2012
cout<< " \n \n Enter the value of a: ";

cin>>a;

c=a*b;

cout<<" \n \n Area of rectangle is: "<<c;

return c;

void main()

clrscr();

cout<<"\n \n SUKRITI MODGIL \n \n";

cout<<"\n \n ECE-2C 259/11 \n \n";

cout<<" \n \n Demonstrating Constant Member Functions \n \n ";

const rectangle obj1;

obj1.getdata();

rectangle obj2;

obj2.setdata();

getch();

DAVIET Page 18
2012

DAVIET Page 19
2012

Program 9: Write a program to illustrate Single Inheritance.


#include<iostream.h>

#include<conio.h>

class A

public:

int a, b;

void getdata()

cout<<endl<< " Enter the value of side of a square "<<endl;

cin>>a;

};

class B: public A

public:

int b;

void showdata()

b=a*a;

cout<<endl<< " The area of square is: "<<b<<endl;

};

DAVIET Page 20
2012
void main()

clrscr();

cout<<endl<< " Sukriti Modgil "<<endl;

cout<<endl<< " ECE-2C "<<endl<< " 259/11 "<<endl;

cout<<endl<< " To illustrate Single Inheritance "<<endl;

B obj;

obj.getdata();

obj.showdata();

getch();

DAVIET Page 21
2012

DAVIET Page 22
2012

Program 10: Write a program to illustrate Multiple Inheritance.


#include<iostream.h>

#include<conio.h>

class A

public:

int a;

void getdata()

cout<<endl<< " Enter the length of rectangle "<<endl;

cin>>a;

};

class B

public:

int b;

void getdata1()

cout<<endl<< " Enter the breadth of rectangle "<<endl;

cin>>b;

};

class C: public A, public 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();

cout<<endl<< " Sukriti Modgil "<<endl;

cout<<endl<< " ECE-2C " <<endl<< "259/11"<<endl;

cout<<endl<< " To illustrate Multiple Inheritance "<<endl;

C c1;

c1.getdata();

c1.getdata1();

c1.showdata();

getch();

DAVIET Page 24
2012

DAVIET Page 25
2012

Program 11: Write a program to demonstrate Multilevel Inheritance.


#include<iostream.h>

#include<conio.h>

class Student

public:

int r_no;

char name[20];

void getdata()

cout<<" Roll number of student is: "<<endl;

cin>>r_no;

cout<<" Name of student is: "<<endl;

cin>>name;

};

class Total : public Student

public:

int m,m1,m2,m3;

void showdata()

{ cout<<" Marks in physics are: "<<endl;

cin>>m1;

cout<<" Marks in chemistry are: "<<endl;

cin>>m2;

DAVIET Page 26
2012
cout<<" Marks in mathematics are: "<<endl;

cin>>m3;

};

class Display : public Total

public:

int m;

void disp()

m=m1+m2+m3;

cout<<" Total marks out of 400 are: "<<m<<endl;

};

void main()

clrscr();

cout<<endl<< " Sukriti Modgil "<<endl;

cout<<endl<< " ECE-2C " <<endl<< "259/11"<<endl;

cout<<endl<< " To illustrate Multilevel Inheritance "<<endl;

Display obj;

obj.getdata();

obj.showdata();

obj.disp();

getch();}

DAVIET Page 27
2012

DAVIET Page 28
2012

Program 12: Write a program to illustrate default constructors.


#include<iostream.h>

#include<conio.h>

class rectangle

public:

int a,b,c;

rectangle()

a= 24;

b= 5;

void getdata()

c=a*b;

cout<<endl<<" Area of rectangle is:" <<endl<<c;

}};

void main()

clrscr();

cout<<"\n \n Sukriti Modgil \n";

cout<<"\n ECE-2C \n 259/11 \n";

cout<<"\n To illustrate Default Constructors \n \n";

rectangle obj;

obj.getdata();

DAVIET Page 29
2012
getch();

DAVIET Page 30
2012

Program 13: Write a program to illustrate Parameterized Constructors.


#include<iostream.h>

#include<conio.h>

class square

public:

int s;

square(int x)

s=x;

int n;

void showdata()

{n=s*s;

cout<< "Area of square is: "<<n;

}};

void main()

{ clrscr();

cout<<"\n \n Sukriti Modgil \n";

cout<<"\n ECE-2C \n 259/11 \n";

cout<<"\n To illustrate Parameterized Constructors \n \n";

square obj(4);

obj.showdata();

getch(); }

DAVIET Page 31
2012

DAVIET Page 32
2012

Program 14: Write a program to illustrate Dynamic Constructor.


#include<iostream.h>

#include<conio.h>

class rectangle

int l,b;

public:

rectangle()

cout<<" \n \n Constructor Invoked \n ";

void read()

cout<<" \n Enter the length \n ";

cin>>l;

cout<<" \n Enter the breadth \n ";

cin>>b;

void area()

cout<<" \n Area of rectangle \n "<<l*b;

~rectangle()

DAVIET Page 33
2012
cout<<" Destructor invoked \n ";

}};

void main()

clrscr();

cout<<" Sukriti Modgil "<<endl<<" ECE-2C "<<endl;

cout<<" 259/11 ";

rectangle *ptr;

ptr=new rectangle;

ptr->read();

ptr->area();

cout<<"\n Destroying object \n "<<endl;

delete ptr;

cout<<" \n End of program \n "<<endl;

getch();

DAVIET Page 34
2012

DAVIET Page 35
2012

Program 15: Write a program to demonstrate Initializer list.


#include<iostream.h>

#include<conio.h>

class rectangle

{ private:

int l,b;

public:

rectangle (int a,int x);

void showdata()

{cout<<" \n Length of Rectangle is: "<<l<<endl;

cout<<" \n Breadth of Rectangle is: "<<b<<endl;

cout<<" \n Area of Rectangle is: "<<l*b<<endl;

}};

rectangle::rectangle(int a,int x):l(a),b(x)

{}

void main()

clrscr();

cout<<" Sukriti Modgil "<<endl<<" ECE-2C "<<endl;

cout<<" To demonstrate Initializer List "<<endl;

rectangle r1(10,20);

r1.showdata();

getch();

DAVIET Page 36
2012

DAVIET Page 37
2012

Program 16: Write a program to illustrate the use of operator overloading.

#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

Program 17: Write a program to Overload Incrementing Unary Operator.

#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

Program 18: Write a program to illustrate the concept of Binary operator.

#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

Program 19: Write a program to overload new and delete operator.

#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

Program 20 : Write a program to implement Type Conversion from class to class.

#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

Program 22: Write a program to illustrate the concept of Virtual functions.

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

cout<<"\n address of i= "<<&i;

cout<<"\n value stored in ptr "<<ptr;

cout<<"\n value of *ptr is "<<*ptr;

getch();

DAVIET Page 52
2012

DAVIET Page 53
2012

Program 24: Write a program to access array using Pointers.

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

long int x[10];

cout<<"enter any 5 numbers";

for(int i=0;i<5;i++)

cin>>x[i];

for(int j=0;j<5;j++)

cout<<"At address "<<(x+j)<<'\n'<<"the value is"<<*(x+j)<<endl;

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<<"original values are ";

cout<<"a="<<*p1<<"b="<<*p2<<endl;

int swap(int& ,int&);

swap(*p1,*p2);

cout<<"values after swapping are ";

cout<<"a="<<*p1<<"b="<<*p2;

getch(); }

DAVIET Page 56
2012

DAVIET Page 57
2012

Program 26: Write a program to show the concept of polymorphism.

#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

Program 27: Write a program to implement Ambiguity in multilevel inheritance.

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

};

class parent1: public grandparent

int p1;

public:

DAVIET Page 60
2012

};

class parent2 : public grandparent

int p2;

public:

};

class child:public parent1,public parent2

int c;

public:

};

void main()

clrscr();

child c1;

c1.showdata();

getch();

DAVIET Page 61
2012

DAVIET Page 62
2012

WAP to use multiple catch statements .

#include<iostream.h>

#include<conio.h>

void main()

int x;

cout<<"enter the value of x\n";

cin>>x;

try

if(x==1)

throw x;

else if (x==0)

throw x;

else if(x==-1)

throw 1.0;

catch(char i)

cout<<"char exception caught";

catch(int i)

DAVIET Page 63
2012
{

cout<<"int exception caught";

catch(float i)

cout<<"float exception caught";

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)

{ cout<<"\n\tFile cannot open correctly.";

exit (-1);}

cout<<"\n\tEnter student details : \n";

cout<<"\n\tEnter Roll no. : ";

cin>>rno;

cout<<"\n\tEnter Name : ";

cin>>name;

cout<<"\n\tEnter Marks : ";

cin>>marks;

cout<<"\n\tCreating file and writing the information onto the file !!!";

file<<rno<<endl;

file<<name<<endl; file<<marks<<endl; getch();}

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)

{ cout<<"\n\tFile cannot open correctly.";

exit(-1);}

cout<<"\n\tReading file contents "<<endl;

file>>rno;

file>>name;

file>>marks;

cout<<"\n\tStudent details : \n";

cout<<"\n\t Roll no. : "<<rno<<endl;

cout<<"\n\t Name : "<<name<<endl;

cout<<"\n\t Marks : "<<marks<<endl;

getch();

DAVIET Page 68
2012

DAVIET Page 69
2012

Program 30: Write a program to implement object slicing.


#include<iostream.h>

#include<conio.h>

class base

public:

int a;

void getdata()

cout<<"Enter a number"<<endl;

cin>>a;

void showdata()

cout<<"a="<<" "<<a<<endl;

};

class derived: public base

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

Program 31: Write a program to pass templates to a function.


#include<iostream.h>

#include<conio.h>

template<class T>

void add(T a,T b)

{ 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

Program 32: Write a program to overload template function.


#include<iostream.h>

#include<conio.h>

template<class T>

void show(T a)

{ cout<<"\n\tTemplate Display : "<<a<<endl; }

void show(int a)

{ cout<<"\n\tFunction Display : "<<a<<endl; }

void main()

{ clrscr();

show(22);

show(31.37);

getch();

DAVIET Page 75
2012

DAVIET Page 76

You might also like