Tuesday 19 May 2015

C++ program to demonstrate the usage of inheritance

#include<iostream>
#include<stdlib.h>
using namespace std;

class student
{
public:
        char usn[10],name[10];
        int age;

        void read()
        {
                cout<<"Enter the usn,name and age\t";
                cin>>usn>>name>>age;
        }
};

class ugstudent:public student
{
public:
        int sem;
        float vfees,stipend;
        void read_data()
        {
                read();
                cout<<"Enter semester, fees and stipend\t";
                cin>>sem>>vfees>>stipend;
        }
};

class pgstudent:public student
{
public:
        int sem;
        float fees,stipend;;
        void read_data()
        {
                read();
                cout<<"enter semester, fees and stipend\t";
                cin>>sem>>fees>>stipend;
        }
};

int main()
{
        ugstudent ug[5];
        pgstudent pg[5];
        int i,n;
        system("clear");

        cout<<"Enter no of ug students:\t";
        cin>>n;
        for(i=1;i<=n;i++)
        {
                ug[i].read_data();
        }

        cout<<"sem and avg age is\t";
        for(int semes=1;semes<=6;semes++)
        {
                int found=0,count=0;
                float sum=0;
                for(i=1;i<=n;i++)
                {
                        if(ug[i].sem==semes)
                        {
                                found=1;
                                sum=sum+ug[i].age;
                                              count++;
                        }
                }

                if(found==1)
                        cout<<"\n"<<semes<<"\t"<<sum/count<<"\t";
                else
                        cout<<"\n"<<semes<<"\t"<<"0\n";
        }


        cout<<"Enter no of pg students:\t";
        cin>>n;
        for(i=1;i<=n;i++)
        {
                pg[i].read_data();
        }
        cout<<"sem and avg age is:\n";
        for(int semes=1;semes<=6;semes++)
        {
                int found=0,count=0;
                float sum=0;
                for(i=1;i<=n;i++)
                {
                        if(pg[i].sem==semes)
                        {
                                found=1;
                                sum=sum+pg[i].age;
                                count++;
                        }
                }
                if(found==1)
                        cout<<"\n"<<semes<<"\t"<<sum/count<<"\t";
                else
                       cout<<"\n"<<semes<<"\t"<<"0\n";
        }

        return 0;
}

No comments:

Post a Comment