Write a C++ program to perform the following tasks:
(i) Create ‘Student’ class, with two data members: name and Branch_Number ;
Branch_Number is an integer and name is a string. The value of Branch_Number is 1 for CSE
student and 2 for ECE student.
(ii) Derive two classes ‘CSE’ and ‘ECE’. There are three subjects in each branch. Marks obtained
in each subject is considered as data members. In other word, for the class CSE, the data
members are CSE_sub1_marks, CSE_sub2_marks and CSE_sub3_marks. Similarly, for class
ECE, the data members are ECE_sub1_marks, ECE_sub2_marks and ECE_sub3_marks.
(iii) Input appropriate data from the keyboard for 2 CSE and 3 ECE Students. Write necessary
function to get data.
(iv) Display Branch_Number, name, marks for CSE students first and then for ECE students.
#include <iostream>
#include <string>
using namespace std;
class Student{
private:
//Branch_Number is an integer and name is a string.
string name;
int branch_Number;
public:
virtual void get(){
cout<<"Enter name: ";
getline(cin,name);
cout<<"Enter branch number: ";
cin>>branch_Number;
}
virtual void display(){
cout<<"Name: "<<name<<"\n";
cout<<"Branch number: "<<branch_Number<<"\n";
}
};
class CSE:public Student{
private:
int CSE_sub1_marks, CSE_sub2_marks,CSE_sub3_marks;
public:
void get(){
Student::get();
cout<<"Enter CSE sub1 marks: ";
cin>>CSE_sub1_marks;
cout<<"Enter CSE sub2 marks: ";
cin>>CSE_sub2_marks;
cout<<"Enter CSE sub3 marks: ";
cin>>CSE_sub3_marks;
}
void display(){
Student::display();
cout<<"CSE sub1 marks: "<<CSE_sub1_marks<<"\n";
cout<<"CSE sub2 marks: "<<CSE_sub2_marks<<"\n";
cout<<"CSE sub3 marks: "<<CSE_sub3_marks<<"\n";
}
};
class ECE:public Student{
private:
int ECE_sub1_marks, ECE_sub2_marks,ECE_sub3_marks;
public:
void get(){
Student::get();
cout<<"Enter ECE sub1 marks: ";
cin>>ECE_sub1_marks;
cout<<"Enter ECE sub2 marks: ";
cin>>ECE_sub2_marks;
cout<<"Enter ECE sub3 marks: ";
cin>>ECE_sub3_marks;
}
void display(){
Student::display();
cout<<"ECE sub1 marks: "<<ECE_sub1_marks<<"\n";
cout<<"ECE sub2 marks: "<<ECE_sub2_marks<<"\n";
cout<<"ECE sub3 marks: "<<ECE_sub3_marks<<"\n";
}
};
int main(){
//array of pointers
Student** students=new Student*[5];
for(int i=0;i<5;i++){
int choice=0;
// Input appropriate data from the keyboard for 2 CSE and 3 ECE Students.
while(choice<1 || choice>2){
cout<<"1. Add a new CSE Student\n";
cout<<"2. Add a new ECE Student\n";
cout<<"Your choice: ";
cin>>choice;
cin.ignore();
}
if(choice==1){
CSE* _CSE=new CSE();
_CSE->get();
students[i]=_CSE;
}
if(choice==2){
ECE* _ECE=new ECE();
_ECE->get();
students[i]=_ECE;
}
}
for(int i=0;i<5;i++){
students[i]->display();
delete students[i];
}
system("pause");
return 0;
}
Comments
Leave a comment