C++ Practical Programs- 2
C++ Practical Programs- 2
Page
S.No. Date Topic Sign
No.
07-07-2023 Function Overloading , Default Argument and
1.
Inline function
2. 10-07-2023 Class and Object
3. 18-07-2023 Passing Objects to Function(call by value)
4. 24-07-2023 Friend Function
5. 31-07-2023 Passing Objects to Functions(call by reference)
6. 07-08-2023 Constructor and Destructor
7. 15-08-2023 Unary Operator
8. 21-08-2023 Binary Operator
9. INHERITANCE
28-08-2023 (a)Single inheritance
11-09-2023 (b)Multiple inheritance
19-09-2023 (c)Multilevel inheritance
25-09-2023 (d)Hierarchical inheritance
10. 26-09-2023 Virtual Function
11. 03-10-2023 Manipulate a Text File
09-10-2023 Sequential I/O Operation on File
12.
13. 10-10-2023 Find the Biggest Number using Command Line
Argument
14. 16-10-2023 Class Template
15. 17-10-2023 Function Template
16. 25-10-2023 Exception Handling
10)virtual functions
AIM
To implement the virtual functions.
ALGORITHM
step 1: Start the program.
step 2: Declare the class base
step 3: Declare the virtual function and member function.
step 4: Create a derived class to inherit the base class with the member function print() and
show().
step 5: Main function can be declared and defined.
step 6: Create a pointer object*bptr.
step 7: Create a object d for derived class.
step 8: Call the virtual function binding at run time.
step 9: Call the non virtual function binding at compile time.
step 10: Stop the process.
Program
#include <iostream.h>
#include<conio.h>
class base
{
public:
virtual void print()
{
cout << "print base class" << endl;
}
void show()
{
cout << "show base class" << endl;
}
};
class derived : public base
{
public:
void print()
{
cout << "print derived class" << endl;
}
void show()
{
cout << "show derived class" << endl;
}
};
void main()
{
base* bptr;
derived d;
bptr = &d;
clrscr();
cout<<"\n\t\t\tVIRTUAL FUNCTION"; cout<<"\n\t\t\t \n";
bptr->print();
bptr->show();
getch();
}
Output
Print derived class
Show base class
RESULT
Thus the program has been completed successfully.
11)MANIPULATE A TEXT FILE
AIM
To implement the manipulate a text file.
ALGORITHM
step 1: Start the program.
step 2: Declare the data member outfile of the type of stream.
step 3: Open the file sample.txt,ios::app.
step 4: Assigning the value for grade1,grade2,grade3.
step 5: Display the output of grade1,grade2,grade3 of outfile and close the outfile.
step 6: Create a data member in file of the type if stream.
step 7: Open the infile”sample.txt”.
step 8: Checking the condition while(infile>>grade)and display the grade a.
step 9: Close the infile.
step 10: Stop the program.
Program
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
ofstream outFile;
clrscr();
cout<<"\n\t\t\tManipulation of Text file";
cout<<"\n\t\t\t \n";
outFile.open("sample.txt", ios::app);
int grade1 = 91;
int grade2 = 87;
int grade3 = 93;
outFile << grade1 << endl;
outFile << grade2 << endl;
outFile << grade3 << endl;
outFile.close();
cout<<"\nReading from file\n";
ifstream inFile;
inFile.open("sample.txt");
int grade;
while (inFile >> grade)
{
cout << grade<<"\t\n";
}
inFile.close();
getch();
}
Output:
Reading from file
91
87
93
Result
Thus the program has been completed successfully.
Program
#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>
#include<conio.h>
void main()
{
char fname[20], ch;
ifstream fin;
clrscr();
cout<<"\n\t\t\tSEQUENTIAL I/O OPERATION ON FILE";
cout<<"\n\t\t\t "; cout<<"\nEnter the name of the file: ";
cin.get(fname, 20);
cin.get(ch);
fin.open(fname, ios::in);
if(!fin)
{
cout<<"Error occurred in opening the file..!!\n";
cout<<"Press any key to exit...\n";
getch();
exit(1);
}
while(fiin)
{
fin.get(ch);
cout<<ch;
}
cout<<"\nPress any key to exit...\n";
fin.close();
getch();
}
Output
Result
Thus the program has been completed successfully.
14)class template
AIM
To implement the Class Template.
ALGORITHM
step 1: Start the program.
step 2: Declare the class calculator.
step 3: Declare the private data member of the class template T.
step 4: Declare and define member function calculator(tn1,tn2)and assign the value
num1=n1,num2=n2.
step 5: Define the member function displayresult() and call the function.
step 6: Calculate the values of addition, subtraction, multiplication and division using class template.
step 7: Declare the main function and create the objects.
step 8: Passing the values to the function.
step 9: Activate the class template.
step 10: Stop the program.
PROGRAM
#include <iostream.h>
#include<conio.h>
template <class T>
class Calculator
{
private:
T num1, num2;
public:
Calculator(T n1, T n2)
{
num1 = n1; num2 = n2;
}
void displayResult()
{
cout << "\nNumbers are: " << num1 << " and " << num2 << ".\n" ;
cout << "\tAddition is: " << add() << endl;
cout << "\tSubtraction is: " << subtract() << endl; cout << "\tProduct is: " <<
multiply() << endl;
cout << "\tDivision is: " << divide() << endl;
}
T add() { return num1 + num2; }
T subtract() { return num1 - num2;}
T multiply() { return num1 * num2; }
T divide() { return num1 / num2; }
};
int main()
{
Calculator<int> intCalc(2, 1);
Calculator<float> floatCalc(2.4, 1.2);
cout<<"\n\t\t\t CLASS TEMPLATE";
cout<<"\n\t\t\t \n";
cout<<"Integer results:";
cout<<"\n \n";
int Calc.displayResult();
cout<<"\nFloat results:";
cout<<"\n ";
floatCalc.displayResult();
return 0;
}
OUTPUT
Result
Thus the program has been completed successfully.
15)FUNCTION TEMPLATE
AIM
16)EXCEPTION HANDLING
AIM
To implement the exception handling.
ALGORITHM
step 1: Start the program.
step 2: Declare the main function.
step 3: Assign and declare the data member.
step 4: Call the exception handling try.
step 5: Call the exception handling catch.
step 6: Display the result according to the condition.
step 7: Stop the program.
PROGRAM
#include <iostream>
#include<conio.h>
void main ()
{
int x = -1;
cout<<"\n\t\t\tEXCEPTION HANDLING";
cout<<"\n\t\t\t \n";
cout << "\nBefore try \n";
try
{
cout << "Inside try \n";
if (x < 0)
{
throw x;
cout << "After throw (Never executed) \n";
}
}
catch (int x )
{
cout << "Exception Caught \n";
}
cout << "After catch (Will be executed) \n";
getch();
}
OUTPUT
Result
Thus the program has been completed successfully.