CP+ Lab M
CP+ Lab M
Technology
(AICTE Approved, VTU Affiliated, NAAC Accredited with 'A' Grade)
Dr. Vishnuvardhan Road, R.R Nagar Post, Channasandra, Beganluru-
ESTD: 2001
An Institute with a Difference
560098.
VISION
" Building RNSIT into a world-class institution "
MISSION
" To impart high quality education in Engineering and Technology and Management with
a difference, enabling students to excel in their career "
Department
VISION
“Synergizing Computer Applications for real world”
MISSION
Produce technologists of highest caliber to engage in design research and development, so
as to enable the nation to be self-reliant
Give conceptual orientation in basic computer applications and mathematics, motivate the
students for lifelong learning
Integrate project environment experiences at every level of the post graduate curriculum to
give a firm practical foundation.
Prepared by
Rajatha S Asst. Prof ,
Department of MCA, RNSIT
© MCA- 2019-20
SEMESTER – I
CREDITS –02
Subject Code: 18MCA16 CIE
Marks: 40
SEE Hours: 3 SEE
Marks: 60
Course Outcomes: At the end of the course, students will be able to:
CO1: Differentiate the effect of different types of calling a function with C++
programs.
CO2: Design the function template and implement the inline function.
CO3: Apply the static polymorphism concept using friend class, friend
function and
operator overloading
CO4: Write programs to demonstrate the run-time polymorphism with
constructor,
destructor and virtual functions
CO5: Design an application to handle errors with exceptions and store data
in the files.
PART -A
=================================================================
1. Write a C++ program to find the sum for the given variables using function with default
arguments.
#include <iostream>
using namespace std;
void sum (int a =2, int b=3, int c=4, int d=5)
{
int res;
res = a + b + c + d;
cout << "Sum =" << res;
}
int main ( )
{
int a, b, c, d;
cout << "\n Enter 4 Nos : ";
cin >> a >>b >>c >>d;
return 0;
}
2. Write a C++ program to swap the values of two variables and demonstrates a function
using call by value.
//Program: Call-by-Value
#include <iostream>
using namespace std;
int main ()
{
int a, b;
cout<<"******** SWAPPING USING CALL BY VALUE ******\n"<<endl;
3. Write a C++ program the swap the values of two variables and demonstrates a
function using Call by reference using reference variable (&).
#include <iostream>
using namespace std;
void swap (int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
}
int main ()
{
cout<<"Swapping Using Call By Reference Using(&)\n"<<endl;
int a = 100;
int b = 200;
4. Write a C++ program the swap the values of two variables and demonstrates a
function using Call by reference using pointer.
int main ()
{
cout<<"Swapping Using Call By ReferenceUsingPointer(*)\n"<<endl;
int a = 100;
int b = 200;
5. Write a C++ program to swap the values of two dynamically allocated variables and
release the memory after swapping. (use new & delete operators)
#include <iostream>
using namespace std;
int main()
{
cout<<" ******** Demonstrate INLINE Function *******";
int a, b, c, large, small, secLargest;
7.Write a program to calculate the volume of different geometric shapes like cube,
cylinder and sphere and hence implement the concept of Function Overloading.
#include<iostream>
using namespace std;
float volume ( float, int);
float volume (float);
int volume (int);
int main()
{
float cRadius, sRadius, height;
int side;
cout <<"Enter cylinder Details:" << endl ;
cout <<"Cylinder Radius and height= ";
cin >> cRadius >>height;
cout<<endl<< "Enter Cube Details:";
cout <<"Cube Side = " ;
cin>>side;
cout<<endl<< "Enter Sphere Details:";
cout <<"Sphere Radius = " ;
cin>>sRadius;
8. Write a C++ program to create a template function for Bubble Sort and demonstrate
sorting of integers and doubles.
#include<iostream>
using namespace std;
template <class T > void bubble(T a[], int n)
{
int i,j;
T temp;
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>=a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
int main()
{
int intarr[10],i,n,m;
double dbarr[10];
bubble(intarr,n);
bubble(dbarr,m);
}
PART B
--------------------------------------------------------------
1. Define a STUDENT class with USN, Name, and Marks in 3 tests of a subject. Declare
an array of 10 STUDENT objects. Using appropriate functions, find the average of
the two better marks for each student. Print the USN, Name and the average marks of
all the students.
#include<iostream>
using namespace std;
class Student
{
char USN[11], name[15];
float m1, m2, m3, avg;
public:
void readStudent();
void computeAvg();
void showStudent();
};
void Student::computeAvg()
{
float small;
small =((m1<=m2)?((m1<=m3)?m1:m3):((m2<=m3)?m2:m3));
avg=(m1+m2+m3-small)/2;
}
int main()
{
Student st [10];
int n;
cout<<"**********STUDENT INFORMATION************"<< endl;
cout<<"Enter the number of students:";
cin>>n;
for(int i = 0; i < n; i++)
{
cout<<endl<<"_____________________________ " <<endl;
cout<<"Enter Details of student:"<< i + 1<<endl<<endl;
st[i].readStudent();
}
2. Write a C++ program to create a class called COMPLEX and implement the
following overloading functions ADD that return a complex number:
a. ADD (a, s2) – where “a‟ is an integer (real part) and s2 is a complex number
b. ADD (s1, s2) – where s1 and s2 are complex numbers
#include<iostream>
using namespace std;
class COMPLEX
{
private:
int real, img;
public:
COMPLEX Add (int, COMPLEX);
COMPLEX Add (COMPLEX, COMPLEX);
void read();
void show();
};
int main( )
{
COMPLEX s1, s2, s3;
int a;
Output:
Enter Complex Number and store in s2:
Real part = 6
Imaginary part = 5
Read Integer Number To Add To s2 = 2
______________________________________
Perform s1 = a + s2 using ADD function
______________________________________
Complex Num s2 = 6 + 5i
Number to add = 2
-----------------------------------
Complex Res s1 = 8 + 5i
______________________________________
Perform s3 = s1 + s2 using ADD function
______________________________________
s1 = 8 + 5i
s2 = 6 + 5i
-----------------------------------
s3 = 14 + 10i
class WIFE
{
private:
char name[10];
int age;
float salary;
public :
void getdata()
{ cout << "Enter Name : " ;
cin >> name;
cout << "Enter Age : ";
cin >> age;
cout << "Enter Salary : ";
cin >> salary;
}
void display()
{
cout << "Name : " << name << endl;
cout << "Age : " << age << endl;
cout << "Salary : " << salary << endl;
}
friend void totSal (HUSBAND, WIFE);
};
int main()
{
cout<<"Program To Find The Total Salary of the Family"<<endl;
HUSBAND Hus;
WIFE Wf;
cout << endl << "Enter Husband Details : " << endl;
Hus.getdata();
cout << endl << "Enter Wife Details : " << endl;
Wf.getdata();
include<iostream>
using namespace std;
class Student
{
private:
char name[15];
float m1,m2,m3,avg;
public:
void getData();
friend class AVGMARKS;
void showData();
};
class AVGMARKS
{
public:
int getAvg(Student t)
{
t.avg = (t.m1 + t.m2 + t.m3) / 3.0;
return t.avg;
}
};
void Student:: getData()
{
cout << "Enter Student name : " ;
cin >> name;
cout << "Enter test marks of 3 Subjects :" << endl;
cout << "Test1 = " ;
cin >> m1;
cout << "Test2 = " ;
cin >> m2;
cout << "Test3 = " ;
cin >> m3;
}
void Student:: showData()
{
cout<<"--------------------------------------"<<endl;
cout<<name<<" Details are:"<<endl;
cout<<"--------------------------------------"<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Test1:"<<m1<<endl;
cout<<"Test2:"<<m2<<endl;
cout<<"Test3:"<<m3<<endl;
cout<<"---------------------------------------\n";
}
int main()
{
Student s1;
AVGMARKS ob;
s1.getData();
s1.showData();
4.Create a class called MATRIX using two-dimensional array of integers. Implement the
following operations by overloading the operator == which checks the compatibility of two
matrices to be added and subtracted. Perform the addition and subtraction by overloading the
+ and – operators respectively. Display the results by overloading the operator <<. If (m1==
m2) then m3 = m1 + m2 and m4 = m1 - m2 else display error.
#include <iostream>
using namespace std;
class MATRIX
{
private:
int r, c, i, j;
int mat[10][10];
public:
void read_order();
void read_matrix();
temp.r=r;
temp.c=c;
return temp;
}
temp.r=r;
temp.c=c;
return temp;
}
int main()
{
MATRIX m1,m2,m3,m4;
cout<<"first matrix";
m1.read_order();
cout<<"second matrix";
m2.read_order();
if(m1==m2)
{
m1.read_matrix();
m2.read_matrix();
5.Write a program to create an HUMAN class with features Head, Legs, Hands.
(NOTE:Head, Legs and Hands are of integer/float types)
a) Create an object HUMAN1 using default constructor. (Default features to have 1
Head, 2 Legs and 2 Hands)
b) Create an object HUMAN2 with customized inputs using Parameterized
Constructor
c) Create an object HUMAN3 using existing object HUMAN1 (Copy Constructor).
d) All Humans die after their lifetime. (Destructor)
#include<iostream>
#include<string.h>
using namespace std;
class HUMAN
{
private:
char name[30];
int head;
int legs;
int hands;
public:
HUMAN()
{
strcpy(name , "\n--man1--\n");
head = 1;
legs = 2;
hands = 2;
}
HUMAN (int head, int legs, int hands)
{
strcpy(name , "\n--man2--\n");
this->head = head;
this->legs = legs;
this->hands = hands;
}
HUMAN ( HUMAN &man)
{
strcpy(name , "\n--man3--\n");
head = man.head;
legs = man.legs;
hands = man.hands;
}
~HUMAN()
{
cout << name << " is killed "<<endl;
getch();
}
void show()
{
cout << "head=" << head << endl << "legs=" << legs <<endl;
cout << "hands=" << hands << endl;
}
};
int main()
{
HUMAN man1, man2(3,4,5), man3 = man1;
6. Demonstrate Simple Inheritance concept by creating a base class FATHER with data members
FirstName, SurName, DOB and BankBalance and creating a derived class SON, which inherits
SurName and BankBalance feature from base class but provides its own feature FirstName
and DOB. Create and initialize F1 and S1 objects with appropriate constructors and display the
Father & Son details. (Hint : While creating S1 object, call Father base class parameterized
constructor through derived class by sending values)
#include<iostream>
#include<string.h>
using namespace std;
class FATHER
{
private:
char fname[20];
char DOB [20];
protected:
float bal;
char surName[20];
public:
FATHER () { }
FATHER (char *fname, char *surName, char *fdob, float bal )
{
strcpy(this->fname, fname);
strcpy(this->surName, surName);
strcpy(DOB, fdob);
this->bal = bal;
}
void showFather()
{
cout<<endl<<"******* FATHERS DETAILS ********"<<endl;
cout<<"Father name = "<< fname <<endl;
cout<<"Surname = "<< surName <<endl;
cout<<"DOB = "<< DOB <<endl;
}
};
int main()
{
char fName[10], surName[10], fDOB[10];
float bal;
cout<<"Surname:"<<endl;
cin>>surName;
#include<iostream>
using namespace std;
class EMPLOYEE
{
protected:
char Name[50], EmpID[10];
double BasicSal, netSal;
public:
virtual void cal_sal() = 0;
};
cout<<"Manager \t"<<EmpID<<"\t"<<Name<<"\t";
cout<<BasicSal<<"\t"<<DA<< "\t"<<HRA<<"\t\t"<<netSal <<endl;
}
};
int main()
{
EMPLOYEE *emp[2];
emp[0] = new MANAGER();
emp[1] = new SALESMAN();
cout<<"\n****************************************************************"<<
endl;
emp[0]->cal_sal();
emp[1]->cal_sal();
return 0;
}
8. Write a program to implement FILE I/O operations on characters. I/O operations includes
inputting a string, Calculating length of the string, Storing the string in a file, fetching the stored
characters from it, etc
#include <fstream>
#include <iostream>
#include <iostream.h>
#include<conio.h>
class BagException
{
public:
char* what()
{
return " Bag weight is exceeding 40 kg.. Not allowed ";
}
};
class AgeException
{
public:
char* what()
{
return " Age is Less than 10 years..Not allowed to travel";
}
};
class LuggageException
{
public:
char* what()
{
return " No. of Luggage are exceeding 10..Not allowed";
}
};
void main()
{
int bagWeight, age, luggageCount, seatNo;
float cost;
cout<<"Enter Bag weight :"<<endl;
cin>>bagWeight;
cout<<"Enter Passsenger Age :"<<endl;
cin>>age;
cout<<"Enter number of Luggage Passenger has :"<<endl;
cin>>luggageCount;
cout<<"Enter Seat No :"<<endl;
cin>>seatNo;
cout<<"Enter Flight Cost Paid:"<<endl;
cin>>cost;
try
{
if(bagWeight>40)
throw BagException(); //throw exception of type BagException
if(age<10)
throw AgeException(); //throw exception of type AgeException
if(luggageCount>10)
throw LuggageException(); //throw exception of type LuggageException
if(seatNo < 5)
throw seatNo; //throw exception of type int
if(cost < 0)
throw cost; //throw exception of type float
catch(AgeException e)
{
cout<<"\nCaught an exception : "<<endl;
cout<<e.what()<<endl;
}
catch(LuggageException e)
{
cout<<"\nCaught an exception : "<<endl;
cout<<e.what()<<endl;
}
catch(int seatNo)
{
cout<<"\nCaught an exception :"<<endl;
cout<<"Seat no below -"<< seatNo <<" are a reserved....”<<endl
}
catch(float price)
{
cout<<"\nCaught an exception :"<<endl;
cout<<"Cost :” << price << “ is less than zero and Invalid....”<<endl
}
cout<<"\n End";
}
OUTPUT
10) a) Write a C++ program to concatenate 2 strings using STL string class function.
#include <iostream>
using namespace std;
int main()
{
string s1, s2, result;
result = s1 + s2;
Output:
Write a simple C++ program to store and display integer elements using STL Vector class
#include <iostream>
#include <vector>
int main()
{
vector<int> v;
int n, element;