0% found this document useful (0 votes)
47 views

C++ Lab Manual

The document contains examples of C++ programs demonstrating different concepts like sorting arrays, finding sum of natural numbers, function overloading, inheritance, reading and writing to binary files. The programs show how to implement these concepts through functions, classes and use of streams.

Uploaded by

s0821verma
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)
47 views

C++ Lab Manual

The document contains examples of C++ programs demonstrating different concepts like sorting arrays, finding sum of natural numbers, function overloading, inheritance, reading and writing to binary files. The programs show how to implement these concepts through functions, classes and use of streams.

Uploaded by

s0821verma
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/ 13

C++

1.Write a C++ program to sort the element in ascending and


descending order.

#include<bits/stdio.h>
using namespace std;
void asc Dec Func (int a[], int n)
{
int temp;
for(int i=0;i < n-1;i++)
{
for(int j = 0;j < n/2; j++) { if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}

for(int j = n/2;j < n-1; j++)


{
if(a[j] < a[j+1])
{
temp=a[j];
C++
a[j]=a[j+1];
a[j+1]=temp;
}
}

for(int j = n/2;j < n-1; j++)


{
if(a[j] < a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

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


cout<<a[i]<<" ";
}

int main()
{
C++
int arr[] = {3, 2, 4, 1, 10, 30, 40, 20};
int len = sizeof(arr) / sizeof(arr[0]);
ascDecFunc(arr, len);

return 0;
}
2.Write a C++ program to find the sum of all the natural number from
1 to n
#include <iostream>
using namespace std;

int main()
{
int n, sum = 0;
cout << "How many numbers do you want to add? ";
cin >> n;
int arr[n];
cout << "\nEnter numbers: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
for (int i = 0; i < n; i++)
sum += arr[i];
cout << "\nSum is: " << sum << endl;
C++
return 0;
}
3.Write a C++ program to swap 2 values by writing a function that
uses call by references technique.
#include<iostream>
using namespace std;
void swap (int &num1, int &num2) //&num1 and &bnum2 are
Reference variables
{
int temp;
temp=num1;
num1=num2;
num2=temp;
}
int main()
{
int a=30,b=50;
cout<<"\n Before swapping"<<"\n A = "<<a<<"\n B =
"<<b<<endl;
swap(a,b);
cout<<"\n After swapping"<<"\n A = "<<a<<"\n B =
"<<b<<endl;
return 0;
}
C++
4.Write a C++ program to demonstate function overloading for the
following prototype
add(int a, int b)
add(double a, double b)
#include <iostream>
using namespace std;

void add(int a, int b)


{
cout << "sum = " << (a + b);
}

void add(double a, double b)


{
cout << endl << "sum = " << (a + b);
}

// Driver code
int main()
{
add(10, 2);
add(5.3, 6.2);
C++

return 0;
}
5. Create a class named shaped with a function that prints “The 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
function which prints “square is a rectangle”. Now ,try calling the
function by the object of each of these classes.

#include <iostream>
using namespace std;
class Shape{
public:
Shape(){}
virtual void print(){
cout<<"\nThis is a shape.";
}
};
class Polygon: public Shape{
public:
Polygon(){}
void print(){
cout<<"\nPolygon is a shape.";
}
};
C++
class Rectangle: public Polygon{
public:
Rectangle(){}
void print(){
cout<<"\nRectangle is a Polygon.";
}
};
class Triangle: public Polygon{
public:
Triangle(){}
void print(){
cout<<"\nTriangle is a Polygon.";
}
};
class Square: public Rectangle{
public:
Square(){}
void print(){
cout<<"\nSquare is a Rectangle.";
}
};
int main(){
Shape S;
Polygon P;
Rectangle R;
Triangle T;
Square Sq;
S.print();
C++
P.print();
R.print();
T.print();
Sq.print();
return 0;
}

6.Suppose we have three classes Vehicle, Four Wheeler ,and Car. The
class Vehicle is the base class, the class Four Wheeler is derived from
it and the class Four Wheeler. Class Vehicle is has a method ‘vehicle’
that prints ‘I am a vehicle’, class Four Wheeler has a method ‘four
wheeler’ that prints ‘I have Four Wheeler’, 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 a Four Wheeler
I am a Vehicle
Write a c++ program to demonstrate the multilevel inheritance
using this.

#include <iostream>
using namespace std;

// first base class


C++
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};

// second base class


class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle" << endl;
}
};
// sub class derived from two base classes
class Car: public Vehicle, public FourWheeler {
};

// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}

Method 2:

#include <iostream>
using namespace std;
// base class
class Vehicle
C++
{
public:
Vehicle()
{
cout << "Iam a Vehicle" << endl;
}
};
// first sub_class derived from class vehicle
class fourWheeler: public Vehicle
{ public:
fourWheeler()
{
cout<<"I have four wheels"<<endl;
}
};
// sub class derived from the derived base class fourWheeler
class Car: public fourWheeler{
public:
Car()
{
cout<<"Iam a Car"<<endl;
}
};
// main function
int main()
C++
{
//creating object of sub class will
//invoke the constructor of base classes
Car obj;
return 0;
}

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

#include <iostream>
#include <fstream>
#include <iomanip> //for setfill() and setw()

using namespace std;

#define FILE_NAME "time.dat"

//function to write time into the file


void writeTime(int h, int m, int s){

char str[10];

fstream file;
file.open(FILE_NAME, ios::out|ios::binary);

if(!file){
cout<<"Error in creating file!!!"<<endl;
return;
}

//make string to write


sprintf(str,"%02d:%02d:%02d",h,m,s);

//write into file


C++
file.write(str,sizeof(str));
cout<<"Time "<<str<<" has been written into file."<<endl;

//close the file


file.close();

//function to read time from the file


void readTime(int *h,int *m, int *s){

char str[10];
int inH,inM,inS;

fstream finC;
finC.open(FILE_NAME,ios::in|ios::binary);
if(!finC){
cout<<"Error in file opening..."<<endl;
return;
}
if(finC.read((char*)str,sizeof(str))){
//extract time values from the file
sscanf(str,"%02d:%02d:%02d",&inH,&inM,&inS);
//assign time into variables, which are passing in
function
*h=inH;
*m=inM;
*s=inS;
}
finC.close();
}

int main(){
int m,h,s;

cout<<"Enter time:\n";
cout<<"Enter hour: "; cin>>h;
C++
cout<<"Enter minute: "; cin>>m;
cout<<"Enter second: "; cin>>s;

//write time into file


writeTime(h,m,s);

//now, reset the variables


h=m=s=0;

//read time from the file


readTime(&h,&m,&s);

//print the time


cout<<"The time is
"<<setw(2)<<setfill('0')<<h<<":"<<setw(2)<<setfill('0')<<m<<":"<<s
etw(2)<<setfill('0')<<s<<endl;

return 0;
}

You might also like