Friday, 15 November 2013

6. Write a C++ program to create a class called STUDENT with data members USN,Name and Age. Using inheritance, create the classes UGSTUDENT and PGSTUDENT having fields as Semester,Fees and Stipend. Enter the data for at least 5 students. Find the semester wise average age for all UG and PG students separately.

#include<iostream.h> 
#include<conio.h> 
class student 

 public:  int reg,age; 
            char name[20]; 
            void read_data(); 
}; 

class ugstudent:public student 

 public:  int stipend,sem; 
            float fees; 
           void read_data(); 
}; 


class pgstudent:public student 

 public:  int stipend,sem; 
            float fees; 
           void read_data(); 
}; 

/* function to read student details*/ 
void student::read_data() 

 cout<<"\n Enter name:"; 
 cin>>name; 
 cout<<"\n Enter Reg.no."; 
 cin>>reg; 
 cout<<"\n Enter age:"; 
 cin>>age;  
 }
void ugstudent::read_data() 

 student::read_data(); 
 cout<<"\nEnter the sem:"; 
 cin>>sem; 
 cout<<"\nEnter the fees:"; 
 cin>>fees; 
 cout<<"\nEnter the stipend:"; 
 cin>>stipend; 


/* function to read additional details for pgstudents*/ 
void pgstudent::read_data() 

 student::read_data(); 
 cout<<"\nEnter the sem:"; 
 cin>>sem; 
 cout<<"\nEnter the fees:"; 
 cin>>fees; 
 cout<<"\nEnter the stipend:"; 
 cin>>stipend; 


/* main function */ 
void main() 

 ugstudent ug[20]; 
 pgstudent pg[20]; 
 int i,n,m; 
 float average; clrscr(); 
 cout<<"\nEnter the no. of entries in the ugstudent class:"; 
 cin>>n; 
 for(i=1;i<=n;i++) 
 ug[i].read_data(); 
 for(int sem=1;sem<=8;sem++) 
 { 
 float sum=0; 
 int found=0,count=0; 
 for(i=1;i<=n;i++) 
 { 
 if(ug[i].sem==sem) 
 { 
 sum=sum+ug[i].age; 
 found=1;
   count++; 
 } 
 } 
 if(found==1) 
 { 
 average=sum/count; 
 cout<<"\nAverage of age of sem "<<sem<<" is "<<average; 

 } 
 } 
 cout<<"\nEnter the no. of entries of pgstudent class:"; 
 cin>>n; 
 for(i=1;i<=n;i++) 
 pg[i].read_data(); 
 for(sem=1;sem<=8;sem++) 
 { 
 float sum=0; 
 int found=0,count=0; 
 for(i=1;i<=n;i++) 
 { 
 if(pg[i].sem==sem) 
 { 
 sum=sum+pg[i].age; 
 found=1; 
 count++; 
 } 
 } 
 if(found==1) 
 { 
 average=sum/count; 
 cout<<"\nAverage of age of sem "<<sem<<" is "<<average; 
 } 
 } 
 getch(); 


OUTPUT 

Enter the no. of entries in the ugstudent class:3 
Enter name:Anagha 
Enter Reg.no.3 
Enter age:19 
Enter the sem:4 
Enter the fees:12000 
Enter the stipend:3489 
Enter name:Madhulika 
Enter Reg.no.17 
Enter age:20 
Enter the sem:4 
Enter the fees:15000 
Enter the stipend:3245 
Enter name:Ankitha 
Enter Reg.no.4 
Enter age:21 
Enter the sem:4 
Enter the fees:20000 
Enter the stipend:34278 
Average of age of sem 4 is 20 
Enter the no. of entries of pgstudent class:4 
Enter name:SnowWhite 
Enter Reg.no.6 
Enter age:19 
Enter the sem:4 
Enter the fees:78346 
Enter the stipend:478 
Enter name:Cindy 
Enter Reg.no.7 
Enter age:20 
Enter the sem:4 
Enter the fees:76479 
Enter the stipend:8734 
Enter name:Della 
Enter Reg.no.8 
Enter age:21 
Enter the sem:5 
Enter the fees:78578 
Enter the stipend:478 
Enter name:Stella 
Enter Reg.no.9 
Enter age:22 
Enter the sem:5 
Enter the fees:63278
Enter the stipend:748 
Average of age of sem 4 is 19.5 
Average of age of sem 5 is 21.5        

6 comments:

  1. Why you are using 'found' 'count'?

    ReplyDelete
  2. hello this code is little bit wrong because you are using same variable sem in pg students its showing error i have tried so change the name of varible at scope of pg students then it is correct

    ReplyDelete
  3. Judging from the output, I would suggest to add a little bit more format and improve the readability. Like for example, you could clear the screen ( clrscr(); or system("CLS"); ) before you ask the name of the student/candidate.

    ReplyDelete
  4. And to show proof that it works, add choices:
    1. Enter data
    2. Check/print data
    3. (optional)Delete data
    Try and make it more professional

    ReplyDelete
  5. Add a space after the colon and not-good blog background.

    ReplyDelete