C++ Programming
Slip11
Q.1) Write a C++ program to find area and volume of cylinder using Inline function.
[Marks 15]
Solution
#include<iostream.h>
#include<conio.h>
inline void volume(int r,int h)
{
float m;
m=3.14*r*r*h;
cout<<"Volume of cylinder:"<<m<<endl;
}
inline void area(int r,int h)
{
float m;
m=2*3.14*r*h+2*3.14*r*r; //A=2ãrh+2ãr2
cout<<"Area of cylinder:"<<m<<endl;
}
void main()
{
int r,h;
clrscr();
cout<<"Enter the value of r & h:";
cin>>r>>h;
volume(r,h);
area(r,h);
getch();
}
Output
Enter the value of r & h:
4
5
Volume of cylinder:251.199997
Area of cylinder:226.080002
Q.2) Create a class College containing data members as College_Id, College_Name,
Establishment_year, University_Name. Write a C++ program with following member
functions:
i. To accept ‘ n’ College details
ii. To display College details of a specified University
iii. To display College details according to a specified establishment year
(Use Array of Object and Function overloading) [Marks 25]
Soluution
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class college
{
private:
int c_id;
char cname[20];
int year;
char uname[30];
int s[20];
public:
void accept();
void display();
void search(char uni[]);
void search(int y);
};
void college:: accept()
{
cout<<"\nEnter the college id:";
cin>>c_id;
cout<<"Enter the college name:";
gets(cname);
cout<<"Enter the Estabilish year:";
cin>>year;
cout<<"Enter the University name:";
gets(uname);
}
void college:: display()
{
cout<<"\nCollege id:"<<c_id;
cout<<"\nCollege name:"<<cname;
cout<<"\nEstabilish year:"<<year;
cout<<"\nUniversity name:"<<uname;
}
void college:: search(char uni[])
{
if((strcmp(uname,uni))==0)
{
display();
}
}
void college:: search(int y)
{
if(year==y)
{
display();
}
}
void main()
{
college c[10];
int i,n,y;
char uni[20];
clrscr();
cout<<"Enter how many details u want:";
cin>>n;
for(i=0;i<n;i++)
{
c[i].accept();
}
cout<<"\nEnter the University name to search:";
gets(uni);
for(i=0;i<n;i++)
{
c[i].search(uni);
}
cout<<"\n\nEnter the year:";
cin>>y;
for(i=0;i<n;i++)
{
c[i].search(y);
}
getch();
}
Output
Enter how many details u want:
2
Enter the college id:123
Enter the college name:sarhad
Enter the Estabilish year:2015
Enter the University name:pune
Enter the college id:321
Enter the college name:morden
Enter the Estabilish year:2016
Enter the University name:mumbai
Enter the University name to search:pune
College id:123
College name:sarhad
Estabilish year:2015
University name:pune
Enter the year:2016
College id:321
College name:morden
Estabilish year:2016
University name:mumbai
Tags:
c++