Write a program with Student as abstract class and create derive classes Arts, Science from base class Student. Create the objects of the derived classes and process them and access them using array of pointer of type base class Student.
#include<iostream>
using namespace std;
class Student
{
protected:
char name[20], depart[20];
public:
virtual void get_data() = 0;
virtual void show_data() = 0;
};
class Arts : public Student
{
public:
void get_data()
{
cout<<"Enter the information of Arts student ";
cout<<"\nEnter Name: ";
cin>>name;
cout<<"Enter Department: ";
cin>>depart;
}
void show_data()
{
cout<<"\nThe information of Arts student is ";
cout<<"\nName: "<<name;
cout<<"\n Department: "<<depart;
}
};
class Science : public Student
{
public:
void get_data()
{
cout<<"\nEnter information of Science Student: \n";
cout<<"Enter name: ";
cin>>name;
cout<<"Enter Department: ";
cin>>depart;
}
void show_data()
{
cout<<"The information of Science student is: \n";
cout<<"\nName: "<<name;
cout<<"\nDepartment: "<<depart;
}
};
int main()
{
Student *stu[2];
stu[0] = new Arts;
stu[1] = new Science;
for(int i = 0; i<3; i++)
{
stu[i]->get_data();
}
for(int i = 0; i< 3; i++)
{
stu[i]->show_data();
}
return 0;
}
Comments
Leave a comment