0% found this document useful (0 votes)
45 views47 pages

3RD Sem C++ Programs

The document contains multiple C++ programming tasks including finding the largest, smallest, and second largest of three numbers using inline functions, calculating the volume of geometric shapes using function overloading, defining a STUDENT class to compute average marks, and demonstrating inheritance and polymorphism concepts. Each task includes code snippets and expected outputs. Overall, the document serves as a guide for various C++ programming exercises focusing on object-oriented concepts.

Uploaded by

harshamurthy2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views47 pages

3RD Sem C++ Programs

The document contains multiple C++ programming tasks including finding the largest, smallest, and second largest of three numbers using inline functions, calculating the volume of geometric shapes using function overloading, defining a STUDENT class to compute average marks, and demonstrating inheritance and polymorphism concepts. Each task includes code snippets and expected outputs. Overall, the document serves as a guide for various C++ programming exercises focusing on object-oriented concepts.

Uploaded by

harshamurthy2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 47

1.

Write a C++ program to find largest, smallest & second largest of


three numbers using inline
functions MAX & Min.

numbers using inline functions MAX & Min.


#include <iostream>
inline int MAX(int a, int b , int c)
{
if(a>=b && a>=c)
return a;
else if(b>=a && b>=c)
return b;
else
return c;
}
inline int Min(int a, int b , int c)
{
if(a<=b && a<=c)
return a;
else if(b<=a && b<=c)
return b;
else
return c;
}
int main()
{
int h,k;
int x,y,z; // Variables to store three numbers
cout<<"Enter three numbers:\n";
cin>>x>>y>>z;
h=MAX(x,y,z);
k=Min(x,y,z);
cout<<"largest is: "<<h<<"\nSmallest is: "<<k<<"\n";
if(h==x&&k==y|| h==y&& k==x)
cout<<"Second largest is: "<<z<<"\n";
else if((h==x&&k==z|| h==z&& k==x))

cout<<"Second largest is: "<<y<<"\n";


else
cout<<"Second largest is: "<<x<<"\n";
return 0;
}
2.Write a C++ program to find the volume of cube, sphere
and cylinder using function overloading.
#include<iostream>
using namespace std;
#define PI 3.1416

// function prototypes
float volume(float length, float breadth, float height);
float volume(float radius);
float volume(float radius, float height);

int main(){
float cube_l = 40.0, cube_b = 30.0, cube_h = 10.0;
float sphere_r = 2.5;
float cylinder_r = 2.5, cylinder_h = 10.0;
cout<<"Volume of Cube ="<<volume(cube_l, cube_b, cube_h)<<endl;
cout<<"Volume of Sphere ="<<volume(sphere_r)<<endl;
cout<<"Volume of Cylinder ="<<volume(cylinder_r, cylinder_h)<<endl;
return 0;
}
// function defination
float volume(float length, float breadth, float height){
return length * breadth * height;
}
float volume(float radius){
return (4.0/3.0) * PI * radius * radius *radius;
}
float volume(float radius, float height){
return PI * radius *radius * height;
}

OUTPUT
Volume of Cube =12000
Volume of Sphere =65.45
Volume of Cylinder =196.35

Write a C++
Program to
define a
STUDENT class
with USN, Name
and
Marks in 3 tests
of subject.
Declare an
array of 1O
STUDENT
objects.
Using
appropriate
functions, find
the average of
two better
marks for
each student.
Print the USN,
Name and
average marks
Write a C++
Program to
define a
STUDENT class
with USN, Name
and
Marks in 3 tests
of subject.
Declare an
array of 1O
STUDENT
objects.
Using
appropriate
functions, find
the average of
two better
marks for
each student.
Print the USN,
Name and
average marks
Write a C++
Program to
define a
STUDENT class
with USN, Name
and
Marks in 3 tests
of subject.
Declare an
array of 1O
STUDENT
objects.
Using
appropriate
functions, find
the average of
two better
marks for
each student.
Print the USN,
Name and
average marks
Write a C++
Program to
define a
STUDENT class
with USN, Name
and
Marks in 3 tests
of subject.
Declare an
array of 1O
STUDENT
objects.
Using
appropriate
functions, find
the average of
two better
marks for
each student.
Print the USN,
Name and
average marks
Write a C++
Program to
define a
STUDENT class
with USN, Name
and
Marks in 3 tests
of subject.
Declare an
array of 1O
STUDENT
objects.
Using
appropriate
functions, find
the average of
two better
marks for
each student.
Print the USN,
Name and
average marks
Write a C++
Program to
define a
STUDENT class
with USN, Name
and
Marks in 3 tests
of subject.
Declare an
array of 1O
STUDENT
objects.
Using
appropriate
functions, find
the average of
two better
marks for
each student.
Print the USN,
Name and
average marks
Write a C++
Program to
define a
STUDENT class
with USN, Name
and
Marks in 3 tests
of subject.
Declare an
array of 1O
STUDENT
objects.
Using
appropriate
functions, find
the average of
two better
marks for
each student.
Print the USN,
Name and
average marks
Write a C++
Program to
define a
STUDENT class
with USN, Name
and
Marks in 3 tests
of subject.
Declare an
array of 1O
STUDENT
objects.
Using
appropriate
functions, find
the average of
two better
marks for
each student.
Print the USN,
Name and
average marks
Write a C++
Program to
define a
STUDENT class
with USN, Name
and
Marks in 3 tests
of subject.
Declare an
array of 1O
STUDENT
objects.
Using
appropriate
functions, find
the average of
two better
marks for
each student.
Print the USN,
Name and
average marks
Write a C++
Program to
define a
STUDENT class
with USN, Name
and
Marks in 3 tests
of subject.
Declare an
array of 1O
STUDENT
objects.
Using
appropriate
functions, find
the average of
two better
marks for
each student.
Print the USN,
Name and
average marks
Write a C++
Program to
define a
STUDENT class
with USN, Name
and
Marks in 3 tests
of subject.
Declare an
array of 1O
STUDENT
objects.
Using
appropriate
functions, find
the average of
two better
marks for
each student.
Print the USN,
Name and
average marks
C++ Lab
programs
Part A
1. Write a C++
Program to
define a
STUDENT class
with USN, Name
and
Marks in 3 tests
of subject.
Declare an
array of 1O
STUDENT
objects.
Using
appropriate
functions, find
the average of
two better
marks for
each student.
Print the USN,
Name and
average marks.
A:
Write a C++
Program to
define a
STUDENT class
with USN, Name
and
Marks in 3 tests
of subject.
Declare an
array of 1O
STUDENT
objects.
Using
appropriate
functions, find
the average of
two better
marks for
each student.
Print the USN,
Name and
average marks

3.Define a STUDENT class with USN, Name, and Marks in 3 tests of a


subject. Declare an array of 20 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 <stdio.h>
#include <conio.h>
class STUDENT
{
private: char USN[10], Name[10];
float marks1, marks2, marks3; //marks for the three
subjects average_marks; //Average marks for
the best two
public: void Readdata();
void Calculate_Average_Marks();
void Displaydata();
};
void STUDENT::Readdata()
{
cout << "Enter the name and USN : " ;
cin >> Name >> USN;
cout << "Enter marks1, marks2, marks3 : " ;
cin >> marks1 >> marks2 >> marks3;
}
void STUDENT::Calculate_Average_Marks()
{
int smallest;
if( (marks1 < marks2) && (marks1 < marks3) ) average_marks =
(marks2+ marks3)/2;
else if (marks2 < marks3) average_marks = (marks1 + marks3)/2;
else average_marks = (marks1 + marks2)/2;
}
void STUDENT::Displaydata()
{
cout << "USN:" << USN << "\t Name:" << Name << "\t Average
Marks:";
cout("%0.2f\n",average_marks);
}
void main()
{
STUDENT student[10];
clrscr();
for(int i=0;i<20;i++)
{
student[i].Read_Data();
}
for(i=0;i<20;i++)
{
student[i].Calculate_Average_Marks();student[i].Display_Data();
}
}

4.Write a C++ program to create class called MATRIX using two-dimensional array
of integers, by overloading the operator == which checks the compatibility of two
matrices to be added and subtracted. Perform the addition and subtraction by
overloading + 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 matrix
{
private:long m[5][5];
int row;int col;
public:void getdata();
int operator ==(matrix);
matrix operator+(matrix);
matrix operator-(matrix);
friend ostream & operator << (ostream &,matrix &);
};
/* function to check whether the order of matrix are same or
not */
int matrix::operator==(matrix cm)
{
if(row==cm.row && col==cm.col)
{
return 1;
}
return 0;
}
/* function to read data for matrix*/
void matrix::getdata()
{
cout<<"enter the number of rows\n";
cin>>row;
cout<<"enter the number of columns\n";
cin>>col;
cout<<"enter the elements of the matrix\n";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cin>>m[i][j];
}
}
}
/* function to add two matrix */
matrix matrix::operator+(matrix am)
{
matrix temp;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
temp.m[i][j]=m[i][j]+am.m[i][j];
}
temp.row=row;
temp.col=col;
}
return temp;
}
/* function to subtract two matrix */
matrix matrix::operator-(matrix sm)
{
matrix temp;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
temp.m[i][j]=m[i][j]-sm.m[i][j];
}
temp.row=row;
temp.col=col;
}
return temp;
}
/* function to display the contents of the matrix */
ostream & operator <<(ostream &fout,matrix &d)
{
for(int i=0;i<d.col;i++)
{
for(int j=0;j<d.col;j++)
{
fout<<d.m[i][j];
cout<<" ";
}
cout<<endl;
}
return fout;
}
/* main function */
void main()
{
matrix m1,m2,m3,m4;
clrscr();
m1.getdata();
m2.getdata();
if(m1==m2)
{
m3=m1+m2;
m4=m1-m2;
cout<<"Addition of matrices\n";
cout<<"the result is\n";
cout<<m3;
cout<<"subtraction of matrices\n";
cout<<"The result is \n";
cout<<m4;
}
else
{
cout<<"order of the input matrices is not identical\n";
}
getch();
}
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 & SON that holds
the income respectively .Calculate & display total income of a family
using Friend function.
#include <iostream.h>
#include <conio.h>
class father;
class son
{
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;
}

7.Write a C++ program to accept the student detail such as name & 3
different marks by get_data()method & display the name & average of
marks using display() method. Define a friend function for calculating
the average 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 a 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 c_polygon
{
protected:
float a,b;
public:
void get_data()
{
cout<<"\nEnter any two floating values:\n";
cin>>a>>b;
}
virtual float area()
{
return 0;
}
};
class c_rectangle:public c_polygon
{
public:
float area()
{
return (a*b);
}
};
class c_triangle:public c_polygon
{
public:
float area()
{
return (b*a/2);
}
};
void main()
{

clrscr();
c_rectangle r;
c_triangle t;
c_polygon *p;
p=&r;
p->get_data();
cout<<"\nArea of rectangle is "<<p->area();
p=&t;
p->get_data();
cout<<"\nArea of triangle is "<<p->area();
getch();
}

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: employeenumber (an integer), Employee_ Name (a string of
characters), Basic_ Salary (in integer), All_Allowances (an integer),Net_Salary (an
integer).
(ii) Member functions: To read the data ofan employee, to calculate Net_Salary & to
print the values of all the data members. (All_Allowances= 123% of Basic, Income Tax
(IT)=30%ofgrosssalary(=basic_Salary_All_Allowances_IT).

#include<iostream>

using namespace std;

class employee
{
char name[15];
int id;
float basic,sal,netsal,da,it;

public:
void getdata();
void display();
float cal_sal();
int getnum();
};

void employee::getdata()
{
cout<<" ID : ";
cin>>id;

cout<<" Name : ";


cin>>name;

cout<<" Basic Salary : ";


cin>>basic;
}

void employee::display()
{

cout.width(5);

cout<<"id:"<<id;
cout.width(15);
cout<<"name:"<<name;
cout.width(10);
cout<<"basic:"<<basic;
cout.width(10);
cout<<"it:"<<it;
cout.width(10);
cout<<"da:"<<da;
cout.width(10);
cout<<"netsal:"<<netsal;

}
int employee::getnum()
{
return id;
}

float employee::cal_sal()
{
da = 1.23 * basic;
sal = da + basic;
it = 0.30 * sal;
netsal = sal - it;
}

int main()
{
short int i,j,n;
employee emp[20];

cout<<"Enter the number of Employees : ";


cin>>n;

for(i=0;i<n;i++)
{
emp[i].getdata();
emp[i].cal_sal();
emp[i].display();
}
return 0;
}

Output:

Enter the number of Employees : 1


ID : 1
Name : ram
Basic Salary : 10000
id:1
name:ram
basic:10000
it:6690
da:12300
netsal:15610

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>
class prn_obj // class declaration
{
//private data members
int rno;
char *name;
public:
void set_data(char *n, int r) //set data member values
{
name=n;
rno=r;
}
void print() // prints data member using 'this' pointer
{
cout<<this->name<<" has invoked print() function"<<endl;
cout<<"The roll number is "<<this->rno<<endl;
}
};
int main()
{
prn_obj ob1,ob2,ob3; // object declaration
ob1.set_data("Suba",1);
ob2.set_data("kayal",2);
ob3.set_data("Jeysree",3);
// calling print function using objects
ob1.print();
ob2.print();
ob3.print();
return 0; //denotes successfull termination

12.Write a C++ program to implement exception handling with


minimum 5 exceptions classes including two built in exceptions.
#include <iostream>
using namespace std;
int main()
double numerator, denominator, arr[4] = {0.0, 0.0, 0.0, 0.0};
int index;

cout << "Enter array index: ";


cin >> index;
try {
// throw exception if array out of bounds
if (index >= 4)
throw "Error: Array out of bounds!";

// not executed if array is out of bounds


cout << "Enter numerator: ";
cin >> numerator;
cout << "Enter denominator: ";
cin >> denominator;

// throw exception if denominator is 0


if (denominator == 0)
throw 0;
// not executed if denominator is 0
arr[index] = numerator / denominator;
cout << arr[index] << endl;
}
// catch "Array out of bounds" exception
catch (const char* msg) {
cout << msg << endl;
}
// catch "Divide by 0" exception
catch (int num) {
cout << "Error: Cannot divide by " << num << endl;
}
// catch any other exception
catch (...) {
cout << "Unexpected exception!" << endl;
}
return 0;
}

OUTPUT1:
Enter array index: 5
Error: Array out of bounds!
OUTPUT2:
Enter array index: 2
Enter numerator: 5
Enter denominator: 0
Error: Cannot divide by 0

OUTPUT3:

Enter array index: 2


Enter numerator: 5
Enter denominator: 2
2.5

You might also like