Friday, 29 December 2017

CPP-Slip3



Q.1)Write a C++ program to create a class Date which contains three data members as dd, mm, yyyy. Create and initialize the object by using parameterized constructor and display date in dd-mon-yyyy format. (Input: 19-12-2014 Output: 19-Dec-2014) Perform validation for month.            

 #include<iostream.h>
#include<conio.h>
class date
{
 int dd,mm,yyyy;
 public:
 date(int,int,int);
 void put(int d,int m,int yy)
 {
  cout<<"Input Date:\t"<<d<<"-"<<m<<"-"<<yy<<"\n\n";
  cout<<"Output Date:\t";
  switch(m)
  {
   case 1:
   cout<<d<<"-Jan-"<<yy;
   break;
   case 2:
   cout<<d<<"-Feb-"<<yy;
   break;
   case 3:
   cout<<d<<"-Mar-"<<yy;
   break;
   case 4:
   cout<<d<<"-Apr-"<<yy;
   break;
   case 5:
   cout<<d<<"-May-"<<yy;
   break;
   case 6:
   cout<<d<<"-June-"<<yy;
   break;
   case 7:
   cout<<d<<"-July-"<<yy;
   break;
   case 8:
   cout<<d<<"-Aug-"<<yy;
   break;
   case 9:
   cout<<d<<"-Sep-"<<yy;
   break;
   case 10:
   cout<<d<<"-Oct-"<<yy;
   break;
   case 11:
   cout<<d<<"-Nov-"<<yy;
   break;
   case 12:
   cout<<d<<"-Dec-"<<yy;
   break;
   default:
   cout<<"You entered wrong month";
   }
   }
   };
  void date::date(int a,int b,int c)
  {
  dd=a;
  mm=b;
  yyyy=c;
   }
   void main()
   {
   int dd,mm,yyyy;
  cout<<"Enter Date in dd-mm-yyyy format:\n\n";
  cin>>dd>>mm>>yyyy;
  date d(dd,mm,yyyy);
  clrscr();
  d.put(dd,mm,yyyy);
  getch();
  }
                                                                     


Q.2)Create a base class Student(Roll_No, Name)  which derives two classes Academic_Marks(Mark1, Mark2, Mark3)  and Extra_Activities_Marks(Marks). Class Result(Total_Marks, Grade) inherits both Academic_Marks and Extra_Activities_Marks classes.  (Use Virtual Base Class)

     Write a C++ menu driven program to perform the following functions:

   1)Build a master table2) Calculate Total_marks and grade


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class student{
protected:
int rno;
char name[10];
public:
void getdata(){
cout<<"Enter Student’s Data:\n\tRoll No.: ";
cin>>rno;
cout<<"\tName: ";
gets(name);
fflush(stdin);
}
};
class test:public virtual student{
protected:
float mk1,mk2;
public:
void get_t_data(){
cout<<"\tMarks:\n\t\tSubject 1: ";
cin>>mk1;
cout<"\t\tSubject2: ";
cin>>mk2;
}
};
class sport:public virtual student{
protected:
float score;
public:
void get_s_data(){
cout<<"\t\tSports: ";
cin>>score;
}
};
class result:public test, public sport{
char grade;
public:
int tot_mks;
void cal(){
tot_mks=mk1+mk2+score;
if((tot_mks/3)>60)
grade='A';
else if((tot_mks/3)>50)
grade='B';
else if((tot_mks/3)>40)
grade='C';
else
grade='F';
}
void display(){
cout<<"\n"<<rno<<"\t "<<name;
cout<<"\t "<<mk1<<"\t "<<mk2<<"\t "<<score;
cout<<"\t"<<tot_mks<<"\t "<<grade;
}
};
void main(){
clrscr();
result r[20];
int n, n1=0, ch, i=0, j;
cout<<"\n\n\t1.Build Master Table.\n\t2.Calculate Total Marks And Grade.\n\t3.Display Details in Ascending order of marks.\n\t4.EXIT.";
while(1){
cout<<"\n\nEnter Your Choice: ";
cin>>ch;
switch(ch){
case 1: cout<<"Enter the number of students: ";
cin>>n;
n1=n+n1;
for(;i<n1;++i){
r[i].getdata();
r[i].get_t_data();
r[i].get_s_data();
}
cout<<"\n\nMaster Table Built Successfully.";
break;
case 2: for(j=0;j<n1;++j)
r[j].cal();
cout<<"\n\nCalculation Done.";
break;
case 3:
cout<<"\n\nRNO\t Name\t Sub1\t Sub2\t Score\t Total(Average)\t Grade";

int str[15],tmp;
for(int i=0;i<n1;i++)
str[i]=r[i].tot_mks;
for(i=0;i<n1;i++)
for(int j=i+1;j<n1;j++){
if(str[i]>str[j]){
tmp=str[i];
str[i]=str[j];
str[j]=tmp;
}
}
for(i=0;i<n1;i++)
for(j=0;j<n1;j++){
if(str[i]==r[j].tot_mks){
r[j].display();
break;
}
}
break;
case 4: goto end;
}
}
end:
}