Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore CU-BCA-SEM-II-Object Oriented Programming-Practical Book

CU-BCA-SEM-II-Object Oriented Programming-Practical Book

Published by Teamlease Edtech Ltd (Amita Chitroda), 2020-11-28 18:49:28

Description: CU-BCA-SEM-II-Object Oriented Programming-Practical Book

Search

Read the Text Version

Object Oriented Programming Practical Course Code: BCA127 Credit: 1 Course Objectives:  To inculcate the skills related to basics of Programming and use programming in day to day Applications.  To learn about the Basics of Functions and use of classes.  To develop skills for using Files and perform operations on files. PRACTICAL UNIT 1: PROGRAM BASED ON BASICS DATA TYPES AND OPERATOR IN C++: 1 Assuming there are 7.481 gallons in a cubic foot, write a program that asks the user to enter a number of gallons, and then displays the equivalent in cubic feet. #include <iostream> using namespace std; double Convert(double); int main() { double dGallons; cout << \" -- Program to convert temperature in gallons to cubic feet -- \\n\\n\\n\"; cout << \"Enter value in Gallon : \"; cin >> dGallons; cout << \"Converted value in Cubic : \" << Convert(dGallons) << endl; return 0;

} double Convert(double a) { double dCubic = 7.481; double c = a / dCubic; return c; } Output : 2. You can convert temperature from degrees Celsius to degrees Fahrenheit by multiplying by 9/5 and adding 32. Write a program that allows the user to enter a floating-point number representing degrees Celsius, and then displays the corresponding degrees Fahrenheit. #include <iostream> using namespace std; float Convert(float); int main() { float dCelsius; cout << \" -- Program to convert temperature in Celsius to Fahrenheit -- \\n\\n\\n\"; cout << \"Enter degree in Celsius value : \"; cin >> dCelsius;

cout << \"Corresponding degree in Fahrenheit value is : \" << Convert(dCelsius) << endl;; return 0; } float Convert(float dCelsius) { float dFahrenheit = 7.481; dFahrenheit = (dCelsius * 9.0) / 5.0 + 32; return dFahrenheit; } Output : 2. Program based on conditional constructs: 1. The normal speed of a vehicle is less than 65kmph. If entered speed is less than 65kmph you can print within speed limit otherwise over speed limit. #include <iostream> using namespace std; string SpeedChecker(int); int main() { int intSpeed;

cout << \" -- Program to check speed of a vehicle -- \\n\\n\\n\"; cout << \"Enter the speed of a vehicle : \"; cin >> intSpeed; cout << endl <<SpeedChecker(intSpeed) << endl; return 0; } string SpeedChecker(int intSpeed) { int intNormalSpeed = 65; string strSpeedLimit; if (intSpeed > intNormalSpeed) { strSpeedLimit = \"Over speed limit\"; } else if (intSpeed <= intNormalSpeed) { strSpeedLimit = \"Within speed limit\"; } return strSpeedLimit; } Output:

2. Create the equivalent of a four-function calculator. The program should request the user to enter a number, an operator, and another number. (Use floating point) It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Use a switch statement to select the operation. Finally, display the result. When it finishes the calculation, the program should ask if the user wants to do another calculation. The response can be ‘y’ or ‘n’. #include <iostream> using namespace std; int main() { double INumber, IINumber, Answer; char oper, ch; do { cout << \"\\n-- Calculator --\"; cout << \"\\nEnter first number : \"; cin >> INumber ; cout << \"\\nEnter operator : \"; cin >> oper ; cout << \"\\nEnter second number : \"; cin >> IINumber; switch (oper)

{ case '+': Answer = INumber + IINumber; break; case '-': Answer = INumber - IINumber; break; case '*': Answer = INumber * IINumber; break; case '/': Answer = INumber / IINumber; break; default: Answer = 0; } cout << \"Answer = \" << Answer; cout << \"\\n\\nDo another(Enter 'y' or 'n') ? \"; cin >> ch; } while (ch != 'n'); return 0; } Output:

PRACTICAL UNIT 2: PROGRAM BASED ON POLYMORPHISM: 1. Create a class that imitates part of the functionality of the basic data type int. Call the class int(note different spelling). The only data in this class is an int variable. Include member functions to initialize an int to 0, to initialize it to an int value, to display it (it looks just like an int), and use operator overloading to add two int values. Write a program that exercises this class by creating two initialized and one uninitialized int values, adding these two initialized values and placing the response in the uninitialized value, and then displaying this result. #include <iostream> #include <conio.h> using namespace std; class Int { private: int intvar; public: Int() { intvar = 0; } Int(int x) { intvar = x; } void display() { cout << intvar << endl; }

void add(Int x, Int y) { intvar = x.intvar + y.intvar; } }; int main() { Int a(5), b(95); Int c; cout << \" -- Program based on polymorphism -- \\n\\n\\n\"; c.add(a, b); cout << \" Addition of two variables : \"; c.display(); return 0; } Output : 2. Program based on constructor and destructor Create a class called time that has separate int member data for hours, minutes, and seconds. One constructor should initialize this data to 0, and another should initialize it to fixed values. Another member function should display it, in 11:59:59 format. The final member function should add two objects of type time passed as arguments. A main () program should create two initialized time objects (should they be const?) and one that isn’t initialized. Then it should add the two initialized values together, leaving the result in the third time variable. Finally, it should display the value of this third variable.

Make appropriate member functions const. #include <iostream> #include <conio.h> using namespace std; class time { private: int hours, minutes, seconds; public: time() { hours = minutes = seconds = 0; } time(int h, int m, int s) { hours = h; minutes = m; seconds = s; } void showTime() const { cout << hours << ':' << minutes << ':' << seconds; } void addTime(time x, time y) {

seconds = x.seconds + y.seconds; if (seconds > 59) { seconds -= 60; minutes++; } minutes += x.minutes + y.minutes; if (minutes > 59) { minutes -= 60; hours++; } hours += x.hours + y.hours; } }; int main() { const time a(6, 06, 45), b(4, 03, 15); time c; cout << \" -- Program based on constructor and destructor -- \\n\\n\\n\"; c.addTime(a, b); cout << \" Time clock : \"; c.showTime(); } Output:



PRACTICAL UNIT 3: PROGRAM BASED ON POINTERS AND DYNAMIC MEMORY ALLOCATION. 1. Write a program that reads a group of numbers from the user and places them in an array of type float. Once the numbers are stored in the array, the program should average them and print the result. Use pointer notation wherever possible. #include <iostream> using namespace std; int main() { int numData, i; float numArray[50], Total = 0.0, average; cout << \"Enter the numbers of data between 1 to 50 : \"; cin >> numData; while (numData > 50 || numData <= 0) { cout << \"\\nNumbers should be in range of (1 to 50).\" << endl; cout << \"\\nEnter the number again: \"; cin >> numData; }

for (i = 0; i < numData; ++i) { cout << endl; cout << i + 1 << \". Enter number: \"; cin >> numArray[i]; Total += numArray[i]; } average = Total / numData; cout << \"\\nAverage = \" << average; return 0; } Output: 2. Program to understand the concept of new and delete operator. #include <iostream> using namespace std;

int main() { int numData,n; float fltData; cout << \" -- Program with new and delete operator -- \" ; int* ipointer = NULL; ipointer = new(nothrow) int; cout << \"\\n\\nEnter the integer number : \"; cin >> numData; *ipointer = numData; cout << \"The value of the integer pointer is: \" << *ipointer << endl; cout << \"\\n\\nEnter the decimal number : \"; cin >> fltData; float* fpointer = new float(fltData); cout << \"The value of the float pointer is: \" << *fpointer << endl; cout << \"\\n\\nEnter the numbers of data between 1 to 50 : \"; cin >> n; while (n > 50 || n <= 0) {

cout << \"\\nNumbers should be in range of (1 to 50).\" << endl; cout << \"\\nEnter the number again: \"; cin >> n; } int* pointer = new int[n]; for (int i = 0; i < n; i++) pointer[i] = i + 1; cout << \"\\n\\nThe values stored dynamically are: \" << endl; for (int i = 0; i < n; i++) cout << pointer[i] << endl; delete ipointer; delete fpointer; delete[] pointer; return 0; } Output:

3. Program based on file handling: Write a program to store the information of about 5 students in a file and read the contents and print them on screen. #include<iostream> #include<fstream> using namespace std; class student { int rollno; char studentname[30]; float totalmarks; public: student() { }

void getData(); void displayData(); }; void student::getData() { cout << \"\\nEnter Roll No. :- \"; cin >> rollno; cin.ignore(); cout << \"\\nEnter Name :- \"; cin.getline(studentname, 30); cout << \"\\nEnter Marks :- \"; cin >> totalmarks; } void student::displayData() { cout << \"\\nRoll No. :- \" << rollno << endl; cout << \"\\nName :- \" << studentname << endl; cout << \"\\nMarks :- \" << totalmarks << endl; } int main() { student s[5];

fstream file; int i; file.open(\"D:\\\\Student.txt\", ios::out); cout << \"-- File handling program -- \" << endl; cout << \"\\nWriting Student information to the file :- \" << endl; cout << \"\\nEnter 5 students Details to the File :- \" << endl; for (i = 0; i < 5; i++) { s[i].getData(); file.write((char*)&s[i], sizeof(s[i])); } file.close(); file.open(\"D:\\\\Student.txt\", ios::in); cout << \"\\nReading Student information to the file :- \" << endl; for (i = 0; i < 5; i++) { file.read((char*)&s[i], sizeof(s[i])); s[i].displayData();

} file.close(); return 0; } Output :


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook