Write a complete C++ program to do the following :
(i) ‘Student’ is a base class, having two data members: entryno and name;
entryno is integer and name of 20 characters long. The value of
entryno is 1 for Science student and 2 for Arts student, otherwise it is an error.
(ii) ‘Science’ and ‘Arts’ are two derived classes, having respectively data items marks for Physics, Chemistry, Mathematics and marks for English, History, Economics.
(iii) Read appropriate data from the screen for 3 science and 2 arts
students.
(iv) Display entryno, name, marks for science students first and then for arts students.
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int entryno;
char name[20];
public:
void getdata()
{
cout<<"enter name of the student"<<endl;
cin>>name;
}
void display()
{
cout<<"Name of the student is"<<name<<endl;
}
};
class science:public student
{
int pcm[3];
public:
void getdata()
{
student::getdata();
cout<<"Enter marks for Physics,Chemistry and Mathematics\n";
for(int j=0;j<3;j++)
{
cin>>pcm[j];
}
}
void display()
{
entryno=1;
cout<<"entry no for Science student is"<<entryno<<endl;
student::display();
cout<<"Marks in Physics,Chemistry and Mathematics are"<<endl;
for(int j=0;j<3;j++)
{
cout<<pcm[j]<<endl;;
}
}
};
class arts:public student
{
int ehe[3];
public:
void getdata()
{
student::getdata();
cout<<"Enter marks for English,History and Economics"<<endl;
for(int j=0;j<3;j++)
{
cin>>ehe[j];
}
}
void display()
{
entryno=2;
cout<<"entry no for Arts student is"<<entryno<<endl;;
student::display();
cout<<"Marks in English,History and Economics are"<<endl;
for(int j=0;j<3;j++)
{
cout<<ehe[j]<<endl;;
}
}
};
void main()
{
clrscr();
science s1[3];
arts a1[3];
int i,j,k,l;
cout<<"Entry for Science students"<<endl;
for(i=0;i<3;i++)
{
s1[i].getdata();
}
cout<<"Details of three Science students are"<<endl;
for(j=0;j<3;j++)
{
s1[j].display();
}
cout<<"Entry for Arts students"<<endl;
for(k=0;k<3;k++)
{
a1[k].getdata();
}
cout<<"Details of three Arts students are"<<endl;
for(l=0;l<3;l++)
{
a1[l].display();
}
getch();
}
(i) ‘Student’ is a base class, having two data members: entryno and name;
entryno is integer and name of 20 characters long. The value of
entryno is 1 for Science student and 2 for Arts student, otherwise it is an error.
(ii) ‘Science’ and ‘Arts’ are two derived classes, having respectively data items marks for Physics, Chemistry, Mathematics and marks for English, History, Economics.
(iii) Read appropriate data from the screen for 3 science and 2 arts
students.
(iv) Display entryno, name, marks for science students first and then for arts students.
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int entryno;
char name[20];
public:
void getdata()
{
cout<<"enter name of the student"<<endl;
cin>>name;
}
void display()
{
cout<<"Name of the student is"<<name<<endl;
}
};
class science:public student
{
int pcm[3];
public:
void getdata()
{
student::getdata();
cout<<"Enter marks for Physics,Chemistry and Mathematics\n";
for(int j=0;j<3;j++)
{
cin>>pcm[j];
}
}
void display()
{
entryno=1;
cout<<"entry no for Science student is"<<entryno<<endl;
student::display();
cout<<"Marks in Physics,Chemistry and Mathematics are"<<endl;
for(int j=0;j<3;j++)
{
cout<<pcm[j]<<endl;;
}
}
};
class arts:public student
{
int ehe[3];
public:
void getdata()
{
student::getdata();
cout<<"Enter marks for English,History and Economics"<<endl;
for(int j=0;j<3;j++)
{
cin>>ehe[j];
}
}
void display()
{
entryno=2;
cout<<"entry no for Arts student is"<<entryno<<endl;;
student::display();
cout<<"Marks in English,History and Economics are"<<endl;
for(int j=0;j<3;j++)
{
cout<<ehe[j]<<endl;;
}
}
};
void main()
{
clrscr();
science s1[3];
arts a1[3];
int i,j,k,l;
cout<<"Entry for Science students"<<endl;
for(i=0;i<3;i++)
{
s1[i].getdata();
}
cout<<"Details of three Science students are"<<endl;
for(j=0;j<3;j++)
{
s1[j].display();
}
cout<<"Entry for Arts students"<<endl;
for(k=0;k<3;k++)
{
a1[k].getdata();
}
cout<<"Details of three Arts students are"<<endl;
for(l=0;l<3;l++)
{
a1[l].display();
}
getch();
}
educative indeed.am grateful but the code is too long particularly under exam condition
ReplyDeleteyou are right and in our college they are only giving 1 hour.
ReplyDeleteuseless
ReplyDelete