Ec C++ 358C
Ec C++ 358C
Society’s
Department of
Electronics and Communication Engineering
C++ Programming Laboratory
Subject Code: BEC358C
III Semester BE 2023-24
Faculty Name:Someshree.Uppe
C++ PROGRAMMING LABORATORY
PART-A
1.Write a program to find the largest, smallest & second largest of three numbers. (use
inline function MAX and MIN to find largest & smallest of 2 numbers).
2.Write a program to calculate the volume of different geometric shapes like cube,
cylinder and sphere and hence implement the concept of Function Overloading.
3.Define a STUDENT class with USN, Name, and Marks in 3 tests of a subject.
Declare an array of Ten 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.
5.Demonstrate Simple Inheritance concept by creating a base class FATHER with data
members SurName 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.
6.Write a c++ program to define class name FATHER & SON that holds the income
Respectively.Calculate & display total income of a family using Friend Function.
7.Write a program to accept the student detail such as name and 3 different marks by
get_data() method and display the name and average of marks using display() method.
Define a friend class for calculating the average of marks using the method
mark_avg().
10.Write a c++ program with different class releted through multiple inheritance &
demonstratethe use of different access specifier by means of members variables &
members functions.
11.Write a c++ program to create three objects for a class named count object with
data members such as roll_no& Name.Create a members function set_data() for
setting the data values& display()member function to display which object has
invoked it using_this*pointer.
#include<conio.h>
largest = a;
if (b >= c)
seclargest = b;
else
seclargest = c;
largest = b;
if (a >= c)
seclargest = a;
else
seclargest = c;
else
{
largest = c;
if (a >= b)
seclargest = a;
else
seclargest = b;
int smallest;
smallest = a;
else
if (b <= c)
smallest = b;
else
smallest = c;
}
cout << "smallest number:" << smallest << endl;
int main()
clrscr();
int a, b, c;
max(a, b, c);
min(a, b, c);
getch();
}
Output:
2.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.h>
#include<conio.h>
float vol(int,int);
float vol(float);
int vol(int);
int main()
clrscr();
int r,h,a;
float r1;
cin>>r>>h;
cin>>a;
cin>>r1;
getch();
return 0;
}
float vol(int r,int h)
return(3.14*r*h);
return((4*3.14*r1*r1*r1)/3);
int vol(int a)
return(a*a*a);
}
Output:
3.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.h>
#include <conio.h>
class student
float avg;
public:
void read();
void disp();
void calc();
};
void student::read()
void student::calc()
else
void student::disp()
cout << usn << "\t" << name << "\t" << t1 << "\t" << t2 << "\t" << t3 << "\t"
int main()
clrscr();
int i, n;
cout << "Enter number of students\n";
cin >> n;
stud[i].read();
stud[i].calc();
stud[i].disp();
getch();
return 0;
}
Output:
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.h>
#include <conio.h>
class mat
public:
void readord();
void readmat();
int operator==(mat);
mat operator+(mat);
mat operator-(mat);
};
void mat::readord()
int i, j;
void mat::readmat()
int i, j;
return 1;
else
return 0;
mat temp;
int i, j;
temp.row = row;
temp.col = col;
return temp;
mat temp;
int i, j;
temp.row = row;
temp.col = col;
return temp;
return fout;
int main()
clrscr();
m1.readord();
m2.readord();
if (m1 == m2)
m1.readmat();
m2.readmat();
m4 = m1 - m2;
<< endl;
<< endl;
else
getch();
return 0;
}
Output :
5. Demonstrate Simple Inheritance concept by creating a base class
FATHER with data members SurName 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.h>
#include<conio.h>
class FATHER
protected:
char surname[20];
float bal;
public:
FATHER();
void disp1();
};
FATHER::FATHER()
void FATHER::disp1()
cout << "FATHER FIRST NAME IS" << fname << "\n";
cout << "FATHER SUR NAME IS" << surname << "\n";
cout << "FATHER DATE OF BIRTH IS" << dob << "\n";
public:
void read();
void disp();
};
void SON::read()
void SON::disp()
cout << " name of son is" << fname << "\n";
cout << " sur name is" << surname << "\n";
cout << " son date of birth is" << dob << "\n";
cout << " son bank balance is" << bal << "\n";
int main()
clrscr();
SON s;
s.read();
s.disp1();
s.disp();
getch();
return 0;
}
OUTPUT:
6. Write a C++ program to define class name FATHER and SON that holds the
income respectively. Calculate and display the total income of a family using Friend
function.
#include <iostream.h>
#include <conio.h>
class son;
class father
float incomef;
public:
void read()
};
class son
float incomes;
public:
void read()
};
float sum;
int main()
{
clrscr();
father f;
f.read();
son s;
s.read();
total(f, s);
getch();
return 0;
}
OUTPUT:
7.Write a program to accept the student detail such as name and 3 different
marks by get_data() method and display the name and average of marks
using display() method. Define a friend class for calculating the average of
marks using the method mark_avg().
#include<iostream>
class Student
private:
char name[15];
float m1,m2,m3,avg;
public:
void getData();
void showData();
};
class AVGMARKS
public:
int getAvg(Student t)
return t.avg;
}
};
void Student::getData()
void Student::showData()
cout<<"--------------------------------------"<<endl;
cout<<"--------------------------------------"<<endl;
cout<<"Name:"<<name<<endl; cout<<"Test1:"<<m1<<endl;
cout<<"Test2:"<<m2<<endl; cout<<"Test3:"<<m3<<endl;
cout<<"---------------------------------------\n";
int main()
{
s1.getData();
s1.showData();
}
Output
8.Write a c++ program to explain virtual function(polymorphism) by
creating base class polygon which has virtual function areas two classes
rectangle & triangle derived from polygon & they have area to calculate
&return the area of rectangle&triangle respectively.
#include<iostream.h>
#include<conio.h>
Class polygon
{
Public:
Virtual void area()=0;
};
Class Rectangle:public polygon
{
Private:
Int l,w;
Float area1;
Public:
Void setx()
{
Cout<<”enter the length and width of rectangle”>>endl;
Cin>>l>>w;
}
Void area()
{
Area1=l*w;
Void area()
{
Area1=l*w;
Cout<<”area of rectangle:”<<area1<<endl;
}
};
Class triangle:public polygon
{
Private:
Int b,h;
Float area1;
Public:
Void setx()
{
Cout<<”enter the base and height of triangle”<<endl;
Cin>>b>>h;
}
Void area()
{
Area1=0.5*b*h;
Cout<<”area of triangle:”<<area1<<endl;
}
};
Void main()
{
Rectangle obj1;
Triangle obj2;
Obj1.setx();
Obj2.area();
}
OUTPUT:
enter the length and width of rectangle
2
2
The area of rectangle is
4
enter the base and height of triangle
2
3
The area of triangle is
3
9. Design,Develop and execute a program in c++ based on the following
requirements:An EMPLOYEE class containing data members & members
functions:i)Data members:employee number(an integer),All_Allowances(an
integer),Net_Salary(an integer).(ii)Member functions:To read the data of an
employee,to calculate.Net_Salary& to print thr values of all the data members .(All
allowances=123% of Basic,Income Tax(IT)=30% of gross
salary(basic_salary_all_allowances_IT).
#include<iostream.h>
#include<conio.h>
Class Employee
Private:
Int Empno;
Char Ename[30];
int bs;
int alln;
int net_sal;
int Gsal;
public:
void setdata()
Cin>>Empno>>Ename>>bs;
alln = (123*bs/100);
Gsal = (30*bs/100)*alln;
}
Void display()
Cout<<”Empno : ”<<Empno<<endl;
Cout<<Ename : “<<Ename<<endl;
Cout<<”B.Salary : “<<bs<<endl;
};
Void main()
Employee obj1;
obj1.setData();
obj1.calc();
obj1.display();
}
OUTPUT :
111
Ganesh
200
EMPNO : 111
ENAME : Ganesh
B.Salary : 200
Allowances : 246
#include<iostream.h>
#include<conio.h>
Class A
Public:
Void showA()
Cout<<”I am in showA”<<endl;
};
Class B
Public:
Void showB()
COUT<<”I am in showB”<<endl;
{;
Class C
{
Public:
Void showC()
Cout<<”I am in showC”<<endl;
};
public:
void show()
ShowA();
showB();
};
Void main()
Simple obj1;
Clrscr();
Obj1.show();
Obj1.showC();
}
OUTPUT:
I am in class A
I am in class B
I am in simple class
I am in class C
11.Write a c++ program to create three objects for a class named count object with
data members such as roll_no& Name.Create a members function set_data() for
setting the data values& display()member function to display which object has
invoked it using_this*pointer.
#include<iostream.h>
#include<conio.h>
class count
private:
int roll_no;
char name[30];
public:
void set_data()
cin>>roll_no>>name;
void display()
cout<<”roll no:”<<this->roll_no<<endl;
cout<<”name:”<<this->name<<endl;
}
};
Void main()
{
count obj1,obj2,obj3;
clrscr();
obj1.set_data();
obj2.set_data();
obj3.set_data();
obj1.display();
obj2.display();
obj3.display();
getch();
}
Output:
Enter the roll_no and name
089
nagesh
12. Write a program to implement Exception Handling with minimum 5
exceptions Classes including two built-in exceptions.
#include <iostream>
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"; }
};
int main() {
float cost;
cout << "Enter number of Luggage Passenger has :" << endl;
cin >> luggageCount;
try {
AgeException
} catch (BagException e) {
catch (AgeException e) {
catch (LuggageException e) {
cout << "\nCaught an exception : " << endl;
cout << "Seat no below -" << seatNo << " are a reserved...." << endl;
cout << "Cost : " << price << " is less than zero and Invalid...." << endl;
return 0;
}
Output:
………………………………….Thank You………………………………………..