Write a program to do the following task in C++ language:
(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 3 arts students.
(iv) Display entryno, name, marks for science students first and then for arts students.
Program Code:
#include<iostream>
using namespace std;
//base class name student having some data members and member functions
class student
{
protected:
int entryno;
char name[20];
public:
void getdata()
{
cout<<"Enter name of the student : " ;
cout<<endl;
cin>>name;
cout<<endl;
}
void display()
{
cout<<"Name of the student is : "<<name;
cout<<endl;
}
};
// derived class name science which is derived from student class
class science : public student
{
int pcm[3];
public:
void getdata()
{
student :: getdata();
cout<<"Enter marks for Physics,Chemistry and Mathematics : ";
cout<<endl;
for(int j=0; j<3; j++)
{
cin>>pcm[j];
}
}
void display()
{
entryno=1;
cout<<"Entry no for Science student is : "<<entryno;
cout<<endl;
student :: display();
cout<<"Marks in Physics,Chemistry and Mathematics are : ";
cout<<endl;
for(int j=0; j<3; j++)
{
cout<<pcm[j];
cout<<endl;;
}
}
};
// derived class name arts which is derived from student class
class arts : public student
{
int ehe[3];
public:
void getdata()
{
student :: getdata();
cout<<"Enter marks for English,History and Economics : ";
cout<<endl;
for(int j=0; j<3; j++)
{
cout<<ehe[j];
cout<<endl;;
}
}
};
//main function which is used to call all functions of base and derived class
int main()
{
science s1[3]; // array of object of science class
arts a1[3]; //array of object of science class
int i,j,k,l;
cout<<"Entry for Science students : ";
cout<<endl;
for(i=0; i<3; i++)
{
s1[i].getdata();
}
cout<<"Details of three Science students are : ";
cout<<endl;
for(j=0; j<3; j++)
{
s1[j].display();
}
cout<<"Entry for Arts students : ";
cout<<endl;
for(k=0; k<3; k++)
{
a1[k].getdata();
}
cout<<"Details of three Arts students are : ";
cout<<endl;
for(l=0; l<3; l++)
{
a1[l].display();
}
return 0;
}
// copyright to this program is reserved with the author.
Thanks
Mukesh Rajput
Post A Comment:
0 comments: