C++ Lab Manual
C++ Lab Manual
#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;
}
}
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;
// 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;
// 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()
char str[10];
fstream file;
file.open(FILE_NAME, ios::out|ios::binary);
if(!file){
cout<<"Error in creating file!!!"<<endl;
return;
}
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;
return 0;
}