C++ code using class

Q.7) Create a class student (roll - no, name). Derive two classes science (maths, physics, computer) and commerce (account, stats). Accept details of 10 students dynamically, they could be science or commerce students. Display the details of the entire student in same order of acceptance.

Ans.

class student
{
char name[30];
introllNo;
public:
voidgetData()
{
cout << "Enter Student RollNo => \t";
cin >> rollNo;
cout << "Enter Student Name => \t";
cin >> name;
}
};

class Science: student
{
intmath,physics,computer;
public:
voidgetMarks()
{
getData();

cout << "Enter Maths Marks => \t";
cin >> math;
cout << "Enter Physics Marks => \t";
cin >> physics;
cout << "Enter Computer Marks => \t";
cin >> computer;
}
voidshowMarks()
{
cout <<"\n Maths => " << math << " Physics => " << physics << " computer =>" << computer;
}
};

class Commerce: student
{
intaccount,stats;
public:
voidgetCommerceMarks()
{
getData();

cout << "Enter account marks=> \t";
cin >> account;
cout << "Enter stats marks=> \t";
cin >> stats;

}
voidshowCommerceMarks()
{
cout << "\n Account => " << account << " stats => " << stats;
}
};

void main()
{
Science *sciObj[10];
Commerce *comObj[10];
char stream[10];
intsci=0,com=0;

for(int i=0;i<10;i++)
{
cout << "\n Enter s for science and c for commerce";
cin >> stream[i];

if(stream[i]=='s')
{

sciObj[sci]=new Science();
sciObj[sci]->getMarks();
sci++;
}
else if(stream[i]=='c')
{
comObj[com]=new Commerce();
comObj[com]->getCommerceMarks();
com++;
}
}
sci=com=0;
cout << "\n You have entered the following information";

for(int i=0;i<10;i++)
{
if(stream[i]=='s')
{

sciObj[sci]->showMarks();
sci++;
}
else
{
comObj[com]->showCommerceMarks();
com++;
}
}
}
Post your comment