0% found this document useful (0 votes)
40 views13 pages

PLC Lab Manual

Lab manual Programming language basic

Uploaded by

tanishqontop
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)
40 views13 pages

PLC Lab Manual

Lab manual Programming language basic

Uploaded by

tanishqontop
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

ACHARYA INSTITUTE OF TECHNOLOGY

Acharya Dr. Sarvepalli Radhakrishnan Road, Bangalore-560 107


DEPARTMENT OF Computer Science & Engineering

Introduction to C++ Programming


(22PLC15D/22PLC25D)

Programming Assignments

Complied and Executed By:


Soniya R
Assistant Professor
Dept of CSE

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

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

3. Write a C++ program to swap 2 values by writing a function that uses call by reference

technique.

4. Write a C++ program to demonstrate function overloading for the following prototypes.

add(int a, int b)

add(double a, double b)

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.

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

2
I am a vehicle

Write a C++ program to demonstrate multilevel inheritance using this.

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.

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

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.

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

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

#include<iostream>
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
num[i] = num[j]; // second element assigning to first element
4
num[j] = temp; // variable temp (means first element) assigning to second element
}
}
}
cout<<"\n Numbers in Ascending Order : \n";
for (i = 0; i < n; ++i)
{
cout<<" ";
cout<<num[i];
cout<<"\n";
}
}

Output:

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

Program:

#include<iostream>
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:

6
3. Write a C++ program to swap 2 values by writing a function that uses call by reference
technique.

Program:

#include<iostream>
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:

7
4. Write a C++ program to demonstrate function overloading for the following
prototypes.
add(int a, int b)
add(double a, double b)

Program:

#include <iostream>
using namespace std;
// function with 2 integer parameters
void add(int a, int b) {
cout << "Integer number1: " << a<< endl;
cout << " and Integer number2: " << b << endl;
}
// function with 2 double parameter
void add(double p, double q) {
cout << "Double number1: " << p << endl;
cout << "Double number2: " << q << endl;
}
int main() {
int x = 5 , y=10;
double l = 5.5, m=10.5;
// call function with int parameter
add(x,y);
// call function with double parameters
add(l, m);
return 0;
}

Output:

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

Program:5
#include <iostream>
#include <iomanip>
using namespace std;

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();
};

9
void Triangle::show()
{
cout << "Triangle is a Polygon" << endl;
}

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;
}

int main( )
{

Shape s1;
Polygon p1;
Rectangle r1;
Triangle t1;
Square sq1;
s1.show();
p1.show();
r1.show();
t1.show();
sq1.show();
return 0;
}

10
Output

This is a Shape
Polygon is a Shape
Rectangle is a Polygon
Triangle is a Polygon
Square is a Rectangle

Program 6

Multilevel Inheritance

Question
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>
#include <iomanip>
using namespace std;

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, 12
class Car : public FourWheeler
{
public:

void car();
};
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

Dept of CSE, 12

You might also like