KEMBAR78
C++ Lab Programs HKBK | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
25 views14 pages

C++ Lab Programs HKBK

The document contains multiple C++ programs demonstrating various concepts such as sorting arrays, calculating the sum of natural numbers, swapping values using call by reference, function overloading, class inheritance, exception handling, file operations, and binary file reading/writing. Each program is presented with its respective code and comments explaining its functionality. The document serves as a comprehensive guide for learning and practicing fundamental C++ programming techniques.

Uploaded by

sameer974021
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)
25 views14 pages

C++ Lab Programs HKBK

The document contains multiple C++ programs demonstrating various concepts such as sorting arrays, calculating the sum of natural numbers, swapping values using call by reference, function overloading, class inheritance, exception handling, file operations, and binary file reading/writing. Each program is presented with its respective code and comments explaining its functionality. The document serves as a comprehensive guide for learning and practicing fundamental C++ programming techniques.

Uploaded by

sameer974021
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/ 14

Write a C++ program to sort the elements in ascending and

descending order.

#include <iostream.h>
#include<conio.h>

void main() {
int a[50], n, i, j, t;

cout << "Enter total number of elements:";


cin >> n;

cout << "Enter the elements:\n";


for (i = 0; i < n; i++) {
cin >> a[i];
}

// Sorting in Ascending Order


for (i = 0; i < n - 1; i++)
for (j = i + 1; j < n; j++)
if (a[i] > a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
cout << "Elements in Ascending Order:\n";
for (i = 0; i < n; i++) {
cout << a[i] << endl;
getch();
}
Desending Order
#include <iostream.h>
#include<conio.h>
void main() {
int a[50], n, i, j, t;

cout << "Enter total number of elements:";


cin >> n;

cout << "Enter the elements:\n";


for (i = 0; i < n; i++) {
cin >> a[i];
}

// Sorting in desending Order


for (i = 0; i < n - 1; i++)
for (j = i + 1; j < n; j++)
if (a[i] < a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
cout << "Elements in Ascending Order:\n";
for (i = 0; i < n; i++) {
cout << a[i] << endl;
getch();
}
Write a C++ program to find the sum of all the natural
numbers from 1 to n.

METHOD 1:
#include <iostream.h>
#include<conio.h>
void main()
{
int n, sum;
cout << "Enter the value of n: ";
cin >> n;
sum = n * (n + 1) / 2;
cout << "Sum of natural numbers is: " << sum << endl;
}

METHOD 2:
#include <iostream.h>
#include<conio.h>
void main()
{
int n, sum = 0;
cout << "Enter the value of n: ";
cin >> n;
for (int i = 1; i <= n; i++)
{
sum = sum+i;
}

cout << "Sum of natural numbers is: " << sum << endl;
getch();
}
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); // Call by reference

void main()
{
int x, y;

cout << "Enter 2 values:\n";


cin >> x >> y;

cout << "Before Swap: x = " << x << ", y = " << y << endl;

Swap(x, y); // Call the function

cout << "After Swap: x = " << x << ", y = " << y << endl;

getch();
}

// Function to swap values using reference


void Swap(int &x, int &y)
{
int t = x;
x = y;
y = t;
}
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 a, b;
double x, y;

cout << "Enter 2 integers:\n";


cin >> a >> b;
add(a, b); // Calls the int version

cout << "Enter 2 float point numbers:\n";


cin >> x >> y;
add(x, y); // Calls the double version
getch();
}
void add(int a, int b)
{
cout << a << " + " << b << " = " << a + b << endl;
}
void add(double a, double b)
{
cout << a << " + " << b << " = " << a + b << endl;
}
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.h>
#include<conio.h>
class Shape {
public:
void Show() {
cout << "This is a shape" << endl;
}
};
class Polygon : public Shape {
public:
void Show() {
cout << "Polygon is a shape" << endl;
}
};
class Rectangle : public Polygon {
public:
void Show() {
cout << "Rectangle is a polygon" << endl;
}
};
class Triangle : public Polygon {
public:
void Show() {
cout << "Triangle is a polygon" << endl;
}
};
class Square : public Rectangle {
public:
void Show() {
cout << "Square is a rectangle" << endl;
}
};
void main()
{
Shape S1;
Polygon P1;
Rectangle R1;
Triangle T1;
Square S2;

S1.Show(); // This is a shape


P1.Show(); // Polygon is a shape
R1.Show(); // Rectangle is a polygon
T1.Show(); // Triangle is a polygon
S2.Show(); // Square is a rectangle

getch();
}
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.h>
#include <conio.h>

class Vehicle {
public:
void vehicle() {
cout << "I am a vehicle" <<
endl;
}
};

class FourWheeler : public


Vehicle {
public:
void fourWheeler() {
cout << "I have four wheels" <<
endl;
}
};
class Car : public FourWheeler {
public:
void car() {
cout << "I am a car" << endl;
}
};

void main()
{
Car c1;
c1.car();
c1.fourWheeler();
c1.vehicle();

getch();
}
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.h>
#include <conio.h>
void div()
{
int a, b, c;
cout << "Enter 2 values :" << endl;
cin >> a >> b;
if (b == 0)
{
throw 0;
}
else
{
c = a / b;
cout << a << "/" << b << "=" << c << endl;
}
}
void main()
{
try
{
div();
} catch (int x)
{
cout << "Divide by zero error \n";
}
cout << "After Exception \n"; }
getch();
}
Write a C++ program function which handles array of
bounds exception using C++.

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
vector <int> V = {1, 2, 3};
try
{
V.at(10);
}
catch (out_of_range e)
{
cout << "caught : " << e.what();
}
return 0;
}
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>
#include <string>
using namespace std;
int main()
{
char ch;
ofstream fout("Sample");
if(fout.fail())
{
cout<<”Cannot open the file\n”;
exit(0);
}
while(1)
{
cin.get(ch);
if(ch==’$’)
break;
fout<<ch<<endl:
}
fout.close();
ifstream fin("Sample");
while(!fin.efo())
{
fin.unsetf(is::skipws);
fin>>ch;
cout<<ch;
}
fin.close();
return 0;
}
Write aC++ program to write and read time
in/from binary file using fstream

#include <iostream>
#include<fstream>
class Time
{
public:
int h,m,s;
void readtime(int hr,int min, int sec)
{
h=hr;
m=min;
s=sec;
}
void writetime()
{
cout<<"time is:\n";
cout<<h<<":"<<m<<":"<<s<<endl;
}
};
int main()
Time t1;
ofstream fout;
fout.open("sample",ios::binary|ios::out);
if (fout.fail())
{
cout<<"File can not be created\n";
exit(0);
}
char ch='y';
int h,m,s;
while(ch!='n')
{
cout<<"Enter the time:\n";
cout<<"enter hour\n";
cin>>h;
cout<<"Enter the minute\n";
cin>>m;
cout<<"enter the seconds:\n";
cin>>s;
t1.readtime(h,m,s);
fout.write((char*)&t1,size of (t1));
cout<<"Do you want to continue?(y/n\n";
cin>>ch;
}
fout.close();
ifstream fin;
fin.open("sample",ios::binary|ios::in);
if(!fin.is_open())
{
cout<<"Can not open file for reading\n";
exit(0);
}
while(fin)
{
fin.read((char*)&t1,size of(t1));
t1.writetime();
}
fin.close();
return 0;
}

You might also like