Friday, 19 January 2018

CPP-Slip17



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)


  #include<stdio.h>
#include<conio.h>
#include<iostream.h>
class student
{
char name[10];
float per;
int rno;
public:
void accept()
{
cout<<"\nEnter rno : ";
cin>>rno;
cout<<"\nEnter name : ";
cin>>name;
cout<<"\nEnter percentage : ";
cin>>per;
}
void display()
{
cout<<"\nrno = "<<rno;
cout<<"\nName = "<<name;
cout<<"\nPercantage = "<<per<<"%";
if(per>=70&&per<=100)
cout<<"\nDistinction";
else if(per<70&&per>=60)
cout<<"\n1st class";
else if(per<60&&per>=50)
cout<<"\n2nd class";
else if(per<50&&per>40)
cout<<"\npass class";
else
cout<<"\nfail";
cout<<"\n=================================";
}
};

void main()
{
int n,i;
student ob[10];
clrscr();
cout<<"enter no of students";
cin>>n;
for(i=0;i<n;i++)
{
ob[i].accept();
}
for(i=0;i<n;i++)
{
ob[i].display();
}
getch();
}


Another way


#include<iostream.h>

#include<conio.h>

class student
{
int Roll_number;
char Stud_name[20];
float percentage;
public:
void getdata();
void putdata();
};

void student::getdata()
{
cout<<"\n\nEnter the Roll number of the Student:";
cin>>Roll_number;
cout<<"\nEnter the name of the student:";
cin>>Stud_name;
cout<<"\nEnter the Percentage:";
cin>>percentage;
}

void student::putdata()
{
cout<<"\n**********************************";
cout<<"\n1.Student Roll No.:"<<Roll_number;
cout<<"\n2.Student Name:"<<Stud_name;
cout<<"\n3.Percentage:"<<percentage;
if(percentage>=70)
cout<<"\n4.Class:Distinction.";
else if(percentage>=60 && percentage<=69)
cout<<"\n4.Class:First Class.";
else if(percentage>=40 && percentage<=59)
cout<<"\n4.Class:Second Class.";
else
cout<<"\n4.Class:Failed.";
}

int main()
{
int i,n;
clrscr();

student s[10];
cout<<"\n*********** Accepting Student Details ***********";
cout<<"\nHow many records you want to enter:";
cin>>n;
for(i=0;i<n;i++)
{
s[i].getdata();
}
cout<<"\n*********** Student Details ***********";
for(i=0;i<n;i++)
{
s[i].putdata();
}
getch();
return 0;
 }
 
                                                         
 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                                  

#include<iostream.h>
#include<conio.h>
class distance
{
public:
int km,m;
void getdata()
{
cout<<"enter distance in kilometer\t";
cin>>km;
cout<<"enter distance in meters\t";
cin>>m;
}
void display()
{
cout<<"entered distance is\t";
cout<<km<<"."<<m;
cout<<endl;
}
void operator+=(distance d)
{
int kilometer=0,meter=0;
kilometer=km+d.km;
meter=m+d.m;
if(meter>=1000)
{
kilometer=km+d.km+(meter/1000);
meter=meter%1000;
}
cout<<"\naddition of two distances is\t"<<kilometer<<"."<<meter;
}
void operator>(distance d)
{
if(km>d.km)
{
cout<<"\ngreater distance is\t"<<km<<"."<<m;
}
else
{
cout<<"\ngreater distance is\t"<<d.km<<"."<<d.m;
}
}
};
void main()
{
clrscr();
            distance d,d1;
d.getdata();
d1.getdata();
d.display();
d1.display();
d+=d1;
d>d1;
getch();
}

Another way

#include<iostream.h>
#include<conio.h>
 class distance
{
 int kilomiter,miter;
 public:
   void getdata()
 {
cout<<"\n\nEnter the kilomiter:\t";
cin>>kilomiter;
cout<<"\nEnter the Miter:\t";
cin>>miter;
 }
  void display()
 {
cout<<"\nAddition of two distance:\t";
cout<<kilomiter<<"."<<miter;
 }
 void display2()
 {
 cout<<kilomiter<<"."<<miter;
 }
distance operator+=(distance &d)
 {
distance t;
t.kilomiter=d.kilomiter+kilomiter;
t.miter=d.miter+miter;
 return t;
 }


int operator >(distance &d)
 {
if(kilomiter>d.kilomiter)
{
return 1;
}
else
{
return 0;
}
}
};
 void main()
{
 distance c1,c2,c3,c4,c5;
 clrscr();
c1.getdata();
c2.getdata();
c3=c1+=c2;
c3.display();
c4.getdata();
c5.getdata();
 if(c4>c5)
 {
cout<<"\n";
c4.display2();
cout<<"\tgreater than\t";
c5.display2();
 }
 else
  {
cout<<"\n";
c5.display2();
cout<<"\tgreater than\t";
c4.display2();
 }
 getch();
}