C++

C++  Programming

Slip17

Q.1) Write a C++ program to create a class Student which contains data members as
Roll_Number, Stud_Name, Percentage. Write member functions to accept Student
information. Display all details of student along with a class obtained depending on
percentage. (Use array of objects) [Marks 15]

Solution

#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
 private:
int rno;
char sname[20];
float per;
char cls[40];
 public:
void accept();
void display();
void check();
 };
void student:: accept()
{
 cout<<"Enter the roll no of student:";
 cin>>rno;
 cout<<"Enter the student name:";
 cin>>sname;
 cout<<"Enter the  percentage:";
 cin>>per;
}
void student:: display()
{
 cout<<"\nRoll no of student:"<<rno;
 cout<<"\nStudent name:"<<sname;
 cout<<"\npercentage:"<<per;
 cout<<"\nClass obtained:"<<cls;
}
void student:: check()
{
 if(per>=80)
 {
  strcpy(cls,"First class with distinction");
 }
 else if(per<80 && per>=65)
 {
  strcpy(cls,"First class");
 }
 else if(per<65 && per>=55)
 {
  strcpy(cls,"Second class");
 }
 else if(per<55 && per>=40)
 {
  strcpy(cls,"Pass");
 }
 else if(per<40 && per>=35)
 {
  strcpy(cls,"Just pass");
 }
 else if(per<35)
 {
  strcpy(cls," Sorry, you r fail");
 }
}
void main()
{
 student s[10],w[10];
 int i,n;
 clrscr();
 cout<<"\nEnter how many student details u want:";
 cin>>n;
 for(i=0;i<n;i++)
 {
  s[i].accept();
 }
 for(i=0;i<n;i++)
 {
   s[i].check();
 }
 for(i=0;i<n;i++)
 {
  s[i].display();
 }
 getch();
}


Output

Enter how many student details u want:3
Enter the roll no of student:1
Enter the student name:Akshay
Enter the  percentage:78
Enter the roll no of student:2
Enter the student name:Ravi
Enter the  percentage:56
Enter the roll no of student:3
Enter the student name:Sagar
Enter the  percentage:65

Roll no of student:1
Student name:Akshay
percentage:78
Class obtained:First class
Roll no of student:2
Student name:Ravi
percentage:56
Class obtained:Second class
Roll no of student:3

Student name:Sagar                                                           
percentage:65                                                                 
Class obtained:First class                                                   
                 


Q.2) Create a class Distance which contains data members as: kilometer, meter. Write C++
program to perform following functions:
i. To accept a distance
ii. To display a distance
iii. To overload += operator to add two distances.
iv. To overload > operator to compare two distances [Marks 25] 

Solution

#include<iostream.h>
#include<conio.h>
class distance
{
float km,m;
public:
void accept()
{
cout<<"Enter a Kilometer: ";
cin>>km;
cout<<"Enter meter: ";
cin>>m;
}
void display()
{
cout<<"Kilometer : "<<km<<endl;
cout<<"Meter:"<<m<<endl;
}
void operator +=(distance &d)
{
km=km+d.km;
m=m+d.m;
}
void operator >(distance &d)
{
float l1,l2;
l1=km+(m/100);
l2=d.km+(d.m/100);
if(l1>l2)
cout<<"first object distance is greater : "<<l1<<"km";
else
cout<<"Second object distance is greater : "<<l2<<"km";
}
};
int main()
{
distance d1,d2,d3;
clrscr();
int choice;
do
{
cout<<"\n 1:To accept a distance\n 2:To Display a distance\n 3:To overload +=operator to add two distance\n";
cout<<" 4:To overload > Operator to compare two distances\n 5:Exit\n";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1: d1.accept();
d2.accept();
break;
case 2:d1.display();
d2.display();
break;
case 3:cout<<"Addition Of two Objct is.... "<<endl;
d1+=d2;
d1.display();
break;
case 4: d1>d2;
break;
}
}while(choice!=5);
getch();
return 0;
}


Output

 1:To accept a distance
 2:To Display a distance
 3:To overload +=operator to add two distance
 4:To overload > Operator to compare two distances
 5:Exit
Enter your choice: 1
Enter a Kilometer: 5.5
Enter meter: 2.6
Enter a Kilometer: 5.9
Enter meter: 2.8

 1:To accept a distance
 2:To Display a distance
 3:To overload +=operator to add two distance
 4:To overload > Operator to compare two distances
 5:Exit
Enter your choice: 2
Kilometer : 5.5
Meter:2.6
Kilometer : 5.9
Meter:2.8

 1:To accept a distance
 2:To Display a distance
 3:To overload +=operator to add two distance
 4:To overload > Operator to compare two distances
 5:Exit
Enter your choice: 3
Addition Of two Objct is....
Kilometer : 11.4
Meter:5.4

 1:To accept a distance
 2:To Display a distance
 3:To overload +=operator to add two distance
 4:To overload > Operator to compare two distances
 5:Exit
Enter your choice: 4
first object distance is greater : 11.454km
 1:To accept a distance
 2:To Display a distance
 3:To overload +=operator to add two distance
 4:To overload > Operator to compare two distances
 5:Exit
Enter your choice: 5
BCA Pratical Solution

My name is Vivek And I from Mumbai and Complete my Graduation Bca.my Age is 23 Years.

Post a Comment

Previous Post Next Post