1- Define a class called CSClass (as in a computer science department class) Def
ID: 3578534 • Letter: 1
Question
1- Define a class called CSClass (as in a computer science department class) Define enough data and functionality to be meaningful. Use an array for student names and one for student numbers as a minimum of data. Define at least 5 methods, making sure they do something useful (like average grades, print out names). You may define them as non-implemented for this part of the exercise. One method MUST be abstract. Be sure to explain why you chose the one you did.
2- Choose three of the methods you defined above, and write them. One of your methods MUST display the proper use of an array, and at least one must usefully use a loop, and there must be an example of a switch. Your abstract method must show an example of how it should be properly implemented.
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
class CSClass
{
string names[20];
int number;
float gpa[20];
public:
void getData()
{
cout<<"enter number of students ";
cin>>number;
for (int i = 0; i < number; ++i)
{
cout<<"Enter name of student "<<i+1<<endl;
cin>>names[i];
cout<<"Enter GPA of student "<<i+1<<endl;
cin>>gpa[i];
}
}
void printData()
{
cout<<" Names of students and their GPA ";
for (int i = 0; i < number; ++i)
{
cout<<names[i]<<" "<<gpa[i]<<endl;
}
}
float AvgGPA()
{
float sum=0;
for (int i = 0; i < number; ++i)
{
sum+=gpa[i];
}
return sum/number;
}
int getGPA(string n)
{
for (int i = 0; i < number; ++i)
{
if(n==names[i])
{
cout<<"GPA is "<<gpa[i]<<endl;
return 1;
}
}
cout<<"Student not found ";
}
virtual int findStudent(string n)
{
for (int i = 0; i < number; ++i)
{
if(n==names[i])
{
cout<<"Name is "<<names[i]<<endl;
cout<<"GPA is "<<gpa[i]<<endl;
return 1;
}
}
cout<<"Student not found ";
}
};
int main(int argc, char const *argv[])
{
int flag=1;CSClass o;
string n;
while(flag)
{
cout<<"1)Enter Data 2)Print Data 3)Print Average GPA 4)Get GPA of student 5)Search student 6)Quit";
int ch;
cin>>ch;
switch(ch)
{
case 1:o.getData();break;
case 2:o.printData();break;
case 3:cout<<"Average gpa is"<<o.AvgGPA()<<endl;break;
case 4:
cout<<"Enter student name ";
cin>>n;
o.getGPA(n);break;
case 5:
cout<<"Enter student name ";
cin>>n;
o.findStudent(n);break;
case 6:flag=0;break;
}
}
return 0;
}
=================================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ csclass.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
1)Enter Data
2)Print Data
3)Print Average GPA
4)Get GPA of student
5)Search student
6)Quit1
enter number of students
2
Enter name of student 1
Ram
Enter GPA of student 1
3
Enter name of student 2
Shyam
Enter GPA of student 2
4
1)Enter Data
2)Print Data
3)Print Average GPA
4)Get GPA of student
5)Search student
6)Quit2
Names of students and their GPA
Ram 3
Shyam 4
1)Enter Data
2)Print Data
3)Print Average GPA
4)Get GPA of student
5)Search student
6)Quit3
Average gpa is3.5
1)Enter Data
2)Print Data
3)Print Average GPA
4)Get GPA of student
5)Search student
6)Quit4
Enter student name
Ram
GPA is 3
1)Enter Data
2)Print Data
3)Print Average GPA
4)Get GPA of student
5)Search student
6)Quit5
Enter student name
Ram
Name is Ram
GPA is 3
1)Enter Data
2)Print Data
3)Print Average GPA
4)Get GPA of student
5)Search student
6)Quit6
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.