Upload
mongiaamity
View
249
Download
6
Embed Size (px)
Citation preview
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 1/46
COMPUTERPRACTICAL FILE
Using concepts of ObjectOriented Programming
with C++
SUSHANT MONGIA
A2305111007
2-ECE-01-(X)
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 2/46
Program List Date:
1) WAP in C++ to find whether the number entered by theuser is even or odd.
14/12/2011
2) WAP in C++ to print the table of a number entered bythe user.
14/12/2011
3) WAP in C++ to find the factorial of a number entered bythe user.
21/12/2011
4) WAP in C++ to swap two numbers using call by value. 21/12/2011
5) WAP in C++ to implement inline function. 04/01/2012
6) WAP in C++ to implement friend function with functionsdefinitions inside the class.
04/01/2012
7) WAP in C++ using friend function to add two complexnumbers with functions definitions outside the class.
11/01/2012
8) WAP in C++ using static data member as a counter. 11/01/2012
9) WAP in C++ to implement array of objects, creating aclass employee and accepting and displaying multipledatasets accepted by the user using array of objects.
18/01/2012
10) WAP in C++ to implement default constructor. 18/01/2012
11) WAP in C++ to implement parameterized constructor. 25/01/2012
12) WAP in C++ to implement copy constructor. 25/01/2012
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 3/46
13) WAP in C++ to implement Single inheritance. 01/02/2012
14) WAP in C++ to implement multi-level inheritance. 01/02/2012
15) WAP in C++ to implement multiple inheritance. 08/02/2012
16) WAP in C++ to implement hierarchical inheritance. 08/02/2012
17) WAP in C++ to implement hybrid inheritance. 22/02/2012
18) WAP in C++ to implement virtual base class. 22/02/2012
19) Write a program to illustrate application of exceptionhandling.
No output
29/02/2012
20) Write a program to copy the contents of one file toother.
No output
07/03/2012
21) Write a program to read from two files simultaneously. 14/03/2012
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 4/46
1. WAP in C++ to find whether the number entered by the user is even or odd.
#include<iostream.h>
#include<conio.h>
void main ()
{
int i;
cout << "Enter an integer"<< endl;
cin >> i;
if ( i % 2== 0 )cout << i << " is even"<<endl;
else
cout << I << " is odd "<<endl;
getch();
}
Output:
Enter an integer
2
2 is even
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 5/46
2. WAP in C++ to print the table of a number entered by the user.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a ;
cout << "Enter an integer" << endl ;
cin >> a ;
for ( int i = 1 ; i <= 10 ; i++)cout << a << " x " << i << " = " << i*a << "\n" ;
getch();
}
Output:
Enter an integer
5
5 x 1 =5
5 x 2 =10
5 x 3 =15
5 x 4 =20
5 x 5 =25
5 x 6 =30
5 x 7 =35
5 x 8 =40
5 x 9 =45
5 x 10 =50
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 6/46
3. WAP in C++ to find the factorial of a number entered by the user.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a ;
cout << " Please enter an integer " << endl ;
cin >> a ;
for ( int i = a - 1 ; i >= 1 ; i--)a = a * i ;
cout << " Factorial = " << a << "\n" ;
getch();
}
Output:
Enter an integer
4
Factorial = 24
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 7/46
4. WAP in C++ to swap two numbers using call by value.
#include<iostream.h>
#include<conio.h>
void swap ( int , int ) ;
void swap ( int x , int y )
{
int temp;
temp = x;
x = y; y = temp;
cout << "\n After swapping \n";
cout << " a = " << x <<"\n";
cout << " b = " << y ;
}
void main ()
{
int a , b ;
cout << " Enter a " ;
cin >> a ; Output:
cout << " Enter b "; Enter a 20
cin >> b ; Enter b 65
swap( a , b ); After swapping
getch(); a = 65
} b = 20
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 8/46
5. WAP in C++ to implement inline function.
#include<iostream.h>
#include<conio.h>
inline float mul ( float x , float y )
{
return ( x * y ) ;
}
inline double div ( double p , double q )
{return ( p / q ) ;
}
void main ()
{
float a = 12.345 , b = 9.82 ;
cout << mul(a,b) << "\n" ;
cout << div(a,b) << "\n" ;
getch();
}
Output:
121.227898
1.257128
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 9/46
6. WAP in C++ to implement friend function with functions definitions inside theclass.
#include<iostream.h>
#include<conio.h>
class sample
{
int a;
int b;
public:
void setvalue() { a=25 ; b=40 ; }
friend float mean( sample s ) ;
};
float mean ( sample s)
{
return float ( s.a + s.b ) / 2.0 ;
} Output:
void main () Mean value = 32.5
{
sample x;
x.setvalue();
cout << "Mean value = " << mean(x) << "\n" ;
getch();
}
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 10/46
7. WAP in C++ using friend function to add two complex numbers with functionsdefinitions outside the class.
#include<iostream.h>
#include<conio.h>
int r , i ;
class complex
{
int real;
int imag;
public:
void get() ;
friend void add() ;
} c1 , c2 ;
void complex :: get()
{
cout << "\n Enter real part: " ;
cin >> real ;
cout << "Enter imaginary part: ";
cin >> imag ;
}
void add()
{
r = c1.real + c2.real ;
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 11/46
i = c1.imag + c2.imag ;
}
void main ()
{
c1.get();
c2.get();
add();
cout << " \n complex no is " << r << " + i " << i ;getch();
}
Output:
Enter real part: 10
Enter imaginary part: 21
Enter real part: 5
Enter imaginary part: 7
complex no is 15 + i 28
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 12/46
8. WAP in C++ using static data member as a counter.
#include<iostream.h>
#include<conio.h>
class item
{
static int count;
int number;
public:
void getdata( int a ){
number = a ;
count ++ ;
}
void getcount( void )
{
cout << " count : " ;
cout << count << "\n" ;
}
} ;int item :: count ;
void main ()
{
item a , b , c ;
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 13/46
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
cout << " After reading data " << "\n" ;
a.getcount();
b.getcount();c.getcount();
getch();
}
Output:
count : 0
count : 0
count : 0
After reading data
count : 3
count : 3
count : 3
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 14/46
9. WAP in C++ to implement array of objects, creating a class employee andaccepting and displaying multiple datasets accepted by the user using array ofobjects.
#include<iostream.h>
#include<conio.h>
class employee
{
char name[30];
float age;
public:
void getdata( void ) ;
void putdata( void ) ;
} ;
void employee :: getdata(void)
{
cout << "Enter name : " ;
cin >> name ;
cout << "Enter age : " ;
cin >> age ;
}
void employee :: putdata(void)
{
cout << "Name : " << name << "\n" ;
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 15/46
cout << "Age : " << age << "\n" ;
}
const int size=3 ;
void main ()
{
employee manager[size];
for(int i = 0 ; i < size ; i++)
{
cout << "\n Details of manager" << i+1 << "\n" ;manager[i].getdata();
}
cout << "\n" ;
for(i=0;i<size;i++)
{
cout << " \n Manager " << i+1 << "\n" ;
manager[i].putdata();
}
getch();
}
Output:
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 16/46
Details of manager1
Enter name : ABC
Enter age : 25
Details of manager2
Enter name : DEF
Enter age :28
Details of manager3
Enter name : GHI
Enter age : 32Manager1
Name : ABC
Age : 25
Manager2
Name : DEF
Age :28
Manager3
Name : GHI
Age : 32
10. WAP in C++ to implement default constructor.
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 17/46
#include<iostream.h>
#include<conio.h>
class defa
{
int a ;
int b ;
public:
defa ( )
{cout << "Default constructor is called" << "\n" ;
}
} ;
void main ()
{
defa D ;
getch();
}
Output:Default constructor is called
11. WAP in C++ to implement parameterized constructor.
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 18/46
#include<iostream.h>
#include<conio.h>
class para
{
int a ;
int b ;
public:
para ( int x , int y )
{a = x ;
b = y ;
}
void disp()
{
cout << "a = " << a << "\n b = " << b ;
}
} ;
void main ()
{
para P( 100 , 200 ) ; Output:
P.disp(); a = 100
getch(); b = 200
}
12. WAP in C++ to implement copy constructor.
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 19/46
#include<iostream.h>
#include<conio.h>
class code
{
int id ;
public:
code ( )
{ }
code ( int a ){
id = a ;
}
code ( code & x)
{
id = x.id ;
}
void display(void)
{
cout << id ;
}} ;
void main ()
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 20/46
{
code A(100);
code B(A);
code C = A;
code D;
D = A;
cout << "\n id of A: " ; A.display() ;
cout << "\n id of B: " ; B.display() ;
cout << "\n id of C: " ; C.display() ;cout << "\n id of D: " ; D.display() ;
getch();
}
Output:
id of A: 100
id of B: 100
id of C: 100
id of D: 100
13. WAP in C++ to implement Single inheritance.
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 21/46
#include<iostream.h>
#include<conio.h>
class B
{
int a ;
public:
int b ;
void set_ab();
int get_a();void show_a();
} ;
class D : public B
{
int c ;
public:
void mul ();
void display();
};
void B :: set_ab()
{a = 5 ;
b = 10 ;
}
int B :: get_a()
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 22/46
{
return a ;
}
void B :: show_a()
{
cout << "a = " << a << "\n" ;
}
void D :: mul ()
{c = b * get_a() ;
}
void D :: display()
{
cout << "a = " << get_a() << "\n";
cout << "b = " << b << "\n" ;
cout << "c = " << c << "\n\n" ;
}
void main ()
{
D d ;d.set_ab();
d.mul();
d.show_a();
d.display();
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 23/46
d.b = 20 ;
d.mul ();
d.display();
getch();
}
Output:
a = 5
a = 5
b = 10
c = 50
a = 5
b = 20
c = 100
14. WAP in C++ to implement multi-level inheritance.
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 24/46
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll_number;
public:
void get_number(int);
void put_number(void);} ;
void student :: get_number(int a)
{
roll_number = a ;
}
void student :: put_number()
{
cout << "Roll number : " << roll_number << "\n" ;
}
class test : public student
{protected:
float sub1;
float sub2;
public:
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 25/46
void get_marks(float,float);
void put_marks(void);
};
void test :: get_marks(float x, float y)
{
sub1 = x;
sub2 = y;
}
void test :: put_marks(){
cout << "Marks in sub 1 = " << sub1 << "\n" ;
cout << "Marks in sub 2 = " << sub2 << "\n" ;
}
class result : public test
{
float total ;
public:
void display(void);
};
void result :: display(){
total = sub1 + sub2 ;
put_number();
put_marks();
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 26/46
cout << "Total = " << total << "\n" ;
}
void main ()
{
result student1;
student1.get_number(111);
student1.get_marks(75.0,59.5);
student1.display();
getch();}
Output:
Roll number : 111
Marks in sub1 = 75
Marks in sub2 = 59.5
Total = 134.5
15. WAP in C++ to implement multiple inheritance.
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 27/46
#include<iostream.h>
#include<conio.h>
class M
{
protected:
int m ;
public:
void get_m(int);
};class N
{
protected:
int n;
public:
void get_n(int);
};
class P : public M, public N
{
public:
void display(void);};
void M :: get_m(int x)
{
m = x ;
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 28/46
}
void N :: get_n(int y)
{
n = y ;
}
void P :: display(void)
{
cout << "m = " << m << "\n" ;
cout << "n = " << n << "\n" ;cout << "m * n = " << m*n << "\n" ;
}
void main ()
{
P p;
p.get_m(10);
p.get_n(20);
p.display();
getch();
}
Output:m = 12
n = 3
m*n =36
16. WAP in C++ to implement hierarchical inheritance.
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 29/46
#include<iostream.h>
#include<conio.h>
class A
{
int a,b;
public :
void getdata()
{
cout << "\n Enter the value of a and b " ;cin >> a >> b;
}
void putdata()
{
cout << "\n The value of a is : " << a << " and b is " << b;
}
};
class B : public A
{
int c , d;
public :void indata()
{
cout << "\n Enter the value of c and d ";
cin >> c >> d;
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 30/46
}
void outdata()
{
cout << "\n The value of c " << c << " and d is " << d;
}
};
class C: public A
{
int e,f;public :
void input()
{
cout<< "\n Enter the value of e and f ";
cin >> e >> f ;
}
void output()
{
cout << " \nthe value of e is " << e << " and f is " << f;
}
};
void main()
{
B obj1;
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 31/46
C obj2;
obj1.getdata();
obj1.indata();
obj2.getdata();
obj2.input();
obj1.putdata();
obj1.outdata();
obj2.output();
getch();}
Output:
Enter the value of a and b 2 8
Enter the value of c and d 15 56
Enter the value of a and b 54 32
Enter the value of e and f 85 52
The value of a is : 2 and b is 8
The value of c is : 15 and d is 56
The value of e is : 85 and f is 52
17. WAP in C++ to implement hybrid inheritance.
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 32/46
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll_number ;
public:
void get_number(int a)
{roll_number = a ;
}
void put_number(void)
{
cout << "Roll no. : " << roll_number << "\n" ;
}
};
class test : public student
{
protected:
float part1, part2;public:
void get_marks(float x, float y)
{
part1 = x ; part2 = y ;
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 33/46
}
void put_marks(void)
{
cout << "Marks obtained : " << "\n"
<< "part1 = " << part1 << "\n"
<< "part2 = " << part2 << "\n" ;
}
};
class sports{
protected:
float score ;
public:
void get_score(float s)
{
score = s ;
}
void put_score(void)
{
cout << "Sports wt: " << score << "\n\n" ;}
};
class result : public test, public sports
{
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 34/46
float total ;
public :
void display(void);
};
void result :: display(void)
{
total = part1 + part2 + score ;
put_number();
put_marks();put_score();
cout << "Total score : " << total << "\n" ;
}
void main()
{
result student_1;
student_1.get_number(1234);
student_1.get_marks(27.5,33.0);
student_1.get_score(6.0);
student_1.display();getch();
}
Output:
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 35/46
Roll no. : 1234
Marks obtained :
part1 = 27.5
part2 = 33
Sports wt: 6
Total score: 66.5
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 36/46
18. WAP in C++ to implement virtual base class.
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll_number;
public:
void get_number(int a){
roll_number = a;
}
void put_number(void)
{
cout << "Roll No: " << roll_number << "\n";
}
};
class test : virtual public student
{
protected:float part1, part2;
public:
void get_marks(float x, float y)
{
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 37/46
part1 = x; part2 = y;
}
void put_marks(void)
{
cout << "Marks obtained: " << "\n"
<< "Part1 = " << part1 << "\n"
<< "Part2 = " << part2 << "\n";
}
};class sports : public virtual student
{
protected:
float score ;
public:
void get_score(float s)
{
score = s;
}
void put_score(void)
{cout << "Sports wt: " << score << "\n\n";
}
};
class result : public test, public sports
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 38/46
{
float total;
public:
void display(void)
{
total = part1 + part2 + score;
put_number();
put_marks();
put_score();cout << "Total score: " << total << "\n";
}
};
void main()
{
result student_1;
student_1.get_number(678);
student_1.get_marks(30.5, 25.5);
student_1.get_score(7.0);
student_1.display();
getch();}
Output:
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 39/46
Roll no. : 678
Marks obtained :
part1 = 38.5
part2 = 25.5
Sports wt: 7
Total score: 63
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 40/46
19. Write a program to illustrate application of exception handling.
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
cout << "Enter Values of a and b \n";
cin >> a;
cin >> b;int x = a-b;
try
{
if(x != 0)
{
cout << "Result (a/x) = " << a/x << "\n";
}
else // There is an exception
{
throw(x) ; // Throws int object
}}
catch (int i) // Catches the exception
{
cout<<"Exception cought: DIVIDE BY ZERO\N";
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 41/46
}
cout << "END";
getch();
}
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 42/46
20. Write a program to copy the contents of one file to other.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<process.h>
void main()
{
fstream file1, file2;char ch;
clrscr();
file1.open("file1.txt",ios :: out);
cout << "\n\t Press 'enter key' to quit \n\n";
cout << "\n Enter the data \n";
cin.get(ch);
while(ch != '\n')
{
file1.put(ch);
cin.get(ch);
}file1.close();
file1.open("file1.txt",ios :: in);
if(!file1)
{
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 43/46
cout<< "\n File1 not found !! \n";
getch();
exit(0); // aborting…
}
cout << "\n First file contents \n";
while(file1)
{
file1.get(ch);
cout.put(ch);}
file1.seekg(0); /* copying process..... */
file2.open("file2.txt",ios :: out );
cout << "\n\n First file contents copied to second file... \n";
while(file1)
{
file1.get(ch);
file2.put(ch);
}
file1.seekg(0);
file2.close();file2.open("file1.txt",ios :: in);
if(!file1)
{
cout<< "\n File1 not found !! \n";
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 44/46
getch();
exit(0); // aborting…
}
cout << "\n Second file contents \n";
while(file2)
{
file2.get(ch);
cout.put(ch);
}file2.close();
getch();
}
21. Write a program to read from two files simultaneously.
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 45/46
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
void main( )
{
clrscr();
const int SIZE = 80;
char line[SIZE];
//First creating two filesofstream fout1,fout2;
fout1.open("country");
fout1<<"India\n";
fout1<<"Japan\n";
fout1<<"Sri Lanka\n";
fout1.close();
fout2.open("capital");
fout2<<"New Delhi\n"<<"Tokyo\n"<<"Colombo\n";
fout2.close();
ifstream fin1, fin2; // create two input streams
fin1.open("country");fin2.open("capital");
cout<<"The contents of two files are given below:\n";
fin1.getline(line,80);
cout<<line<<"\n";
8/12/2019 Computer Practical File C++
http://slidepdf.com/reader/full/computer-practical-file-c 46/46
fin2.getline(line,80);
cout<<line<<"\n";
fin1.getline(line,80);
cout<<line<<"\n";
fin2.getline(line,80);
cout<<line<<"\n";
fin1.getline(line,80);
cout<<line<<"\n";
fin2.getline(line,80);cout<<line<<"\n";
fin1.close();
fin2.close();
getch();
}
Output:
The contents of the two files are given below:
India
New Delhi
Japan
Tokyo
Sri Lanka
Colombo