3RD Sem C++ Programs
3RD Sem C++ Programs
// 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
#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:
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>
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;
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];
for(i=0;i<n;i++)
{
emp[i].getdata();
emp[i].cal_sal();
emp[i].display();
}
return 0;
}
Output:
#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
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: