Create a complete program based on the following criteria: (a) Create class Stud
ID: 3636448 • Letter: C
Question
Create a complete program based on the following criteria:(a) Create class Student.
(i) data members (protected): name (string), location (string), age (int).
(ii) member functions : - constructor to display “Student Details”
(b) Create class Marks.
(i) data members (protected): marks[3] (float) , grade (char).
(ii) member functions : - void mark_entry() --- get user input for marks
(using for loop).
- void average() --- calculate average of 3 marks.
Call function set_grade(..).
- void set_grade(..) --- determine grade.
If average >= 80, grade = ‘A’
If average >= 50, grade = ‘B’
If average >= 0, grade = ‘C’
(c) Create class Result which inherits publicly from class Student and class Marks.
(i) data members : semester (int).
(ii) member functions : - void setStudent() --- get user input for name,
location, age, semester
- void display() --- display name, location, age,.
Semester, all the marks and grade.
(d) In main()
(i) Declare a pointer called Stud1 that points to an object of class Result (created
using the new operator).
(ii) Using the pointer Stud1, call setStudent(), mark_entry(), average() and
display().
(iii) Delete the object.
(iv) Repeat the steps above by getting the number of students in main() during
runtime.(here you have to use Dynamic Memory Allocation)
//Solution
#include<iostream>
#include<string>
using namespace std;
class Student
{
protected :
string name, location;
int age;
public :
Student()
{
cout<<"---------------------------------------------"<<endl;
cout<<" Student Details "<<endl;
cout<<"---------------------------------------------"<<endl;
}
};
class Marks
{
protected :
float marks [3];
char grade;
public :
void mark_entry()
{
float sum=0;
cout<<"--------------------------------------------"<<endl;
cout<<" Marks Details "<<endl;
cout<<"--------------------------------------------"<<endl;
for (int i=0;i<3;i++)
{
cout<<"Enter Marks : ";
cin>>marks[i];
sum+=marks[i];
}
}
void average()
{
float average,sum;
average=sum/3;
void set_grade(float average);
}
void set_grade(float average)
{
if(average>=80)
{
grade= 'A';
}
else if (average>=50)
{
grade= 'B';
}
else
{
grade= 'C';
}
}
};
class Result:public Student, public Marks
{
protected :
int semester;
public :
void setStudent()
{
cout<<"Enter Name : ";
cin>>name;
cout<<"Enter Location : ";
cin>>location;
cout<<"Enter Age : ";
cin>>age;
cout<<"Enter Semester : ";
cin>>semester;
}
void display()
{
cout<<"--------------------------------------------"<<endl;
cout<<" Result Slip "<<endl;
cout<<"--------------------------------------------"<<endl;
cout<<"Name : "<<name<<endl;
cout<<"Location : "<<location<<endl;
cout<<"Age : "<<age<<endl;
cout<<"Semester : "<<semester<<endl;
for(int i=0;i<3;i++)
{
cout<<"Marks "<<i+1<<" : "<<marks[i]<<endl;
}
cout<<"Grade : "<<grade<<endl;
}
};
int main()
{
int number;
cout<<"Please Enter The Number Of Student : ";
cin>>number;
for(int i=0;i<number;i++)
{
class Result *Stud1;
Stud1= new Result;
(*Stud1).setStudent();
(*Stud1).mark_entry();
(*Stud1).average();
(*Stud1).display();
delete Stud1;
}
system("pause");
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
class Student
{
protected:
string name;
string location;
int age;
public:
Student()
{
cout << "---------------------------------------------" << endl;
cout << " Student Details " << endl;
cout << "---------------------------------------------" << endl;
}
};
class Marks
{
protected:
float marks[3];
char grade;
public:
void mark_entry()
{
cout << "--------------------------------------------" << endl;
cout << " Marks Details" << endl;
cout << "--------------------------------------------" << endl;
for (int i = 0; i < 3; i++)
{
cout << "Enter Marks: ";
cin >> marks[i];
//You don't need the variable "sum" here, since mark_entry() is used for getting user input only, you don't have to calculate anything yet
}
}
void average()
{
float average = 0.0; //initialize average = 0.0
for (int i = 0; i < 3; i++)
{
average += marks[i]; //use average as "sum"
}
average /= 3.0; //after adding up all the marks, calculate its average here
set_grade(average); //don't declare function's type and parameter's type again when you call the function
}
void set_grade(float average)
{
if(average >= 80)
{
grade = 'A';
}
else if (average >= 50)
{
grade = 'B';
}
else
{
grade = 'C';
}
}
};
class Result : public Student, public Marks
{
protected:
int semester;
public:
void setStudent()
{
cout << "Enter Name: ";
cin >> name;
cout << "Enter Location: ";
cin >> location;
cout << "Enter Age: ";
cin >> age;
cout << "Enter Semester: ";
cin >> semester;
}
void display()
{
cout << "--------------------------------------------" << endl;
cout << " Result Slip" << endl;
cout << "--------------------------------------------" << endl;
cout << "Name: " << name << endl;
cout << "Location: " << location << endl;
cout << "Age: " << age << endl;
cout << "Semester: " << semester << endl;
for(int i = 0; i < 3; i++)
{
cout << "Marks " << i+1 << ": " << marks[i] << endl;
}
cout << "Grade: " << grade << endl;
}
};
int main()
{
int number;
cout << "Please Enter The Number Of Student: ";
cin >> number;
for (int i = 0; i < number; i++)
{
Result *Stud1; //you don't need keyword "class" here
Stud1 = new Result;
Stud1->setStudent(); // (*Stud1). equals to Stud1->
Stud1->mark_entry();
Stud1->average();
Stud1->display();
delete Stud1;
}
cin.ignore(); //use other codes to pause the program than system("pause");
cin.get();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.