Practical File Object Oriented Programming Using C++ Submitted to: Signature Submitted by: P a g e 1
Acknowledgemen t I have taken efforts in this practical file. I am highly indebted to the C++ programming teacher for her guidance and constant supervision as well as for providing necessary information regarding the programs and also for her support in completing the practical file. I would like to express my gratitude towards my parents for their kind co-operation and encouragement which helped me in the completion of this practical file. My thanks and appreciations also go to my classmates in developing the practical file and to the people who have willingly helped me out with their abilities. Place: Date: P a g e 2
Name: Signature
Table of Contents S.No. Title Page No. Remarks 1. Program to add two numbers. 2. Program to print first 10 natural numbers. 3. Program to print sum of first 10 natural numbers. 4. Program to check whether the entered number is prime or not. 5. Program to print factorial of a number. 6. Program to print Fibonacci series. 7. Program to check whether the number is odd or even. 8. Program to print day of the week based on the day number entered. 9. Program to print first 10 odd natural numbers using while & do while loop. 10. Program to input a variable in hexadecimal form and display the number in different notations like hexadecimal, octal & decimal. 11. Program to input a variable in octal form and display the variable in different number system using setbase(b) manipulator. 12. Program to calculate the area of a rectangle using objects & classes. P a g e 3
13. Program to calculate the area of a rectangle by defining member function outside the class. 14. Program to understand the concept of static data members. 15. Program to show the concept of function overloading to calculate area where same name functions differ in number of parameters. 16. Program to illustrate the use of constructor member function to initialize an object during its creation. 17. Program to initialize an object with different set of values using parameterized constructor. 18. Program to understand the use of copy constructor. 19. Program to demonstrate the use of constructor overloading and destructor. 20. Program to demonstrate how non- member function can access private data members of a class. 21. Program to demonstrate how a friend function acts as a bridge between two classes. 22. Program to illustrate that a member function of one class can act as a friend of another class. P a g e 4
23. Program to illustrate how to declare the entire class as a friend of another class. 24. Program to increment a number by 1 without overloading the operator. 25. Program to overload unary increment operator (++). 26. Program to overload a unary operator using friend function. 27. Program to compare the date of birth of two persons by overloading the equality operator (==). 28. Program to add two time objects by overloading += operator. 29. Program to illustrate how to derive a class from a base class. 30. Program to implement multilevel inheritance. 31. Program to implement hierarchical inheritance. P a g e 5
32. Program to demonstrate multiple inheritance in which a class is derived publicly from both the base classes. 33. Program to understand hybrid inheritance, consider an example of calculating the result of a student on the basis of marks obtained in internal and external examination. 34. Program to calculate the result of the student using private inheritance. 35. Program to demonstrate the use of protected members. 36. Program to illustrate the concept of overriding member functions. 37. Program to maintain student information by creating a composite class having one of its data member as an object of another predefined class. P a g e 6
P a g e 7
// Program to add two numbers. #include<iostream.h> #include<conio.h> void main(){ int a, b; clrscr(); cout << "Enter value of a: "; cin >> a; cout << "Enter value of b: "; cin >> b; cout << "Sum = " << (a + b); getch(); } P a g e 8
P a g e 9
// Program to print first 10 natural numbers. #include<iostream.h> #include<conio.h> void main(){ int i; clrscr(); cout << "First 10 natural numbers: "; for(i = 1; i <= 10; i++) cout << i << " "; getch(); } P a g e 10
P a g e 11
// Program to print sum of first 10 natural numbers. #include<iostream.h> #include<conio.h> void main(){ int i, sum = 0; clrscr(); cout << "First 10 natural numbers: "; for(i = 1; i <= 10; i++){ cout << i << " "; sum += i; } cout << "nSum = " << sum; getch(); } P a g e 12
P a g e 13
// Program to check whether the entered number is prime or not. #include<iostream.h> #include<conio.h> void main(){ int i, num, count = 0; clrscr(); cout << "Enter any number: "; cin >> num; for(i = 1; i <= num; i++) { if((num % i) == 0) count++; } if(count == 2) cout << num << " is a prime number."; else cout << num << " is not a prime number."; getch(); } P a g e 14
P a g e 15
// Program to print factorial of a number. #include<iostream.h> #include<conio.h> void main(){ clrscr(); int i, num, fact = 1; cout << "nEnter any number: "; cin >> num; for(i = 1; i <= num; i++) fact *= i; cout << "Factorial of " << num << " = " << fact; getch(); } P a g e 16
P a g e 17
// Program to print Fibonacci series. #include<iostream.h> #include<conio.h> void main(){ int i, n, a = 0, b = 1, c; clrscr(); cout << "Enter no. of terms: "; cin >> n; if(n == 1) cout << "Fibonacci series: " << a << " "; else cout << "Fibonacci series: " << a << " " << b << " "; for(i = 3; i <= n; i++){ c = a + b; cout << c << " "; a = b; b = c; } getch(); } P a g e 18
P a g e 19
// Program to check whether the no. is odd or even. #include<iostream.h> #include<conio.h> void main(){ int num; clrscr(); cout << "Enter a number: "; cin >> num; if((num % 2) == 0) cout << num << " is an even number."; else cout << num << " is an odd number."; getch(); } P a g e 20
P a g e 21
// Program to print day of the week based on the day number entered. #include<iostream.h> #include<conio.h> void main(){ int dnum; clrscr(); cout << "Enter any day number (1-7): "; cin >> dnum; switch(dnum){ case 1: cout << "Sunday"; break; case 2: cout << "Monday"; break; case 3: cout << "Tuesday"; break; case 4: cout << "Wednesday"; break; case 5: cout << "Thursday"; break; case 6: cout << "Friday"; break; case 7: cout << "Saturday"; break; default: cout << "Invalid day no."; } getch(); } P a g e 22
P a g e 23
// Program to print first 10 odd natural numbers using while & do while loop. #include<iostream.h> #include<conio.h> void main(){ clrscr(); int i = 1, x = 1; cout << "While loop: " << endl; cout << "First 10 odd natural numbers: "; while(i <= 10){ cout << x << " "; x += 2; i++; } i = 1; x = 1; cout << "nnDo while loop: " << endl; cout << "First 10 odd natural numbers: "; do{ cout << x << " "; x += 2; i++; }while(i <= 10); getch(); } P a g e 24
P a g e 25 // Program to enter a variable in hexadecimal form and display the number in different notations like hexadecimal, octal & decimal. #include<iostream.h> #include<conio.h> #include<iomanip.h> void main(){ clrscr(); int i; cout << "nEnter a hexadecimal no.: "; cin >> hex >> i; cout << "nValue in hex format = " << hex << i; cout << "nValue in octal format = " << oct << i; cout << "nValue in decimal format = " << dec << i; getch(); } P a g e 26
P a g e 27
// Program to input a variable in octal form and display the variable in different number system using setbase(b). . #include<iostream.h> #include<conio.h> #include<iomanip.h> void main(){ clrscr(); int num; cout << "nEnter value of num in octal form: "; cin >> setbase(8) >> num; cout << "nValue in hex form = " << setbase(16) << num; cout << "nValue in oct form = " << setbase(8) << num; cout << "nValue in dec form = " << setbase(10) << num; getch(); } P a g e 28

Practical File of c++.docx lab manual program question

  • 1.
    Practical File Object Oriented ProgrammingUsing C++ Submitted to: Signature Submitted by: P a g e 1
  • 2.
    Acknowledgemen t I have takenefforts in this practical file. I am highly indebted to the C++ programming teacher for her guidance and constant supervision as well as for providing necessary information regarding the programs and also for her support in completing the practical file. I would like to express my gratitude towards my parents for their kind co-operation and encouragement which helped me in the completion of this practical file. My thanks and appreciations also go to my classmates in developing the practical file and to the people who have willingly helped me out with their abilities. Place: Date: P a g e 2
  • 3.
  • 4.
    Table of Contents S.No.Title Page No. Remarks 1. Program to add two numbers. 2. Program to print first 10 natural numbers. 3. Program to print sum of first 10 natural numbers. 4. Program to check whether the entered number is prime or not. 5. Program to print factorial of a number. 6. Program to print Fibonacci series. 7. Program to check whether the number is odd or even. 8. Program to print day of the week based on the day number entered. 9. Program to print first 10 odd natural numbers using while & do while loop. 10. Program to input a variable in hexadecimal form and display the number in different notations like hexadecimal, octal & decimal. 11. Program to input a variable in octal form and display the variable in different number system using setbase(b) manipulator. 12. Program to calculate the area of a rectangle using objects & classes. P a g e 3
  • 5.
    13. Program tocalculate the area of a rectangle by defining member function outside the class. 14. Program to understand the concept of static data members. 15. Program to show the concept of function overloading to calculate area where same name functions differ in number of parameters. 16. Program to illustrate the use of constructor member function to initialize an object during its creation. 17. Program to initialize an object with different set of values using parameterized constructor. 18. Program to understand the use of copy constructor. 19. Program to demonstrate the use of constructor overloading and destructor. 20. Program to demonstrate how non- member function can access private data members of a class. 21. Program to demonstrate how a friend function acts as a bridge between two classes. 22. Program to illustrate that a member function of one class can act as a friend of another class. P a g e 4
  • 6.
    23. Program toillustrate how to declare the entire class as a friend of another class. 24. Program to increment a number by 1 without overloading the operator. 25. Program to overload unary increment operator (++). 26. Program to overload a unary operator using friend function. 27. Program to compare the date of birth of two persons by overloading the equality operator (==). 28. Program to add two time objects by overloading += operator. 29. Program to illustrate how to derive a class from a base class. 30. Program to implement multilevel inheritance. 31. Program to implement hierarchical inheritance. P a g e 5
  • 7.
    32. Program todemonstrate multiple inheritance in which a class is derived publicly from both the base classes. 33. Program to understand hybrid inheritance, consider an example of calculating the result of a student on the basis of marks obtained in internal and external examination. 34. Program to calculate the result of the student using private inheritance. 35. Program to demonstrate the use of protected members. 36. Program to illustrate the concept of overriding member functions. 37. Program to maintain student information by creating a composite class having one of its data member as an object of another predefined class. P a g e 6
  • 8.
    P a ge 7
  • 9.
    // Program toadd two numbers. #include<iostream.h> #include<conio.h> void main(){ int a, b; clrscr(); cout << "Enter value of a: "; cin >> a; cout << "Enter value of b: "; cin >> b; cout << "Sum = " << (a + b); getch(); } P a g e 8
  • 10.
    P a ge 9
  • 11.
    // Program toprint first 10 natural numbers. #include<iostream.h> #include<conio.h> void main(){ int i; clrscr(); cout << "First 10 natural numbers: "; for(i = 1; i <= 10; i++) cout << i << " "; getch(); } P a g e 10
  • 12.
    P a ge 11
  • 13.
    // Program toprint sum of first 10 natural numbers. #include<iostream.h> #include<conio.h> void main(){ int i, sum = 0; clrscr(); cout << "First 10 natural numbers: "; for(i = 1; i <= 10; i++){ cout << i << " "; sum += i; } cout << "nSum = " << sum; getch(); } P a g e 12
  • 14.
    P a ge 13
  • 15.
    // Program tocheck whether the entered number is prime or not. #include<iostream.h> #include<conio.h> void main(){ int i, num, count = 0; clrscr(); cout << "Enter any number: "; cin >> num; for(i = 1; i <= num; i++) { if((num % i) == 0) count++; } if(count == 2) cout << num << " is a prime number."; else cout << num << " is not a prime number."; getch(); } P a g e 14
  • 16.
    P a ge 15
  • 17.
    // Program toprint factorial of a number. #include<iostream.h> #include<conio.h> void main(){ clrscr(); int i, num, fact = 1; cout << "nEnter any number: "; cin >> num; for(i = 1; i <= num; i++) fact *= i; cout << "Factorial of " << num << " = " << fact; getch(); } P a g e 16
  • 18.
    P a ge 17
  • 19.
    // Program toprint Fibonacci series. #include<iostream.h> #include<conio.h> void main(){ int i, n, a = 0, b = 1, c; clrscr(); cout << "Enter no. of terms: "; cin >> n; if(n == 1) cout << "Fibonacci series: " << a << " "; else cout << "Fibonacci series: " << a << " " << b << " "; for(i = 3; i <= n; i++){ c = a + b; cout << c << " "; a = b; b = c; } getch(); } P a g e 18
  • 20.
    P a ge 19
  • 21.
    // Program tocheck whether the no. is odd or even. #include<iostream.h> #include<conio.h> void main(){ int num; clrscr(); cout << "Enter a number: "; cin >> num; if((num % 2) == 0) cout << num << " is an even number."; else cout << num << " is an odd number."; getch(); } P a g e 20
  • 22.
    P a ge 21
  • 23.
    // Program toprint day of the week based on the day number entered. #include<iostream.h> #include<conio.h> void main(){ int dnum; clrscr(); cout << "Enter any day number (1-7): "; cin >> dnum; switch(dnum){ case 1: cout << "Sunday"; break; case 2: cout << "Monday"; break; case 3: cout << "Tuesday"; break; case 4: cout << "Wednesday"; break; case 5: cout << "Thursday"; break; case 6: cout << "Friday"; break; case 7: cout << "Saturday"; break; default: cout << "Invalid day no."; } getch(); } P a g e 22
  • 24.
    P a ge 23
  • 25.
    // Program toprint first 10 odd natural numbers using while & do while loop. #include<iostream.h> #include<conio.h> void main(){ clrscr(); int i = 1, x = 1; cout << "While loop: " << endl; cout << "First 10 odd natural numbers: "; while(i <= 10){ cout << x << " "; x += 2; i++; } i = 1; x = 1; cout << "nnDo while loop: " << endl; cout << "First 10 odd natural numbers: "; do{ cout << x << " "; x += 2; i++; }while(i <= 10); getch(); } P a g e 24
  • 27.
    P a ge 25 // Program to enter a variable in hexadecimal form and display the number in different notations like hexadecimal, octal & decimal. #include<iostream.h> #include<conio.h> #include<iomanip.h> void main(){ clrscr(); int i; cout << "nEnter a hexadecimal no.: "; cin >> hex >> i; cout << "nValue in hex format = " << hex << i; cout << "nValue in octal format = " << oct << i; cout << "nValue in decimal format = " << dec << i; getch(); } P a g e 26
  • 28.
    P a ge 27
  • 29.
    // Program toinput a variable in octal form and display the variable in different number system using setbase(b). . #include<iostream.h> #include<conio.h> #include<iomanip.h> void main(){ clrscr(); int num; cout << "nEnter value of num in octal form: "; cin >> setbase(8) >> num; cout << "nValue in hex form = " << setbase(16) << num; cout << "nValue in oct form = " << setbase(8) << num; cout << "nValue in dec form = " << setbase(10) << num; getch(); } P a g e 28