0% found this document useful (0 votes)
9 views48 pages

Ec C++ 358C

The document outlines a C++ Programming Laboratory course for BE students, detailing various programming assignments and projects. It includes tasks such as finding the largest and smallest numbers, calculating volumes of geometric shapes, implementing classes for students and matrices, and demonstrating inheritance and polymorphism. Each task is accompanied by example code snippets to guide students in their programming exercises.

Uploaded by

bhaktigcm959
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)
9 views48 pages

Ec C++ 358C

The document outlines a C++ Programming Laboratory course for BE students, detailing various programming assignments and projects. It includes tasks such as finding the largest and smallest numbers, calculating volumes of geometric shapes, implementing classes for students and matrices, and demonstrating inheritance and polymorphism. Each task is accompanied by example code snippets to guide students in their programming exercises.

Uploaded by

bhaktigcm959
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/ 48

S.V.E.

Society’s

BHEEMANNA KHANDRE INSTITUTE OF TECHNOLOGY


BHALKI

Humnabad Road, Bhalki, Bidar, Karnataka 585328

Department of
Electronics and Communication Engineering
C++ Programming Laboratory
Subject Code: BEC358C
III Semester BE 2023-24
Faculty Name:Someshree.Uppe
C++ PROGRAMMING LABORATORY

Subject Code : BEC358C Exam Marks : 100

Hours/Week : 3 Total Hours : 42

I.A Marks : 40 Exam Hours : 3 HRS

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.

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.

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().

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.

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

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.

12.Write a c++ program to implement exception handling with minimum 5


exceptions classes including two built exceptions.
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)
#include <iostream.h>

#include<conio.h>

inline void max(int a, int b, int c)

int largest, seclargest;

if (a >= b && a >= c)

largest = a;

if (b >= c)

seclargest = b;

else

seclargest = c;

else if (b >= c && b >= a)

largest = b;

if (a >= c)

seclargest = a;

else

seclargest = c;

else
{

largest = c;

if (a >= b)

seclargest = a;

else

seclargest = b;

cout << "largest number:" << largest << endl;

cout << "second largest number:" << seclargest << endl;

inline void min(int a, int b, int c)

int smallest;

if (a <= b && a <= c)

smallest = a;

else

if (b <= c)

smallest = b;

else

smallest = c;

}
cout << "smallest number:" << smallest << endl;

int main()

clrscr();

int a, b, c;

cout << "please enter 3 numbers to compare:";

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

cout<<"Enter radius and height of a cylinder:";

cin>>r>>h;

cout<<"Enter side of cube:";

cin>>a;

cout<<"Enter radius of sphere:";

cin>>r1;

cout<<"volume of cylinder is"<<vol(r,h);

cout<<"\n volume of cube is"<<vol(a);

cout<<"\n volume of sphere is"<<vol(r1);

getch();

return 0;

}
float vol(int r,int h)

return(3.14*r*h);

float vol(float r1)

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

char usn[10], name[20];

int t1, t2, t3;

float avg;

public:

void read();

void disp();

void calc();

};

void student::read()

cout << "Enter usn\n";

cin >> usn;

cout << "Enter name\n";

cin >> name;

cout << "Enter 3 test marks\n";

cin >> t1;

cin >> t2;


cin >> t3;

void student::calc()

if ((t1 < t2) && (t1 < t3))

avg = (t2 + t3) / 2;

cout << avg;

else if (t2 < t3)

avg = (t1 + t3) / 2;

else

avg = (t1 + t2) / 2;

void student::disp()

cout << usn << "\t" << name << "\t" << t1 << "\t" << t2 << "\t" << t3 << "\t"

<< avg << endl;

int main()

clrscr();

student stud[' '];

int i, n;
cout << "Enter number of students\n";

cin >> n;

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

stud[i].read();

stud[i].calc();

cout << "\n USN\tNAME\tTEST1\tTEST2\tTEST3\tAVERAGE\n";

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

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

int a[10][10], row, col;

public:

void readord();

void readmat();

int operator==(mat);

mat operator+(mat);

mat operator-(mat);

friend ostream &operator<<(ostream &, mat &);

};

void mat::readord()

int i, j;

cout << "Enter number of rows\n";


cin >> row;

cout << "\nEnter number of coloumns\n";

cin >> col;

void mat::readmat()

int i, j;

cout << "Enter mat elements\n";

for (i = 0; i < row; i++)

for (j = 0; j < col; j++)

cin >> a[i][j];

int mat::operator==(mat m2)

if (row == m2.row && col == m2.col)

return 1;

else

return 0;

mat mat::operator+(mat m2)

mat temp;

int i, j;

for (i = 0; i < row; i++)


for (j = 0; j < col; j++)

temp.a[i][j] = a[i][j] + m2.a[i][j];

temp.row = row;

temp.col = col;

return temp;

mat mat::operator-(mat m2)

mat temp;

int i, j;

for (i = 0; i < row; i++)

for (j = 0; j < col; j++)

temp.a[i][j] = a[i][j] - m2.a[i][j];

temp.row = row;

temp.col = col;

return temp;

ostream &operator<<(ostream &fout, mat &d)

for (int i = 0; i < d.col; i++)

for (int j = 0; j < d.col; j++)

fout << d.a[i][j];


cout << " ";

cout << endl;

return fout;

int main()

clrscr();

mat m1, m2, m3, m4;

cout << "\nEnter order of first matrix:\n";

m1.readord();

cout << "\nEnter order of second matrix:\n";

m2.readord();

if (m1 == m2)

cout << "\nEnter the elements of first mat:\n";

m1.readmat();

cout << "\nEnter the elements of second mat:\n";

m2.readmat();

cout << "First matrix is\n";

cout << m1;

cout << "\n Second matrix is\n";

cout << m2;


m3 = m1 + m2;

m4 = m1 - m2;

cout << "Sum of 2 matrices are\n"

<< endl;

cout << m3;

cout << "Difference of 2 matrices are\n"

<< endl;

cout << m4;

else

cout << "mat sum and difference cannot be computed\n";

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

char fname[20], dob[20];

protected:

char surname[20];

float bal;

public:

FATHER();

void disp1();

};

FATHER::FATHER()

cout << "enter first name of father\n";

cin >> fname;

cout << "enter sur name of father\n";


cin >> surname;

cout << "enter date of birth of father\n";

cin >> dob;

cout << "enter bank balance of father\n";

cin >> bal;

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

cout << "BANK BALANCE IS" << bal << "\n";

class SON : public FATHER

char fname[20], dob[20];

public:

void read();

void disp();

};

void SON::read()

cout << "enter first name of son\n";

cin >> fname;


cout << "enter date of birth of son\n";

cin >> dob;

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

cout << "enter income father\n";

cin >> incomef;

friend void total(father, son);

};

class son

float incomes;

public:

void read()

cout << "enter income son\n";


cin >> incomes;

friend void total(father, son);

};

void total(father f, son s)

float sum;

sum = f.incomef + s.incomes;

cout << "total income is " << 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>

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

cout<< "Avgerage Marks = ";

cout << ob.getAvg(s1) ;

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

Cout<<”enter the Empno,ename and basic Salary”<<endl;

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;

Cout<<”Gross Salary : “<<GSal<<endl;

};

Void main()

Employee obj1;

obj1.setData();

obj1.calc();

obj1.display();

}
OUTPUT :

Enter the Emp no,ename and Basic Salary

111

Ganesh

200

EMPNO : 111

ENAME : Ganesh

B.Salary : 200

Allowances : 246

Gross Salary : 14760


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.

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

};

Class Simple:private A,protected B,public C

public:

void show()

ShowA();

showB();

cout<<”I am in simple class”<<endl;

};

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

cout<<”enter roll no and name”<<endl;

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>

using namespace std;

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() {

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

cout << "Passenger can board the Plane ...";

} catch (BagException e) {

cout << "\nCaught an exception : " << endl;

cout << e.what() << endl;

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

return 0;

}
Output:

………………………………….Thank You………………………………………..

You might also like