BPLCK105D - Introduction To C++ Programming - 240328 - 081040
BPLCK105D - Introduction To C++ Programming - 240328 - 081040
Reviewed By
Prof. MARY M DSOUZA
Assistant Professor
Approved By:
Prof. Kala Venugopal
Head of Department Information Science and Engineering
MISSION OF INSTITUTE
“Acharya Institute of Technology strives to provide excellent academic ambiance to the students
for achieving global standards of technical education, foster intellectual and personal
development, meaningful research and ethical service to sustainable societal needs.”
VISION OF THE DEPARTMENT
“To be center of Academic and Research excellence in the field of Information Technology
inculcating value based education for the development of quality Human Resource”
“Equip students with fundamental concepts, practical knowledge and professional ethics through
dedicated faculty for higher studies and professional career in various Scientific, Engineering and
Technological streams leading to proficiency in the field of Information Technology”
PROGRAM SPECIFIC OUTCOMES (PSOs)
PSO1: Able to apply knowledge of information management and communication systems to provide secured
solutions for real time engineering applications.
PSO2: Apply best software engineering practices, modern tools and technologies to deliver quality products.
COURSE OUTCOMES
CO6: Demonstrate the OOP concepts, File I/O functions and Exception handling in C++.
The internals marks of lab for 2022 scheme (Odd) is 15 for Continuous Evaluation and 10 for Lab Internals
Continuous Evaluation for 2022 scheme:
Sl No Parameters Mark 5 4 3 0
1. Writing 5 The student is The student is The student has The student is
Program/Logic able to write able to write the written not attempted
(present the program program with incomplete to write
week’s/previous without any minor logical program with program.
week’s) logical and error major logical
syntactical and syntactical
error and error
proper
indentation is
followed.
Parameters 3 3 3 2 1
3 Write a C++ program to swap 2 values by writing a function that uses call by 12
reference technique
Write a C++ program to demonstrate function overloading for the following
prototypes
4 13
add(int a, int b)
add(double a, double b)
Create a class named Shape with a function that prints "This is a shape". Create
another class named Polygon inheriting the Shape class with the same function
that prints "Polygon is a shape". Create two other classes named Rectangle and
Triangle having the same function which prints "Rectangle is a polygon" and
5 14
"Triangle is a polygon" respectively. Again, make another class named Square
having the same function which prints "Square is a rectangle”. Now, try calling the
function by the object of each of these classes.
Suppose we have three classes Vehicle, FourWheeler, and Car. The class
Vehicle is the base class, the class FourWheeler is derived from it and the
class Car is derived from the class FourWheeler. Class Vehicle has a method
'vehicle' that prints 'I am a vehicle', class FourWheeler has a method
'fourWheeler' that prints 'I have four wheels', and class Car has a method
'car' that prints 'I am a car'. So, as this is a multi-level inheritance; we can
6 16
have access to all the other classes’ methods from the object of the class Car.
We invoke all the methods from a Car object and print the corresponding
outputs of the methods. So, if we invoke the methods in this order, car (),
fourWheeler (), and vehicle (), then the output will be I am a car I have four
wheels I am a vehicle Write a C++ program to demonstrate multilevel
inheritance using this.
Write a C++ program to create a text file, check file created or not, if created
7 18
it will write some text into the file and then read the text from the file.
Write a C++ program to write and read time in/from binary file using
8 20
fstream
Write a function which throws a division by zero exception and catch it in
9 catch block. Write a C++ program to demonstrate usage of try, catch and 22
throw to handle exception.
Write a C++ program function which handles array of bounds exception
10 using C++. 23
Introduction to C++ Programming (BPLCK105D)
2023-24
` LABORATORY PROGRAMS
1. Write a C++ program to sort the elements in ascending and descending order.
#include<iostream.h>
//using namespace std;
int main ()
{
int num[20],n;
int i, j, temp;
cout<<"\n Enter the size of Array: \n";
cin>>n;
cout<<"Enter the Array value";
for (i = 0; i < n; ++i)
cin>>num[i];
for (i = 0; i < n; ++i) // 'for' loop is used for sorting the numbers in descending order
{
for (j = i + 1; j < n; ++j)
{
if (num[i] < num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
cout<<"\n Numbers in Descending Order : \n";
for (i = 0; i < n; ++i)
{
cout<<" "; cout<<num[i]; cout<<"\n";
}
for(i=0;i<n;i++) //outer-loop for sorting the numbers in ascending order
{
for(int j=0;j<n;j++) //inner-loop
{
if(num[i]<num[j]) // represent second element in the array list
{
temp = num[i]; // first array element assign to variable temp
}
}
}
cout<<"\n Numbers in Ascending Order : \n";
for (i = 0; i<n; ++i)
{
cout<<" ";
cout<<num[i];
cout<<"\n";
}
}
Output:
Enter the size of Array:
4
Enter the Array value
6
3
7
1
2. Write a C++ program to find the sum of all the natural numbers from 1 to n.
#include<iostream.h>
//using namespace std;
int main()
{
int n;
cout << "Enter a number : ";
cin >> n;
int sum=0;
for(int i=1;i<=n;i++) sum+=i;
cout <<"Sum of " <<n <<" natural numbers is:\n"<< sum;
return 0;
}
Output:
Enter a number: 5
Sum of 5 natural numbers is:
15
3. Write a C++ program to swap 2 values by writing a function that uses call by
reference technique.
#include<iostream.h>
//using namespace std;
void swap(int &,int &);
int main()
{
int x,y;
cout<<"\nEnter 1st number:: ";
cin>>x;
cout<<"\nEnter 2nd number:: ";
cin>>y;
cout<<"\n Before swapping the numbers are:"<<"\n\t x = "<<x<<"\n\t y = "<<y<<endl;
swap(x,y);
cout<<"\n After swapping the numbers are:"<<"\n\t x = "<<x<<"\n\t y = "<<y<<endl;
return 0;
}
void swap (int &num1, int &num2) //&num1 and &bnum2 are Reference variables
{
int temp;
temp=num1;
num1=num2;
num2=temp;
}
Output:
#include <iostream>
using namespace std;
// function with 2 integer parameters
void add(int a, int b)
{
int c;
cout<< "Integer number1: " << a<< endl;
cout<< " and Integer number2: " << b << endl;
c=a+b;
cout<<”Add of 2 numbers:”<<c;
}
}
int main()
{
int x = 5 , y=10;
double l = 5.5, m=10.5;
Output:
Integer number 1: 5
and Integer number 2: 10
Double number1: 5.5
Double number2: 10.5
5. Create a class named Shape with a function that prints "This is a shape". Create another
class named Polygon inheriting the Shape class with the same function that prints
"Polygon is a shape". Create two other classes named Rectangle and Triangle having the
same function which prints "Rectangle is a polygon" and "Triangle is a polygon"
respectively. Again, make another class named Square having the same function which
prints "Square is a rectangle”. Now, try calling the function by the object of each of these
classes.
#include <iostream>
#include <iomanip>
using namespace std;
class Shape
{
public:
void show();
};
void Shape::show()
{
cout << "This is a Shape" << endl;
}
};
void Rectangle::show()
{
cout << "Rectangle is a Polygon" << endl;
}
class Square : public Rectangle
{
public:
void show();
};
void Square::show()
{
cout << "Square is a Rectangle" << endl;
}
int main()
{
Shape s1;
Polygon p1;
Rectangle r1;
Triangle t1;
Square sq1;
s1.show();
p1.show();
r1.show();
t1.show();
sq1.show();
return 0;
}
Output:
This is a Shape
Polygon is a Shape
Rectangle is a Polygon
Triangle is a Polygon
Square is a Rectangle
6. Suppose we have three classes Vehicle, FourWheeler, and Car. The class Vehicle is the
base class, the class FourWheeler is derived from it and the class Car is derived from the
class FourWheeler. Class Vehicle has a method 'vehicle' that prints 'I am a vehicle', class
FourWheeler has a method 'fourWheeler' that prints 'I have four wheels', and class Car
has a method 'car' that prints 'I am a car'. So, as this is a multi-level inheritance; we can
have access to all the other classes methods from the object of the class Car. We invoke all
the methods from a Car object and print the corresponding outputs of the methods.
So, if we invoke the methods in this order, car(), fourWheeler(), and vehicle(), then the
output will be
I am a car
I have four wheels I am a vehicle
Write a C++ program to demonstrate multilevel inheritance using this.
#include<iostream>
#include<iomanip> using
namespace std;
class Vehicle
{
public:
void vehicle();
};
void Vehicle::vehicle()
{
cout << "I am a vehicle" << endl;
};
void FourWheeler::fourWheeler(){
cout << "I have four wheels" << endl;
};
void Car::car(){
cout << "I am a car" << endl;
}
int main()
Car myCar;
myCar.car();
myCar.fourWheeler();
myCar.vehicle();
return 0;
}
Output:
I am a car
I have four wheels
I am a vehicle
7. Write a C++ program to create a text file, check file created or not, if created it will write
some text into the file and then read the text from the file
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string flName;
char mesg[40], ch;
cout << "Enter the file name you want to create : ";
cin >> flName;
cin.get(); //read the trailing enter character
ofstream fout(flName.c_str());
// fout.close();
if(fout.fail())
{
cout << "\nFailed to create file." << endl;
}
else
{
cout << "\nFile " << flName <<" created successfully" << endl;
ifstream fin(flName.c_str());
cout << "Here are the contents of " << flName << ":\n";
while (fin.get(ch))
{
cout << ch; // write it to screen
cout << "\nDone reading file contents\n" << endl;
}
fin.close();
return 0;
Output:
Enter the file name you want to create : hello.txt
8. Write a C++ program to write and read time in/from binary file using fstream
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;
class timeVal
{
int hh, mm, ss; char ampm[3];
public:
void setdata(int h, int m, int s, const char* half)
{
hh = h; mm = m; ss = s;
strcpy(ampm, half);
}
void showdata()
{
cout << "\nThe Time is : ";
cout << setfill('0') << setw(2) << hh << ":";
cout << setfill('0') << setw(2) << mm << ":";
cout << setfill('0') << setw(2) << ss << " ";
cout << ampm << endl << endl;
}
};
int main()
{
outFile.write((char *) &writeObj,sizeof(timeVal));
cout << "\nWritten the time object successfully to binary file" << endl;
outFile.close();
Output:
Enter Hours : 3
Enter Minutes : 4
Enter Seconds : 5
Enter am or pm : am
9. Write a function which throws a division by zero exception and catch it in catch block.
Write a C++ program to demonstrate usage of try, catch and throw to handle exception.
#include <iostream>
using namespace std;
int fdivide(int,int);
int main()
{
int numer,denom,res;
cout<<"Enter the value of numerator and denominator\n";
cin>>numer>>denom;
res=fdivide(numer,denom);
return res;
}
{
try
{
int a,b,c;
a=n1;
b=n2;
c=a/b;
if (b==0)
{
throw b;
}
cout<<"Result="<<c;
}
catch (int b)
{
cout<<"Cannot divide by zero"<<b;
}
}
Output:
10. Write a C++ program function which handles array of bounds exception using C++.
#include <iostream>
using namespace std;
int read_array(int[],int );
int disp_array(int[],int);
int main()
{
int arr[6],n,i;
cout<<"Enter the number of elements to consider\n";
cin>>n;
try
{
if(n>6)
{
throw "Array out of Bound Exception error\n";
}
read_array(arr,n);
disp_array(arr,n);
}
catch(const char *msg)
{
cout<<msg<<" Pls Enter the number of elements less than or equal to "<<6<<"\n";
}
return 0;
}
return 0;
}
Output 2:
Enter the number of elements to consider
7
Array out of Bound Exception error