COMPUTER PRACTICAL FILE Using concepts of Object Oriented Programming with C++ SUSHANT MONGIA A2305111007 2-ECE-01-(X)
Program List
Date:
1) WAP in C++ to find whether the number entered by the user is even or odd.
14/12/2011
2) WAP in C++ to print the table of a number entered by the user.
14/12/2011
3) WAP in C++ to find the factorial of a number entered by the 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 functions definitions inside the class.
04/01/2012
7) WAP in C++ using friend function to add two complex numbers 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 a class employee and accepting and displaying multiple datasets 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
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 exception handling. No output
29/02/2012
20)Write a program to copy the contents of one file to other. No output
07/03/2012
21)Write a program to read from two files simultaneously.
14/03/2012
1. WAP in C++ to find whether the number entered by the user is even or odd. #include
#include void main () { int i; cout << "Enter an integer"<< endl; cin >> i; if ( i % 2== 0 ) cout << i << " is even"<
Output: Enter an integer 2 2 is even
2. WAP in C++ to print the table of a number entered by the user. #include #include 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
3. WAP in C++ to find the factorial of a number entered by the user. #include #include 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
4. WAP in C++ to swap two numbers using call by value. #include #include 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
5. WAP in C++ to implement inline function. #include #include 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
6. WAP in C++ to implement friend function with functions definitions inside the class. #include #include 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(); }
7. WAP in C++ using friend function to add two complex numbers with functions definitions outside the class. #include #include 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 ;
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. WAP in C++ using static data member as a counter. #include #include 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 ;
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
9. WAP in C++ to implement array of objects, creating a class employee and accepting and displaying multiple datasets accepted by the user using array of objects. #include #include 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" ;
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
Output:
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 : 32 Manager1 Name : ABC Age : 25 Manager2 Name : DEF Age :28 Manager3 Name : GHI Age : 32
10.
WAP in C++ to implement default constructor.
#include #include 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.
#include #include 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.
#include #include class code { int id ; public: code ( ) {} code ( int a ) { id = a ; } code ( code & x) { id = x.id ; } void display(void) { cout << id ; } };
void main ()
{ 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.
#include #include 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()
{ 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 () { Dd; d.set_ab(); d.mul(); d.show_a(); d.display();
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.
#include #include 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:
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();
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.
#include #include 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;
} 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.
#include #include 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;
} 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;
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.
#include #include 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 ;
} 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 {
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:
Roll no. : 1234 Marks obtained : part1 = 27.5 part2 = 33 Sports wt: 6 Total score: 66.5
18.
WAP in C++ to implement virtual base class.
#include #include 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) {
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
{ 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:
Roll no. : 678 Marks obtained : part1 = 38.5 part2 = 25.5 Sports wt: 7 Total score: 63
19.
Write a program to illustrate application of exception handling.
#include #include 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";
} cout << "END";
getch(); }
20.
Write a program to copy the contents of one file to other.
#include #include #include #include #include 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) {
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";
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.
#include #include #include void main( ) { clrscr(); const int SIZE = 80; char line[SIZE]; //First creating two files ofstream 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<
fin2.getline(line,80); cout<
Output: The contents of the two files are given below: India New Delhi Japan Tokyo Sri Lanka Colombo