22 CS101 Problem Solving Using C++ Lab Manual
22 CS101 Problem Solving Using C++ Lab Manual
ENGINEERING COLLEGE
(An Autonomous Institution)
R.S.M Nagar, Kavaraipettai, Gummidipoondi Taluk, Thiruvallur District - 601 206.
LABORATORY MANUAL
Name : --------------------------------------------------
Name Mr. S Vijayakumar Dr. S Radhika Dr. S Pavai Madheswari Dr. K A Mohamed Junaid
Signature
Date
PROGRAM OUTCOMES (POs)
Syllabus
Pointers -Variables – Operators – Expressions – Pointers and Arrays – Functions - Scope Rules –
Function Arguments – return Statement – Recursion – Structures – Unions – Enumerations.
Lab Exercises
1. Write C/C++ programs for the following:
a. Find the sum of individual digits of a positive integer.
b. Compute the GCD of two numbers.
c. Find the roots of a number (Newton‘s method)
2. Write C/C++ programs using arrays:
a. Find the maximum of an array of numbers.
b. Remove duplicates from an array of numbers.
c. Print the numbers in an array after removing even numbers.
3. Write C/C++ programs using strings:
a. Checking for palindrome.
b. Count the occurrences of each character in a given word.
4. Generate salary slip of employees using structures and pointers. Create a structure
Employee with the following members:
EID, Ename, Designation, DOB, DOJ, Basicpay
Note that DOB and DOJ should be implemented using structure within structure.
5. Compute internal marks of students for five different subjects using structures and
functions.
6. Write a program Illustrating Class Declarations, Definition, and Accessing Class
Members.
7. Program to illustrate default constructor, parameterized constructor and copy constructors
8. Write a Program to Demonstrate the i) Operator Overloading. ii) Function Overloading.
9. Write a Program to Demonstrate Friend Function and Friend Class.
10. Program to demonstrate inline functions.
11. Program for Overriding of member functions.
12. Write C++ programs that illustrate how the following forms of inheritance are supported:
a) Single inheritance b)Multiple inheritance c)Multi level inheritance
d)Hierarchical inheritance
13. Program to demonstrate pure virtual function implementation.
14. Count the number of account holders whose balance is less than the minimum balance
using sequential access file.
15. Write a Program to Demonstrate the Catching of all Exceptions.
Mini project.
TEXT BOOKS:
1. Herbert Schildt, “The Complete Reference C++”, 4th edition, MH, 2015. (Unit 1 & 2)
2. E Balagurusamy,”Object Oriented Programming with C++”, 4th Edition, Tata McGraw-
Hill Education, 2008. (Unit 3, 4 & 5)
REFERENCES:
1. Nell Dale, Chip Weems, “Programming and Problem Solving with C++”, 5th Edition,
Jones and Barklett Publishers, 2010.
2. John Hubbard, “Schaum's Outline of Programming with C++”, MH, 2016.
3. Yashavant P. Kanetkar, “Let us C++”, BPB Publications, 2020
4. ISRD Group, “Introduction to Object-oriented Programming and C++”, Tata McGraw-
Hill Publishing Company Ltd., 2007.
5. D. S. Malik, “C++ Programming: From Problem Analysis to Program Design”, Third
Edition, Thomson Course Technology, 2007.
6. https://infyspringboard.onwingspan.com/web/en/aptoclex_auth_01297200240671948837
_shared/overview
Course Outcomes
6 Write a program Illustrating Class Declarations, Definition, and Accessing Class Members.
d Hierarchical inheritance
13 Program to demonstrate pure virtual function implementation.
Count the number of account holders whose balance is less than the minimum balance using
14
sequential access file.
15 Write a Program to Demonstrate the Catching of all Exceptions
16 Mini Project
Date :
Exercise Number :1a
Title: Find the sum of individual digits of a positive integer
Aim:
To write a for computing sum of individual digits of a positive integer program using C / C++
Problem Description:
Sum of digits in a C program allows a user to enter any number, divide that number into individual
numbers, and sum those individual numbers.
Example 1:
Given number = 14892 => 1 + 4 + 8 + 9 + 2 = 24.
Sum of digits of a given number “14892” is 24.
Example 2:
Given number = 3721 => 3 + 7 + 2 + 1 = 13.
Sum of digits of a given number “3721” is 13.
To solve this, we will follow these steps −
o num := 58612
o sum := 0
o while num is not equal to 0, do:
sum := sum + num mod 10
num := num / 10
o return sum
Algorithm:
1. Take the number as input.
2. Divide the number by 10 and store the remainder in a variable.
3. Add the remainder to the sum.
4. Repeat the process until the number is not 0.
5. Print/return the sum.
Sample Output:
Enter an integer number: 456
Sum of digits of given integer number 456 is 15
Result:
Thus a program to compute sum of individual digits of a positive integer using C / C++ is
successfully executed.
Date :
Exercise Number :1b
Title: Compute the GCD of two numbers
Aim:
To write a program to compute GCD of two numbers using C / C++.
Problem Description:
The greatest common divisor (GCD) refers to the greatest positive integer that is a common divisor for a
given set of positive integers. It is also termed as the highest common factor (HCF) or the greatest
common factor (GCF).
Factors : Factors of a number divides the original number evenly. For example, if 8 is the factor of 64,
then 8 can divide 64 into 8 equal parts.
Common Factors : If factor of a number is a factor of another number, then it is said to be a common
factor for the two numbers. For example, 2 is a factor of 4 and 8, hence 2 is a common factor.
GCD : GCD is the greatest common factor of two or more numbers. A factor that is the highest among
the numbers.
Example:
The greatest common divisor of 12 and 26 can be calculated as:
Divisors of 12 are 1, 2, 3, 4, 6, and 12.
Divisors of 26 are 1, 2, 13, and 26.
Common divisors of 12 and 26 are 1 and 2.
Therefore the greatest common divisor of (12, 26) is 2.
Example:
Find the greatest common divisor of 15 and 70 using the LCM method.
Solution: The greatest common divisor of 15 and 70 can be calculated as:
Algorithm:
1. Start Step
2. Declare variables num1, num2, gcd = 1, i = 1
3. Input num1 and num2
4. Repeat until i <= num1 and i <= num2
a. If num1 % i == 0 && num2 % i == 0:
b. gcd = i
5. Print gcd
6. Stop
Sample Output:
Enter two integers: 10 20
G.C.D of 10 and 20 is 10
Result:
Thus a program to compute GCD of two numbers using C / C++ is successfully executed.
Date :
Exercise Number :1c
Title: Find the roots of a number (Newton‘s method)
Aim:
To write a C/C++ program to find the roots of a number (Newton‘s method)
Algorithm:
Newton’s Method:
Let N be any number then the square root of N can be given by the formula:
root = 0.5 * (X + (N / X)) where X is any guess which can be assumed to be N or 1.
In the above formula, X is any assumed square root of N and root is the correct square root of N.
Tolerance limit is the maximum difference between X and root allowed.
Sample Output:
Enter a Number : 100
Root of given number is : 10.032579
Result:
Thus a program to compute the roots of a number (Newton‘s method) using C / C++ is
successfully executed.
Date :
Exercise Number :2a
Title: Find the maximum of an array of numbers
Aim:
To write a program for finding the maximum of an array of numbers using C / C++.
Problem Description:
In this program, we need to find out the largest element present in the array and display it. This can be
accomplished by
1. Sorting : Sort the array in ascending order and the last element will be at the last index.
2. Traverse : Traverse the array while updating the max value.
1. Sorting
To find the largest element from the array, a simple way is to arrange the elements in ascending order.
After sorting, the first element will represent the smallest element, the next element will be the second
smallest, and going on, the last element will be the largest element of the array.
Solution Steps
Sort the array.
Return the element at the last index of the array.
Pseudo Code
int largestElement(int[] arr, int size)
{
// sort the array in ascending order
arr.sort()
largest_element = arr[size-1]
return largest_element
}
2. Traverse
Take a max variable and initialize it with the first element of the array. Now start iterating over the
array and whenever a larger element encountered then update the max variable otherwise, move
forward.
Solution Steps
Create a max variable and initialize it with arr[0]
Iterate from the first idx to the last idx of the array:
o If arr[idx] > max then update max with arr[idx]
Return max
Pseudo Code
int largestElement(int[] arr, int size)
{
int max = arr[0]
for(int i = 1 to i < size)
{
if(max < arr[i])
{
max = arr[i]
}
}
return max
}
Algorithm:
1. START
2. INITIALIZE arr[] = {25, 11, 7, 75, 56}
3. length= sizeof(arr) / sizeof(arr[0])
4. max = arr[0]
5. SET i = 0. REPEAT STEP 6 and STEP 7 i<length
6. if(arr[i] > max) max = arr[i]
7. I = i + 1.
8. PRINT "Largest element in given array:" assigning max.
9. RETURN 0
10. END.
Sample Output:
Largest element Present in given array: 75
Result:
Thus a program to for finding the maximum of an array of numbers using C / C++ is successfully
executed.
Date :
Exercise Number :2b
Title: Remove duplicates from an array of numbers
Aim:
To write a program to remove duplicates from an array of numbers using C / C++.
Problem Description:
Removing or deletion of the duplicate elements from an array in the C programming language is
used when the same number of elements occurs in sorted or unsorted array, These elements in the array
are called the duplicate elements. And we need to delete these duplicate elements or same number from
an array to make the resultant array consists of unique elements.
Example :
There is an integer type array intarray[10] that contains { 5, 8, 3, 3, 5, 9} elements. In this array, 3
and 5 occurs two times. Hence, these elements are duplicate elements. So, after deleting the duplicate
elements from an array intarray[], we get 5, 8, 3,9 elements.
Algorithm:
1. Start
2. Input the size of an array and store into the size variable.
3. Use for loop to read the elements of an array and store in intarray[i] variable.
4. To get the duplicate elements from an array two for loops are used.
i. Where the first loop start from 0 to size. And the structure of the loop is: for
(i = 0; i < size; i++)
ii. Another loop selects each element of the array and compare with the
corresponding element to get the duplicate elements. And the structure of the
inner loop is: for (j = i + 1; j < size; j++)
5. To find the subsequent same element use the comparison:
if (arr[i] == arr[j]).
6. If any duplicate element is encountered, delete the duplicate element from an array and the size of
array is decrement by 1 such that, size = size - 1.
7. After that, print the unique elements of an array using the code
for (i = 0; i < size; i++)
{
printf ("%d \t" , arr[i]);
}
8. Stop
Sample Output
Enter the number of elements in an array: 10
Result:
Thus a program to remove duplicates from an array of numbers using C / C++ is successfully
executed.
Date :
Exercise Number :2c
Title: Print the numbers in an array after removing even numbers
Aim:
To write a program to print the numbers in an array after removing even numbers using C / C++.
Problem Description:
Implement a program which takes an array arr and its size and removes all the even elements
from a given array
Algorithm:
1. Start
2. Input the N array Elements
3. Create for loop starts from 0 to N
4. Inside the for loop, check whether the current element is an even number or not
If
it is an even number Ignore the number
Else
print the number
5. Stop
Sample Output :
Enter the number of elements in an array: 10
Result:
Thus a program to print the numbers in an array after removing even numbersusing C / C++ is
successfully executed.
Date :
Exercise Number :3a
Title: Checking for palindrome
Aim:
To write a program to check whether the given string is palindrome or not using C / C++.
Problem Description:
A palindrome is a word, number, phrase, or other sequence of characters which reads the same
backward as forward, such as madam, racecar.
Algorithm:
Sample Output:
Enter a String :malayalam
malayalam is a palindrome
Result:
Thus a program to check whether the given string is palindrome or not using C / C++ is
successfully executed.
Date :
Exercise Number :3b
Title: Count the occurrences of each character in a given word
Aim:
To write a program to count the occurrences of each character in a given word using C / C++.
Problem Description:
A string is a one-dimensional character array that is terminated by a null character. Frequency of
characters in a string is the number of times they occur in a string.
Example:
Algorithm:
1. Read the string entered by the user and store the string into the variable ‘string’ using gets(string).
2. The for loop iterates through the string with the structure for(j=0;string[i];j++) until
the last character of the string becomes null.
3. Initialize the j value to the variable n. j indicates the length of the string.
4. Count = 0.Now count the frequency of each character in the string,The outer for loop iterates
through the string with the structure for(i=0;i<n;i++),for each iteration it selects one
element string[i].
5. The inner for loop compares the string[i] with remaining elements of the string using for loop with
the struct for(j=i+1;j<n;j++). If it matches then increase the count value.
6. Print the count value which is the frequency of the selected element string[i].
Sample Output :
Enter a String : RMK_ENGINEERING_COLLEGE
Count the occurrences of each character in a given word:
'R' = 2
'M' = 1
'K' = 1
'_' = 2
'E' = 5
'N' = 3
'G' = 3
'I' = 2
'C' = 1
'O' = 1
'L' = 2
Result:
Thus a program to count the occurrences of each character in a given word using C / C++ is
successfully executed.
Date :
Exercise Number :4
Title: Generate Pay Slip
Aim:
To write a program to Generate salary slip of employees using structures and pointers using C /
C++.
Create a structure Employee with the following members:
EID, Ename, Designation, DOB, DOJ, Basic pay
Note that DOB and DOJ should be implemented using structure within structure.
Problem Description:
Structures (also called structs) are a way to group several related variables into one place. Each variable
in the structure is known as a member of the structure. Unlike an array, a structure can contain many
different data types (int, string, bool, etc.).
Create a Structure
To create a structure, use the struct keyword followed by structure Tag and declare each of its members
inside curly braces.
struct myStructure
{ // Structure declaration
int myNum; // Member (int variable)
string myString; // Member (string variable)
}ms; // Structure variable
Assign and Access Structure Members
To access members of a structure, use the dot syntax (.):
Algorithm:
1. Start
2. Declare Structure and variables
3. Read the number of employees.
4. Read basic details of the employee.
5. Calculate salary
6. Display the output of the Pay slip calculations for each employee using pointer.
7. Stop
Sample Output:
Enter Employee Id : 229
Enter Name of Employee : Vijayakumar
Enter employee Designation : AssociateProfessor
Enter employee Date of Birth : 21
Enter employee Month of Birth : 10
Enter employee Year of Birth : 1977
Enter employee Date of Joining : 06
Enter employee Month of Joining : 01
Enter employee Year of Joining : 2003
Enter Basic Pay of the employee: 60000
Deduction :13200.000000
Nett Salary :88800.000000
Result:
Thus a program to Generate salary slip of employees using structures and pointers using C / C++
is successfully executed.
Date :
Exercise Number :5
Title: Internal Mark Computation
Aim:
To write a program to compute internal marks of students for five different subjects using
structures and functions using C / C++.
Algorithm:
1. Start
2. Declare variables
3. Read the number of students.
4. Read the student mark details
5. Calculate internal mark by I = total of five test marks / 5 for each subject per student.
6. Display the output of the calculations for all the students usinf structure and functions.
7. Stop
Sample Output:
Enter information of students:
Date :
Exercise Number :6
Title: Class and Member access
Aim:
To write a program illustrating Class Declarations, Definition, and Accessing Class Members
using C++.
Problem Description:
A class in C++ is the building block that leads to Object-Oriented programming. It is a user-
defined data type, which holds its own data members and member functions, which can be
accessed and used by creating an instance of that class. A C++ class is like a blueprint for an
object.
Algorithm:
1. Create a class record.
2. Define the class Student
3. Include the data members Register Number Name and Percentage of a student
4. Include the Function members readDetails() and printDetails()
5. Define and invoke the function readDetails() and printDetails() using the object
6. Stop the program
Sample Output:
Enter Student Details
Enter Student Register Number : 1001
Enter Student Name : Vijayakumar
Enter Percentage : 100
Student Details:
Register Number : 1001
Name : Vijayakumar
Percentage : 100
Result:
Thus a program to illustrate Class Declarations, Definition, and Accessing Class Members using
C++ is successfully executed.
Date :
Exercise Number :7
Title: Constructors
Aim:
To write a program illustrating default constructor, parameterized constructor and copy
constructors using C++.
Algorithm:
Constructors in C++ can be defined as a special member method which will be called implicitly
(automatically) whenever an object of a class is created
Default Constructor
The default constructors can be defined as a constructor that does not take any arguments. It has no
parameters
Syntax:
class class_name
{
………
public:
class_name() { }; //default constructor
………
};
Parameterized Constructor
The parameterized constructor can be used to pass the arguments to the constructor.
Copy Constructor
A copy constructor can be defined as a member function that initializes an object using another object of
the same class.
Syntax:
class-name (class-name &)
{
...
}
Sample Output:
After Default Constructor
Register Number : 0
Name :
Fees Amount : Rs.0/-
Date :
Exercise Number :8
Title: Operator Overloading and Function Overloading
Aim:
To write a program to demonstrate Operator overloading and Function Overloading using C++.
Algorithm:
1. Start the program.
2. Declare the class.
3. Declare the variables and its member function.
4. Using the function getvalue() to get the two numbers.
5. Define the function operator +() to add two complex numbers.
6. Define the function operator –()to subtract two complex numbers.
7. Define the display function.
8. Declare the class objects obj1,obj2 and result.
9. Call the function getvalue using obj1 and obj2
10. Calculate the value for the object result by calling the function operator + and
operator -.
11. Call the display function using obj1 and obj2 and result.
12. Return the values.
13. Stop the program
Algorithm:
1. Start the program.
2. Create the class with variables and functions.
3. In the main(),declare the variables.
4. Use Volume() function to find volume of cylinder, cube and rectangle.
5. Define the volume() function with necessary arguments for calculation.
6. The values that are passed inside the function call will be matched with the definition
part and appropriate calculations are done.
Result:
Thus a program to demonstrate Operator overloading and Function Overloadingusing C++ is
successfully executed.
Date :
Exercise Number :9
Title: Friend Function and Friend Class
Aim:
To write a program to demonstrate Friend Function and Friend Classusing C++.
Result:
Thus a program to demonstrate Friend Function and Friend Classusing C++ is successfully
executed.
Date :
Exercise Number : 10
Title: Inline Function
Aim:
To write a program to demonstrate Inline Function using C++.
Problem Description:
If we make a function as inline, then the compiler replaces the function calling location with the
definition of the inline function at compile time.
Any changes made to an inline function will require the inline function to be recompiled again
because the compiler would need to replace all the code with a new code; otherwise, it will
execute the old functionality.
Syntax for an inline function:
inline return_type function_name(parameters)
{
// function code
}
Inline Function Illustration
Algorithm:
1. Start the program.
2. Declare the class.
3. Declare and define the inline function for multiplication and cube.
4. Declare the class object and variables.
5. Read two values.
6. Call the multiplication and cubic functions using class objects.
7. Return the values.
8. Display.
9. Stop the program
Sample Output:
Enter two values: 5 7
Multiplication value is: 35
Cube Value is :25 and 343
Result:
Thus a program to demonstrate Inline Function using C++ is successfully executed.
Date :
Exercise Number : 11
Title: Function overriding
Aim:
To write a program to demonstrate Function overriding using C++.
Problem Description:
Function overriding in C++ is a concept by which you can define a function of the same
name and the same function signature (parameters and their data types) in both the base class and
derived class with a different function definition. It redefines a function of the base class inside
the derived class, which overrides the base class function.
Syntax :
access_modifier:
// overridden function
return_type function_name(){}
};
access_modifier:
// overriding function
return_type function_name(){}
};
Overriding Illustration :
Algorithm:
1. Start the program.
2. Declare the base class with Function Member void print() { cout<< "Base Function" }.\
3. Declare another class derived that inherits from the base class that has the member
function void print() { cout << " Derived Function" }. as same as of that given in
base class.
4. Declare the class object for derived .
5. When the derived.print() is invoked, it overrides the function specified in base class.
6. Stop the program
Sample Output:
Derived Function
Result:
Thus a program to demonstrate Function overriding using C++ is successfully executed.
Date :
Exercise Number : 12 a
Title: Single Inheritance
Aim:
To write a program to demonstrate Single Inheritance using C++.
Problem Description:
Single Inheritance is defined as the inheritance in which a derived class is inherited from the
only one base class.
In the diagram under Single inheritance box labelled “Base Class” is the base class or Parent
Class, and box labelled “Derived Class” is the Child Class derived class.
It is the simplest among all the types of inheritance since it does not include any kind of
inheritance combination or different levels of inheritance. The child class can inherit all the
members of the base class according to the visibility mode (i.e., private, protected, and public)
that is specified during the inheritance.
Syntax
class BaseClass
{
// code of class BaseClass
}
class DerivedClass : AccessModifier BaseClass
{
// code of class DerivedClass
}
Algorithm:
1. Start the process.
2. Define the base class with variables and functions.
3. Define the derived class with variables and functions.
4. Get two values in main function.
5. Define the object for derived class in main function.
6. Access member of derived class and base class using the derived class object.
7. Stop the process.
Sample Output:
Enter the value of x = 10
Enter the value of y = 20
Product = 200
Result:
Thus a program to demonstrate Single Inheritance using C++ is successfully executed.
Date :
Exercise Number : 12 b
Title: Multiple Inheritance
Aim:
To write a program to demonstrate Multiple Inheritance using C++.
Problem Description:
Multiple inheritance is the process of deriving a new class that inherits the attributes from two or
more base classes.
In the above diagram, there are two-parent classes: Base Class 1 and Base Class 2,
whereas there is only one Child Class labelled “Derived Class”. The Child Class acquires all
features from both Base class 1 and Base class 2. Therefore, we termed the type of Inheritance as
Multiple Inheritance.
Syntax
class BaseClass1
{
// code of class BaseClass1
}
class BaseClass2
{
// code of class BaseClass2
}
class DerivedClass:AccessModifier BaseClass1,AccessModifier
BaseClass2
{
// code of class DerivedClass
}
Algorithm:
1. Start the program.
2. Declare the base class student.
3. Declare and define the function get() to get the student details.
4. Declare the other class sports.
5. Declare and define the function getsm() to read the sports mark.
6. Create the class statement derived from student and sports.
7. Declare and define the function display() to find out the total and average.
8. Declare the derived class object,call the functions get(),getsm() and display()
Output:
Enter the Roll no:100
Enter two marks
90
80
Enter the sport mark :90
Roll no :100
Total :260
Average :86.66
Result:
Thus a program to demonstrate Multiple Inheritance using C++ is successfully executed.
Date :
Exercise Number : 12 c
Title: Multi Level Inheritance
Aim:
To write a program to demonstrate Multi Level Inheritance using C++.
Algorithm:
1. Start the program.
2. Declare the base class Vehicle.
3. Derive the class Four-wheeler from Base class.
4. Derive the class Car from Four-wheeler.
5. Create an object of Class car.
6. Access the methods of class Four-wheeler and Vehicle.
7. Stop
Problem Description:
Multilevel inheritance is a process of deriving a class from another derived class. In C++
programming, not only we can derive a class from the base class but we can also derive a class
from the derived class. This form of inheritance is known as multilevel inheritance.
Syntax
class BaseClass1
{
// code of class BaseClass1
}
class DerivedClass1 : AccessModifier BaseClass1
{
// code of class DerivedClass1
}
class DerivedClass2 : AccessModifier DerivedClass1
{
// code of class DerivedClass2
}
Sample Output:
I am a car
I have four wheels
I am a vehicle
Result:
Thus a program to demonstrate Multi Level Inheritance using C++ is successfully executed.
Date :
Exercise Number : 12 d
Title: Hierarchical Inheritance
Aim:
To write a program to demonstrate Hierarchical Inheritance using C++.
Algorithm:
Sample Output:
Enter Length: 12.4
Enter Breadth: 23.1
Area of Rectangle : 286.44
Enter Radius: 14
Area of Circle : 615.44
Enter Side: 3
Area of Square : 9
Result:
Thus a program to demonstrate Hierarchical Inheritance using C++ is successfully executed.
Date :
Exercise Number : 13
Title: Pure Virtual Function
Aim:
To write a program to demonstrate Pure Virtual Function using C++.
Problem Description:
Pure virtual Functions are virtual functions with no definition. They start with virtual keyword
and ends with = 0. Here is the syntax for a pure virtual function,
Syntax 1
virtual void functionname() = 0;
Note: The = 0 syntax doesn't mean we are assigning 0 to the function. It's just the way we define
pure virtual functions.
Syntax 2
virtual void functionname() {}
Pure virtual functions are used
if a function doesn't have any use in the base class
but the function must be implemented by all its derived classes
Let's take an example,
Suppose, we have derived Triangle, Square and Circle classes from the Shape class, and
we want to calculate the area of all these shapes. In this case, we can create a pure virtual
function named calculateArea() in the Shape. Since it's a pure virtual function, all derived classes
Triangle, Square and Circle must include the calculateArea() function with implementation. A
pure virtual function doesn't have the function body and it must end with = 0. For example,
class Shape
{
public:
// creating a pure virtual function
virtual void calculateArea() = 0;
};
Algorithm:
Sample Output::
Result:
Thus a program to demonstrate Pure Virtual Function using C++ is successfully executed.
Date :
Exercise Number : 14
Title: File Handling
Aim:
To write a program to count the number of account holders whose balance is less than the
minimum balance using sequential access file using C++.
Problem Description:
Files store data permanently in a storage device. With file handling, the output from a program can be
stored in a file. Various operations can be performed on the data while in the file.
Achieve the File Handling
For achieving file handling we need to follow the following steps:-
Step 1 : Create a File.
Step 2 : Opening a file.
Step 3 : Writing data into the file
Step 4 : Reading data from the file
Step 5 : Closing a file.
The fstream Library
The fstream library provides C++ programmers with three classes for working with files. These classes
include:
Ofstream : This class represents an output stream. It’s used for creating files and writing information to
files.
Ifstream : This class represents an input stream. It’s used for reading information from data files.
Fstream : This class generally represents a file stream. It comes with ofstream/ifstream capabilities. This
means it’s capable of creating files, writing to files, reading from data files. The following image makes it
simple to understand:
Algorithm:
Sample Output:
The Number of Account Holder whose Balance less than the Minimum Balance is 3
Result:
Thus a program to count the number of account holders whose balance is less than the minimum
balance using sequential access file using C++ is successfully executed.
Date :
Exercise Number : 15
Title: Exception Handling
Aim:
To write a program to demonstrate the Catching of all Exceptions using C++.
Problem Description:
Exception Handling
Exception Handling in C++ is a process to handle runtime errors. We perform exception
handling so that normal flow of the application can be maintained even after runtime errors.
In C++, exception is an event or object which is thrown at runtime. All exceptions are
derived from std::exception class. It is a runtime error which can be handled. If we don't handle
the exception, it prints exception message and terminates the program.
C++ exception handling is built upon three keywords: try, catch, and throw.
Syntax
try
{
// Block of code to try
throw exception;
}
catch ()
{
// Block of code to handle errors
}
Code within a try/catch block is referred to as protected code
Algorithm:
Sample Output:
Result:
Thus a program to demonstrate the Catching of all Exceptions using C++ is successfully
executed.
Date :
Exercise Number : 16
Title: Mini Project
Learning a new programming language is both the most exciting and the most humbling
experience. And as every coder knows, the best way to learn a language is to apply it in a
project. The best way to get an idea is to find out simple problems and then trying to automate
them.
Chose an idea/project.
Try to choose a topic to work on for your project. Keep your capabilities in mind as well
as the impact it will have. Remember that it should add a value to your resume
Write your ideas into a document/report so that people will understand what you are
trying to accomplish, what features your project will have and what it will not.
Start implementation
Start coding your ideas. If you need to learn a specific library/framework in C++ for your
project, then start with it
Code your project step by step in terms of modules. If you encounter any problem use
online forums Google, stack overflow etc to clear your doubts/hurdles you encounter
during solving these problems.
Write a report
Write a report which includes your idea of this project, difficulties you faced, features
included, possible improvements, etc in the report.
Place your code online, on online repositories like github, gitlab, bitbucket etc so
that you can showcase your project
There you go, you have your project done and hosted online so that people can verify. Go
ahead and add it to your resume
In today’s world, C++ is one of the most pertinent programming languages. Nevertheless,
understanding it can be a bit challenging for beginners, hence the projects will help you master
it. Above mentioned C++ project ideas could demonstrate to be most suitable for you to learn
something new and exciting. You can select the one that thrills you more and guides you extra.
Few are easy, and few are a bit tricky, but your job is to tinker with every project to explore your
hidden skills and talents. These projects also help if you nail your dream job.
https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-projects-for-beginners