Está en la página 1de 14

Page 1 of 14

6TH SEMESTER B.E-MECHANICAL

C++ LAB PROGRAMS


PROGRAM 1:: Write a c++ program to convert time given n seconds to hours:minutes:seconds and vice versa

//C++ program to convert time given in seconds to hours:minutes:seconds


:minutes:seconds and vice versa
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sec,hr,min,totalsec,choice;
cout<<"Enter 1 to input time in total seconds\n";
seconds
cout<<"Enter 2 to input time in hours:minutes:seconds\n";
hours:minutes:seconds
cout<<"choice:";
cin>>choice;
if(choice==1)
{
cout<<"Enter time in total seconds:";
cin>>totalsec;
hr=totalsec/3600;
min=(totalsec%3600)/60;
sec=totalsec-(min*60)-(hr*3600);
(hr*3600);
cout<<"Time in hours:seconds:minutes="<<hr<<":"<<min<<":"<<sec;
}
if(choice==2)
{
cout<<"Enter number of hours:";
cin>>hr;
cout<<"Enter number of minutes:";
cin>>min;
cout<<"Enter number of seconds:";
cin>>sec;
totalsec=hr*3600+min*60+sec;
cout<<"Total time in seconds="<<totalsec;
}
getch();
}
OUTPUT 1

OUTPUT 2

PROGRAM 2: Given that an employee class contains following members employee number, name, basic salary,
DA, IT, net salary. Member functions to read the data, to calculate the net salary and print data members. Write a
c++ program to read the data of n employees and compute net salary.

//C++ program to read data compute net salary and print data of n employees
employe

Department of Mechanical Engineering, University Visvesvaraya College of Engineering, Bangalore


Bangalore-01.
Page 2 of 14

#include<iostream.h>
#include<conio.h>
class employee
{
private:int empno;
char empname[30];
int bsal;
float da;
float it;
float gsal;
float nsal;
public:void readdata()
{
cout<<"Enter the employee no.:";
cin>>empno;
cout<<"Enter the employee name:";
cin>>empname;
cout<<"Enter
t<<"Enter the basic salary in Rs.:";
cin>>bsal;
cout<<"Enter the percentage DA:";
cin>>da;
cout<<"Enter the percentage IT:";
cin>>it;
}
void compute()
{
gsal=bsal+(bsal*da/100);
nsal=gsal-(gsal*it/100);
(gsal*it/100);
}
void printdata()
{
cout<<empno<<"\t"<<empname<<"
t"<<empname<<"\t"<<bsal<<"\t"<<gsal<<"
t"<<gsal<<"\t"<<nsal<<"\n";
}
};
void main()
{
clrscr();
employee emp[100];
int n,i;
cout<<"Enter the no. of employees:";
mployees:";
cin>>n;
for(i=0;i<n;i++)
emp[i].readdata();
for(i=0;i<n;i++)
emp[i].compute();
cout<<"Emp no.\tEmp name\tBasic
tBasic salary(Rs.)\tGross
salary(Rs.) salary(Rs.)\tNet
tNet salary(Rs.)
salary(Rs.)\n";
for(i=0;i<n;i++)
emp[i].printdata();
getch();
}
OUTPUT

Department of Mechanical Engineering, University Visvesvaraya College of Engineering, Bangalore


Bangalore-01.
Page 3 of 14

PROGRAM 3(a): Write a program to show the function overload

//C++ program for function overloading


#include<iostream.h>
#include<conio.h>
int area(int a);
int area(int a,int b);
float area(int l,float h);
float area(float r);
int area(int a)
{
int ar;
ar=a*a;
return ar;
}
int area(int a,int b)
{
int ar;
ar=a*b;
return ar;
}
float area(int l,float h)
{
float ar;
ar=0.5*l*h;
return ar;
}
float area(float r)
{
float ar;
ar=3.142*r*r;
return ar;
}
void main()
{
clrscr();
int a,b,l,sarea,rarea;
float h,r,tarea,carea;
cout<<"Enter side length of square\n";
square
cin>>a;
sarea=area(a);
cout<<"Area of square="<<sarea<<"\n";
square="<<sarea<<"
cout<<"Enter length and breadth of rectangle\n";
rectangle
cin>>a>>b;
rarea=area(a,b);
cout<<"Area of rectangle="<<rarea<<"\n";
rectangle="<<rarea<<"
cout<<"Enter
<<"Enter base length and height of triangle\n";
triangle
cin>>l>>h;
tarea=area(l,h);
cout<<"Area of triangle="<<tarea<<"\n";
triangle="<<tarea<<"
cout<<"Enter the radius of circle\n";
circle
cin>>r;
carea=area(r);
cout<<"Area of circle ="<<carea<<"\n";
="<<carea<<"
getch();
}
OUTPUT

Department of Mechanical Engineering, University Visvesvaraya College of Engineering, Bangalore


Bangalore-01.
Page 4 of 14

PROGRAM 3(b):: Write a program to show the use of function template

//C++ program
rogram to show the use of function template
#include<iostream.h>
#include<conio.h>
template<class T>
T add(T x,T y)
{
T z;
z=x+y;
cout<<"Sum of the two numbers="<<z<<"\n";
numbers="<<z<<"
}
void main()
{
clrscr();
int a,b;
float p,q;
double m,n;
cout<<"Enter the two integer numbers\n";
numbers
cin>>a>>b;
add(a,b);
cout<<"Enter the two real numbers\n";
numbers
cin>>p>>q;
add(p,q);
cout<<"Enter two numbers of double type\n";
type
cin>>m>>n;
add(m,n);
getch();
}
OUTPUT

PROGRAM 4:: Write a program to show the nested classes

//C++
C++ program for nested class
#include<iostream.h>
#include<conio.h>
class student
{
private:char name[30];
int regno;
int age;
public:class date
{
private:int day;
int month;
int year;
public:void inputdate()
{

Department of Mechanical Engineering, University Visvesvaraya College of Engineering, Bangalore


Bangalore-01.
Page 5 of 14

cout<<"Enter the date, month and year:";


cin>>day>>month>>year;
}
void outputdate()
{
cout<<"Day:"<<day<<"\n";
cout<<"Day:"<<day<<"
cout<<"Month:"<<month<<"\n";
cout<<"Month:"<<month<<"
cout<<"Year:"<<year<<"\n";
cout<<"Year:"<<year<<"
}
};
void inputdata()
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter Reg no.:";
cin>>regno;
cout<<"Enter the age:";
cin>>age;
}
void outputdata()
{
cout<<"Name:"<<name<<"
cout<<"Name:"<<name<<"\n";
cout<<"Reg no.:"<<regno<<"\n";
no.:"<<regno<<"
cout<<"Age:"<<age<<"\n";
cout<<"Age:"<<age<<"
}
};
void main()
{
clrscr();
student s;
student::date d;
s.inputdata();
d.inputdate();
s.outputdata();
d.outputdate();
getch();
}
OUTPUT

PROGRAM 5:: Write a c++ program to illustrate the normal banking transaction such as deposit, withdrawal,
balance and display. Use a constructor to input initial values of an object

//C++ program to illustrate normal banking transaction using constructor to input initial values of an object
#include<iostream.h>
#include<conio.h>
class customer
{
private:char name[30];
int accno;
float balance;
public:customer()

Department of Mechanical Engineering, University Visvesvaraya College of Engineering, Bangalore


Bangalore-01.
Page 6 of 14

{
cout<<"Enter the name of the customer\n";
customer
cin>>name;
cout<<"Enter the account number\n";
number
cin>>accno;
cout<<"Enter the balance in the account\n";
account
cin>>balance;
}
void deposit()
{
float amount;
cout<<"Enter the amount to be deposited\n";
deposited
cin>>amount;
balance=balance+amount;
}
void withdrawl()
{
float wamount;
cout<<"Enter the amount to be withdrawn\n";
withdrawn
cin>>wamount;
if(wamount<=balance)
balance=balance-wamount;
wamount;
else
cout<<"Withdrawl not possible\n";
possible
}
void display()
{
cout<<"Name\tAccount
tAccount No.\tBalance\n";
No.
cout<<name<<"\t"<<accno<<"
t"<<accno<<"\t"<<balance<<"\n";
}
};
void main()
{
clrscr();
customer c1;
c1.deposit();
c1.withdrawl();
c1.display();
getch();
}
OUTPUT

PROGRAM 6:: Define a student class with reg. no., name, and marks in 3 subjects. Declare an array of n student
objects. Using appropriate function find the average marks of each student. Print the reg. no., name and the
average marks of all the students

//C++ program to find the


he average marks of 3 subjects using functions
#include<iostream.h>

Department of Mechanical Engineering, University Visvesvaraya College of Engineering, Bangalore


Bangalore-01.
Page 7 of 14

#include<conio.h>
class students
{
private:char stdname[30];
int regno;
int sub1,sub2,sub3;
float avg;
public:void getdata()
{
cout<<"Enter
"Enter the name of the student:";
cin>>stdname;
cout<<"Enter the Reg. no.:";
cin>>regno;
cout<<"Enter the marks of the three subjects:";
cin>>sub1>>sub2>>sub3;
}
void average()
{
avg=(sub1+sub2+sub3)/3.0;
}
void putdata()
{
cout<<stdname<<"\t"<<regno<<"
t"<<regno<<"\t"<<avg<<"\n";
}
};
void main()
{
clrscr();
students std[100];
int n,i;
cout<<"Enter the no. of students:";
cin>>n;
for(i=0;i<n;i++)
{
std[i].getdata();
std[i].average();
}
cout<<"Details of students\ \n";
cout<<"Name\tReg. no.\tAverage
tAverage\n";
for(i=0;i<n;i++)
{
std[i].putdata();
}
getch();
}
OUTPUT

Department of Mechanical Engineering, University Visvesvaraya College of Engineering, Bangalore


Bangalore-01.
Page 8 of 14

PROGRAM 7: Write a program which has two base classes employee and student. The data members of employee
are name, emp. no. and that of students are school name and degree. The data member of scientist being no. of
publications and that of manager being designation. Classes scientist and class manager are also derived from
student. Apply the details of n employee and display them.

#include<iostream.h>
#include<conio.h>
class employee
{
private:char name[30];
int empno;
public:void getemp()
{
cout<<"Enter the employee name:";
cin>>name;
cout<<"Enter the employee number:";
cin>>empno;
}
void putemp()
{
cout<<empno<<"\t"<<name;
}
};
class student
{
private:char schoolname[30];
char degree[20];
public:void getstu()
{
cout<<"Enter the school name:";
cin>>schoolname;
cout<<"Enter the degree:";
cin>>degree;
}
void putstu()
{
cout<<"\t"<<schoolname<<"\t"<<degree;
}
};
class scientist:public employee,public student
{
private:int noofpub;
public:void getsc()
{
getemp();
getstu();
cout<<"Enter the no.of publicatons:";
cin>>noofpub;
}
void putsc()
{
putemp();
putstu();
cout<<"\t"<<noofpub<<"\n";
}
};
class manager:public employee,public student
{
private:char title[20];
public:void getma()

Department of Mechanical Engineering, University Visvesvaraya College of Engineering, Bangalore-01.


Page 9 of 14

{
getemp();
getstu();
cout<<"Enter the name of the title of manager:";
cin>>title;
}
void putma()
{
putemp();
putstu();
cout<<"\t"<<title<<"\
\n";
}
};
void main()
{
clrscr();
manager m[100];
scientist s[100];
int i,m1,n;
cout<<"Enter
"Enter the number of managers:";
cin>>m1;
cout<<"Enter the number of scientist:";
cin>>n;
cout<<"Enter the details of managers\n";
managers
for(i=0;i<m1;i++)
m[i].getma();
cout<<"Enter the details of scientists\n";
scientists
for(i=0;i<n;i++)
s[i].getsc();
cout<<"The
e details of the manager\n";
manager
cout<<"\Emp no.\tEmp name\tSchool
tSchool name\tDegree\tTitle\n";
name
for(i=0;i<m1;i++)
m[i].putma();
cout<<"The details of the scientist\n";
scientist
cout<<"Emp no.\tEmp name\tSchool
tSchool name\tDegree\tNo.
name tNo. of publications\n";
publications
for(i=0;i<n;i++)
s[i].putsc();
getch();
}
OUTPUT

PROGRAM 8:: Implement a class called person having data members as name,
name, date of birth and address. Derive a
class student from person having data members rool no. and semester. Derive another class exam from student
which has data members marks1 and marks2 and average. Compute the average, display the details

Department of Mechanical Engineering, University Visvesvaraya College of Engineering, Bangalore


Bangalore-01.
Page 10 of 14

#include<iostream.h>
#include<conio.h>
class person
{
private:char name[20];
char dob[20];
char addr[50];
public:void getdata()
{
cout<<"Enter the name:";
cin>>name;
cout<<"Enter the DOB in dd_mm_yyyy:";
cin>>dob;
cout<<"Enter the address:";
cin>>addr;
}
void putdata()
{
cout<<name<<"\t"<<dob<<"\t"<<addr;
}
};
class student:public person
{
private:int rollno;
int sem;
public:void getdata1()
{
cout<<"Enter the roll no.:";
cin>>rollno;
cout<<"Enter the sem:";
cin>>sem;
}
void putdata1()
{
cout<<"\t"<<rollno<<"\t"<<sem;
}
};
class exam:public student
{
private:int m1;
int m2;
float avg;
public:void getdata2()
{
getdata();
getdata1();
cout<<"Enter the marks obtained in 1st test:";
cin>>m1;
cout<<"Enter the marks obtained in 2nd test:";
cin>>m2;
avg=(m1+m2)/2.0;
}
void putdata2()
{
putdata();
putdata1();
cout<<"\t"<<avg<<"\n";
}
};
void main()

Department of Mechanical Engineering, University Visvesvaraya College of Engineering, Bangalore-01.


Page 11 of 14

{
clrscr();
exam e[100];
int i,n;
cout<<"Enter the no. of students:";
cin>>n;
cout<<"Enter the details\n";
n";
for(i=0;i<n;i++)
e[i].getdata2();
cout<<"Exam details of the studens\n";
studens
cout<<"Name\tAddress\tDOB\tRoll
tRoll No.\tsemester\tAverage\n";
No.
for(i=0;i<n;i++)
e[i].putdata2();
getch();
}
OUTPUT

PROGRAM 9:: Write a program to create a base class student (name, reg. no. and age) and using inheritance create
the classes ugstudent (semester and fees) and pgstudent (semester, fees and stipend). Enter the data of n
students and display the details of ug and pg student

//C++ program on class inheritance


#include<iostream.h>
#include<conio.h>
class student
{
private:char name[20];
int regno;
int age;
public:void getdata()
{
cout<<"Enter the name of the student:";
cin>>name;
cout<<"Enter the register number:";
cin>>regno;
cout<<"Enter the age:";
cin>>age;
}
void display()
{
cout<<name<<"\t"<<regno<<"
t"<<regno<<"\t"<<age;
}
};
class ugstudent:public student
{
private:int sem;
float fees;

Department of Mechanical Engineering, University Visvesvaraya College of Engineering, Bangalore


Bangalore-01.
Page 12 of 14

public:void getdata1()
{
getdata();
cout<<"Enter the semester:";
cin>>sem;
cout<<"Enter the fess:Rs.";
cin>>fees;
}
void display1()
{
display();
cout<<"\t"<<sem<<"\t"<<fees<<"\n";
}
};
class pgstudent:public student
{
private:int sem;
int stipened;
float fees;
public:void getdata2()
{
getdata();
cout<<"Enter the semester:";
cin>>sem;
cout<<"Enter the stipened:";
cin>>stipened;
cout<<"Enter the fees:Rs.";
cin>>fees;
}
void display2()
{
display();
cout<<"\t"<<sem<<"\t"<<stipened<<"\t"<<fees<<"\n";
}
};
void main()
{
ugstudent ug[100];
pgstudent pg[100];
clrscr();
int i,m,n;
cout<<"Enter the number of UG students:";
cin>>n;
cout<<"Enter the number of PG students:";
cin>>m;
cout<<"Enter the details of UG students\n";
for(i=0;i<n;i++)
ug[i].getdata1();
cout<<"Enter the details of PG students\n";
for(i=0;i<m;i++)
pg[i].getdata2();
cout<<"The details of the UG students";
cout<<"Name\tReg_No.\tAge\tSemester\tFees(Rs.)\n";
for(i=0;i<m;i++)
ug[i].display1();
cout<<"The details of PG students";
cout<<"Name\tReg no.\tAge\tSemester\tStipened\tFees(Rs.)\n";
for(i=0;i<m;i++)
pg[i].display2();
getch();
}

Department of Mechanical Engineering, University Visvesvaraya College of Engineering, Bangalore-01.


Page 13 of 14

OUTPUT

PROGRAM 10:: Write a c++ program to overload assignment operator (=)

//C++ program on overload assignment operator (=)


#include<iostream.h>
#include<conio.h>
#include<string.h>
class book
{
private:char title[30];
int issnno;
float price;
public:void getdata()
{
cout<<"Enter the title of the book:";
cin>>title;
cout<<"Enter the ISSN no.:";
cin>>issnno;
cout<<"Enter the price of the book:Rs.";
cin>>price;
}
void operator=(book & bk)
{
strcpy(title,bk.title);
issnno=bk.issnno;
price=bk.price;
}
void display()
{
cout<<"Title of the book:"<<title<<"\n";
book:"<<title<<"
cout<<"ISSN no.:"<<issnno<<"\n";
no.:"<<issnno<<"
cout<<"Price of the book: Rs."<<price<<"\n";
Rs."<<price<<
}
};
void main()
{
clrscr();
book b1,b2;
b1.getdata();
cout<<"Details of the book 1\n";
1
b1.display();
b2=b1;
cout<<"Details of the book 2\n";
2

Department of Mechanical Engineering, University Visvesvaraya College of Engineering, Bangalore


Bangalore-01.
Page 14 of 14

b2.display();
getch();
}
OUTPUT

Note: For corrections please contact me at kitty.hemanth@gmail.com

Department of Mechanical Engineering, University Visvesvaraya College of Engineering, Bangalore


Bangalore-01.

También podría gustarte