0% found this document useful (0 votes)
22 views16 pages

C++ Practical Programs- 2

The document outlines a series of programming topics and implementations, including function overloading, inheritance, file manipulation, command line arguments, class and function templates, and exception handling. Each section includes an aim, algorithm, program code, and output results demonstrating successful execution. The content is structured with dates and topics, indicating a curriculum or instructional guide for learning programming concepts.

Uploaded by

d8750316
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views16 pages

C++ Practical Programs- 2

The document outlines a series of programming topics and implementations, including function overloading, inheritance, file manipulation, command line arguments, class and function templates, and exception handling. Each section includes an aim, algorithm, program code, and output results demonstrating successful execution. The content is structured with dates and topics, indicating a curriculum or instructional guide for learning programming concepts.

Uploaded by

d8750316
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

INDEX

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.

12)sequential I/O operation on file


AIM
To implement the sequential I/O operation on file.
Algorithm
step 1: Start the program.
step 2: Main function can be declared.
step 3: Declare the data member fname[20},charand decale the fin data member of the type
ifstream.
step 4: Get the name and the choice.
step 5: Open the file (fname,ios::in) and check the if condition and get the choice.
step 6: According to the choice fin will be zero when of is reached.
step 7: Read and display the character.
step 8: Close the file fin
step 9: Stop the program.

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.

13)command line argument


AIM
To implement the command line argument.
ALGORITHM
step 1: Start the program.
step 2: Declare the main function with argument (argc,*argv[])
step 3: Declare the array of ten elements.
step 4: Declare the data member i,temp,j,greatest.
step 5: Initialize the j as o.
step 6: Check the condition greatest of ten numbers.
step 7: Display the result.
step 8: Stop the program.
Program
#include <iostream.h>
#include<stdlib.h>
#include<conio.h>
int main(int argc, char *argv[])
{
int c[10];
int i,temp,j,greatest; j = 0;
for(i=1; i<argc; i++)
{
temp = atoi(argv[i]); c[j] = temp;
j++;
} greatest = c[0];
for (i = 0; i < 10; i++)
{
if (c[i] > greatest) { greatest = c[i];
}
}
cout<<"Greatest of ten numbers is "<<greatest;
return 0;
}
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

To implement the function template.


ALGORITHM

step 1: Start the program.


step 2: Create the function template and check the condition using conditional operator.
step 3: Declare the main function.
step 4: Create the data member of int( i1,i2)float( f1,f2) and char( c1,c2).
step 5: Get the values of data members.
step 6: Check the condition of the values using function templates and display the result.
step 7: Stop the program.
PROGRAM
#include <iostream.h>
#include<conio.h>
template <class T> T Large(T n1, T n2)
{
return (n1 > n2) ? n1 : n2;
}
int main()
{
int i1, i2; float f1, f2; char c1, c2;
clrscr();
cout<<"\n\t\t\tFUNCTION TEMPLATE";
cout<<"\n\t\t\t \n";
cout << "Enter two integers:\n";
cin >> i1 >> i2;
cout << Large(i1, i2) <<" is larger." << endl;
cout << "\nEnter two floating-point numbers:\n";
cin >> f1 >> f2;
cout << Large(f1, f2) <<" is larger." << endl;
cout << "\nEnter two characters:\n"; cin >> c1 >> c2;
cout << Large(c1, c2) << " has larger ASCII value.";
return 0;
}
OUTPUT
Result

Thus the program has been completed successfully.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy