Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
16 views20 pages

C++ Lab Manual

This lab manual provides a series of C++ programming exercises covering various concepts such as sorting arrays, calculating sums, function overloading, inheritance, file handling, and exception handling. Each exercise includes sample code demonstrating the implementation of the specified functionality. The manual is intended for students in an Introduction to C++ Programming course at RYMEC, Ballari.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views20 pages

C++ Lab Manual

This lab manual provides a series of C++ programming exercises covering various concepts such as sorting arrays, calculating sums, function overloading, inheritance, file handling, and exception handling. Each exercise includes sample code demonstrating the implementation of the specified functionality. The manual is intended for students in an Introduction to C++ Programming course at RYMEC, Ballari.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Lab Manual

Course Title: Introduction to C++ Programming

Course Code: BPLCK205D

Dept of CSE, RYMEC, Ballari Page 1


1.Write a C++ program to sort the elements in ascending and
descending order.

#include<iostream.h>

#include<conio.h>

void main ()

int a [10], n, i, j, temp;

clrscr ();

cout<<"enter the size of an array";

cin>>n;

cout<<"enter the elements of an array"<<endl;

for (i=0; i<n; i++)

cin>>a[i];

for (i=0; i<n-1; i++)

for (j=n-1; j>i; j--)

if(a[j]<a[j-1])

temp=a[j];

a[j]=a[j-1];

a[j-1] = temp;

Dept of CSE, RYMEC, Ballari Page 2


}

cout<<"the elements of a array in ascending are"<<endl;

for (i=0; i<n; i++)

cout<<a[i]<<endl;

cout<<"the elements of a array in descending are"<<endl;

for (i=n-1; i>=0; i--)

cout<<a[i]<<endl;

getch();

Dept of CSE, RYMEC, Ballari Page 3


2. Write a C++ program to find the sum of all the natural numbers
from 1 to n.

#include<iostream.h>

#include<conio.h>

void main()

int n,i,sum=0;

clrscr();

cout<<"enter a positive integer"<<"\n";

cin>>n;

for(i=1;i<=n;i++)

sum=sum+i;

cout<<"sum of natural no.="<<sum<<endl;

getch();

Dept of CSE, RYMEC, Ballari Page 4


3. Write a C++ program to swap 2 values by writing a function that
uses call by reference technique.
#include<iostream.h>

#include<conio.h>

void swap( int *x, int *y);

void main( )

int a, b;

clrscr();

cout<<"enter the value of a and b"<<endl;

cin>>a>>b;

cout<<"before swapping a="<<a<<endl;

cout<<"before swapping b="<<b<<endl;

swap(&a,&b);

cout<<"after swapping"<<endl;

cout<<"value of a ="<<a<<endl;

cout<<"value of b="<<b<<endl;

getch();

void swap( int *x, int *y)

int temp;

temp=*x;

*x=*y;

*y=temp;

}
Dept of CSE, RYMEC, Ballari Page 5
4.Write a C++ program to demonstrate function overloading for the
following prototypes.
add(int a, int b)
add(double a, double b)

#include <iostream.h>

#include<conio.h>

void add(int a, int b);

void add(double a, double b);

void main()

int iNum1, iNum2;

double dVal1, dVal2;

clrscr();

cout << "Enter integer values for i1 and i2 : " ;

cin >> iNum1 >> iNum2;

cout << "Enter double values for d1 and d2 : " ;

cin >> dVal1 >> dVal2;

add(iNum1, iNum2);

add(dVal1, dVal2);

getch();

void add(int a, int b)

cout << "Performing Integer Addition" << endl;

cout << "Sum of " << a << " and " << b << " is " << a+b << endl;

Dept of CSE, RYMEC, Ballari Page 6


}

void add(double a, double b)

cout << "Performing Double Addition" << endl;

cout << "Sum of " << a << " and " << b<< " is " << a+b << endl;

Dept of CSE, RYMEC, Ballari Page 7


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 an-other 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.h>
#include<conio.h>
class Shape
{
public:
void show();
};
void Shape::show()
{
cout<<"This is a Shape"<<endl;
}
class Polygon : public Shape
{
public:
void show();
};
void Polygon::show()
{
cout <<"Polygon is a Shape" << endl;
}
class Triangle : public Polygon
{
public:
void show();
};
void Triangle::show()
{
cout <<"Triangle is a Polygon" << endl;

Dept of CSE, RYMEC, Ballari Page 8


}
class Rectangle : public Polygon
{
public:
void show();
};
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;
}
void main()
{
clrscr();
Shape s1;
Polygon p1;
Rectangle r1;
Triangle t1;
Square sq1;
s1.show();
p1.show();
r1.show();
t1.show();
sq1.show();
getch();
}

Dept of CSE, RYMEC, Ballari Page 9


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(), four-Wheeler(), 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.h>
#include<conio.h>
class Vehicle
{
public:
void vehicle( );
};
void Vehicle::vehicle( )
{
cout << "I am a vehicle" << endl;
}
class FourWheeler : public Vehicle
{
public:
void fourWheeler( );
};
void FourWheeler::fourWheeler( )
{
cout << "I have four wheels" << endl;

Dept of CSE, RYMEC, Ballari Page 10


}
class Car : public FourWheeler
{
public:
void car( );
};
void Car::car( )
{
cout << "I am a car" << endl;
}
void main( )
{
clrscr( );
Car myCar;
myCar.car ( );
myCar.fourWheeler( );
myCar.vehicle( );
getch( );
}

Dept of CSE, RYMEC, Ballari Page 11


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.h>

#include<conio.h>

#include<string.h>

#include<fstream.h>

void main()

char flName[100];

char mesg[40], ch;

clrscr();

cout<<"Enter the file name you want to create : ";

cin>>flName;

cin.get();

ofstream fout("flname.txt");

if(fout.fail())

cout <<"\nFailed to create file."<<endl;

else

cout <<"\nFile " <<flName<<" created successfully" << endl;

cout <<"Enter a message : ";

Dept of CSE, RYMEC, Ballari Page 12


cin.getline(mesg,40);

fout<<mesg<<endl;

cout << "\nMessage written to file successfully\n" <<endl;

fout.close();

ifstream fin("flname.txt");

while(fin.get(ch)) // read character from file and

cout<<ch; // write it to screen

cout<<"\nDone reading file contents\n" <<endl;

fin.close();

getch();

Dept of CSE, RYMEC, Ballari Page 13


8. Write a C++ program to write and read time in/from binary file
using fstream

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<string.h>

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 << hh << ":";

cout << mm << ":";

cout << ss << " ";

cout << ampm << endl <<endl;

Dept of CSE, RYMEC, Ballari Page 14


}

};

void main()

timeVal writeObj, readObj;

int hh, mm, ss;

char ampm[3];

clrscr();

cout << "Enter Hours : "; cin >> hh;

cout << "Enter Minutes : "; cin >> mm;

cout << "Enter Seconds : "; cin >> ss;

cout << "Enter am or pm : "; cin >> ampm;

writeObj.setdata(hh,mm,ss,ampm);

ofstream outFile("TimeFile", ios::out | ios::binary);

if(!outFile) {

cout << "Cannot open file.\n";

outFile.write((char *) &writeObj, sizeof(timeVal));

cout << "\nWritten the time object successfully to binary file" << endl;

outFile.close();

ifstream inFile("TimeFile", ios::in | ios::binary);

if(!inFile) {

cout << "Cannot open file.\n";

Dept of CSE, RYMEC, Ballari Page 15


inFile.read((char *) &readObj, sizeof(timeVal));

cout << "\nRead the time object successfully from binary file" << endl;

readObj.showdata();

inFile.close();

getch();

Dept of CSE, RYMEC, Ballari Page 16


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>

#include <stdexcept>

using namespace std;

// defining CheckDenominator

float CheckDenominator(float den)

if (den == 0)

throw "Error";

else

return den;

} // end CheckDenominator

int main()

float numerator, denominator, result;

//numerator = 12.5;

//denominator = 7;

cout<<"Enter the value of numerator and denominator";

cin>>numerator>>denominator;

// try block

try {

// calls the CheckDenominator function

// by passing a string "Error"

Dept of CSE, RYMEC, Ballari Page 17


if (CheckDenominator(denominator))

result = (numerator / denominator);

cout << "The quotient is "

<< result << endl;

// catch block

// capable of catching any type of exception

catch (...)

// Display a that exception has occurred

cout << "Exception occurred Divide by Zero Error" << endl;

} // end main

Dept of CSE, RYMEC, Ballari Page 18


10. Write a C++ program function which handles array of bounds
exception using C++.

#include <iostream>
using namespace std;
void expab(int a)
{
int arr[10] = { 3, 1, 4, 1, 5 };
cout<<"\nthe current array is"<<"\n";
for(int i=0;i<5;i++)
cout <<arr[i]<<"\n";
arr[0] = 4; // arr is now: { 4, 1, 4, 1, 5 }
cout<<"updated array is\n";
for(int i=0;i<5;i++)
cout <<arr[i]<<"\n";
//arr[5] = 9; // Undefined behavior: index ouf of bounds
// (No bounds checking was done here)
throw arr;
}

int main()
{
try
{
expab(9); // use at() function to access elemebnt
}
catch (...)
{

Dept of CSE, RYMEC, Ballari Page 19


cout << "Out-of-bounds exception captured! cannot assign 9 at 5th
location\n";
}
return 0;
}

Dept of CSE, RYMEC, Ballari Page 20

You might also like