Completely Solved C, C++ Programs Assignment.




C++ Program design class:student,sports,exam and results.

Filed Under:

Problem Statement :
Design 4 classes: student, exam, sports, and result. The student class is having the data members such as roll_no, name, dob, department, college. Create the class exam by inheriting all the members of Student. This class is having the scores of six subjects as the local data member. Now create another class sports also by inheriting the members of Student. Sports class has the local value performance grade. (If p_g = ‘A’ then 50, p_g = ‘B’ then 40, p_g = ‘C’ then 30, p_g = ‘D’ then 20) marks will be added to the total marks. Now derive a class result from exam and sports to calculate the result of the student. Include appropriate member functions to each class.

Logical Approach :
The above program is done using an Abstract Data Type (ADT) called class. A class is a mechanism to create objects.We have used inheritance feature in this program. The mechanism of deriving a new class(derived class) from an old one(base class) is called inheritance. This program is based on hybrid inheritance(combination of multiple, multilevel and hierarchical inheritance) In this program a class named student is created ,
which contains the following data members :
 roll_no
 name[]
 dept_name[]
 college[]
 dob[]
and following member functions :
 get_input() - Used to input basic student information(declared as data members of this class).
 print_data() - Used to display basic student information.
exam is another class which is derived from student class and contains the following data member :
 marks[]
and following member functions :
 get_marks()-Used to input marks of six subjects.
 print_marks()-Used to display marks of six subjects.
sports is another class which is also derived from student class and contains the following data member :
 p_g(denotes performance grade)
and following member functions :
 get_ grade () - Used to input performance grade of a student .
 print_ grade () - Used to display performance grade of a student.
result is another class derived from both exam and sports which contains following data member :
 grand_total
and following member functions :
 get_info() - Used to get all the information(basic, performance in exam and sports) of a student. Actually this body of this member function just contains above mentioned member functions.
 display() - Used to display all the information of a student along with his or her result(total marks obtained combining marks of six subjects and p_g in sports).

Algorithm :
Creating an ADT with name student

Step 1 : Creating an ADT student

Step 2 : Declaring private data members
int roll_no //Declaring an integer
char name[] //Declaring a character array
char dept_name[] //Declaring a character array
char college[] //Declaring a character array
char dob[] //Declaring a character array

Step 3 : Declaring public member functions
 get_input()
 print_data()

This function is used to input basic student information

get_input()
{
Read roll_no, name, dept_name, College, dob;
}

This function is used to display basic student information

print_data()
{
Display roll_no, name, dept_name, College, dob;
}

Creating an ADT with name exam

Step 1 : Creating an ADT exam

Step 2 : Declaring private data members
int marks[] //Declaring an integer array

Step 3 : Declaring public member functions
 get_marks()
 print_marks()

This function is used to input marks of six subjects

get_marks()
{
for(int i=1 to 6)
{
Read marks[i];
}
}

This function is used to display marks of six subjects

print_marks()
{
for(int i=1 to 6)
{
display marks[i];
}
}

Creating an ADT with name sports

Step 1 : Creating an ADT sports

Step 2 : Declaring private data members
char p_g //Declaring a character variable

Step 3 : Declaring public member functions
 get_ grade ()
 print_ grade ()

This function is used to input performance grade of a student

get_grade()
{
Read p_g;
}

This function is used to display performance grade of a student

print_grade()
{
Display p_g;
}

Creating an ADT with name result

Step 1 : Creating an ADT result

Step 2 : Declaring private data members
int grand_total //Declaring a integer variable

Step 3 : Declaring public member functions
 get_ info ()
 display ()

This function is used to get all the information(basic, performance in exam and sports) of a student

get_info()
{
get_input();
get_marks();
get_grade();
}

This function is used to display all the information of a student along with his or her result

display()
{
grand_total=0;
for(int i=1 to 6)
grand_total=grand_total+marks[i];
if(p_g='A')
grand_total= grand_total +50;
if(p_g='B')
grand_total= grand_total +40;
if(p_g='C')
grand_total= grand_total +30;
if(p_g='D')
grand_total= grand_total +20;
print_data();
print_marks();
print_grade();
display grand_total;
}

Main procedure

Read number of students in num;
Dynamically allocating number of instances same as num;
for(i=1 to num)
stu[i].get_info();
for(i=1 to num)
stu[i].display();


Program listing :
//C++ program to implement a student database using inheritance

#include<iostream>

using namespace std;
//Creating a class with name student
class student
{
protected:
int roll_no;
char name[40];
char dept_name[40];
char college[40];
char dob[12];
public:
void get_input();
void print_data();
};
//This function is used to input basic student information
void student::get_input()
{
cout<<"\nEnter roll-no:";
cin>>roll_no;
cout<<"\nEnter name:";
cin.ignore();
cin.getline(name,100);
cout<<"\nEnter department name:";
cin.getline(dept_name,20);
cout<<"\nEnter college name:";
cin.getline(college,20);
cout<<"\nEnter date of birth of this student(in DD/MM/YY format):";
cin.getline(dob,12);
}
//This function is used to display basic student information
void student::print_data()
{
cout<<"\n\nRoll:"<<roll_no;
cout<<"\n\nName:"<<name;
cout<<"\n\nDate of birth:"<<dob;
cout<<"\n\nDepartment:"<<dept_name;
cout<<"\n\nCollege:"<<college;
}
//Creating an class with name exam
class exam:virtual public student
{
protected:
int marks[5];
public:
void get_marks();
void print_marks();
};
//This function is used to input marks of six subjects
void exam::get_marks()
{
cout<<"\nEnter Marks\n-------------------\n";
for(int i=0;i<6;i++)
{
while(true)
{
cout<<"\nSubject"<<i+1<<":";
cin>>marks[i];
if(marks[i]>=0&&marks[i]<=100)//Checking validity of marks
break;
cout<<"Invalid marks given!!Enter proper marks..\n";
}
}
}
//This function is used to display marks of six subjects
void exam::print_marks()
{
cout<<"\n\nMarks obtained in theory\n---------------------------\n";
for(int i=0;i<6;i++)
cout<<"Subject"<<i+1<<": "<<marks[i]<<"\n";
}
//Creating a class with name sports
class sports:virtual public student
{
protected:
char p_g;
public:
void get_grade();
void print_grade();
};
//This function is used to input performance grade of a student
void sports::get_grade()
{
while(true)
{
cout<<"\nEnter performance grade in sports for this student(A/B/C/D):";
cin>>p_g;
if(p_g=='A'||p_g=='B'||p_g=='C'||p_g=='D')
break;
cout<<"\nInvalid grade entered!!!";
}
}
//This function is used to display performance grade of a student
void sports::print_grade()
{
cout<<"\n\nPerformance grade in sports:"<<p_g;
}
//Creating a class with name result
class result:public exam,public sports
{
private:
int grand_total;
public:
void get_info();
void display();
};
//This function is used to get all the information(basic, performance in exam and sports) of a student
void result::get_info()
{
get_input();
get_marks();
get_grade();
}
//This function is used to display all the information of a student along with his or her result
void result::display()
{
grand_total=0;
for(int i=0;i<6;i++)
grand_total+=marks[i];
if(p_g=='A')
grand_total+=50;
if(p_g=='B')
grand_total+=40;
if(p_g=='C')
grand_total+=30;
if(p_g=='D')
grand_total+=20;
print_data();
print_marks();
print_grade();
cout<<"\n\nAll total marks of "<<name<<":"<<grand_total<<"\n";
}
int main()
{
int num,i;
while(true)
{
cout<<"Enter number of students:";
cin>>num;
if(num>0)//Checking validity of number of students
break;
cout<<"Invalid number given!!\n";
}
result *stu=new result[num];//Dynamically allocating number of instances
for(i=0;i<num;i++)
{
cout<<"\n\nEnter record for student no:"<<i+1<<"\n\n-------------------------\n-------------------------";
stu[i].get_info();
}
cout<<"\n\nDisplaying results of students \n--------------------------------------------------------\n";
//Printing all the student's information
for(i=0;i<num;i++)
{
cout<<"\n\n\nRecord for student no:"<<i+1<<"\n\n------------------------\n------------------------";
stu[i].display();
}
cout<<"\n";
delete[]stu;
return 0;
}



Output:
Enter number of students:3
Enter record for student no:1
-------------------------
-------------------------
Enter roll-no:122

Enter name:Rohit Sharma
Enter department name:Mathematics
Enter college name:Scottish Church
Enter date of birth of this student(in DD/MM/YY format):07/05/1988
Enter Marks
-------------------

Subject1:55

Subject2:67

Subject3:84

Subject4:45

Subject5:90

Subject6:34

Enter performance grade in sports for this student(A/B/C/D):A


Enter record for student no:2

-------------------------
-------------------------
Enter roll-no:167

Enter name:Pritam Biswas
Enter department name:Computer Sc.
Enter college name:Vidyasagar
Enter date of birth of this student(in DD/MM/YY format):12/12/1989

Enter Marks
-------------------

Subject1:56

Subject2:67

Subject3:44

Subject4:37

Subject5:89

Subject6:90

Enter performance grade in sports for this student(A/B/C/D):D


Enter record for student no:3

-------------------------
-------------------------
Enter roll-no:160
Enter name:Paramita Roy
Enter department name:History
Enter college name:Rammohan
Enter date of birth of this student(in DD/MM/YY format):23/11/1991

Enter Marks
-------------------

Subject1:23

Subject2:45

Subject3:88

Subject4:76

Subject5:35

Subject6:57

Enter performance grade in sports for this student(A/B/C/D):C


Displaying results of students
--------------------------------------------------------



Record for student no:1

------------------------
------------------------

Roll:122

Name:Rohit Sharma

Date of birth:07/05/1988

Department:Mathematics

College:Scottish Church

Marks obtained in theory
---------------------------
Subject1: 55
Subject2: 67
Subject3: 84
Subject4: 45
Subject5: 90
Subject6: 34


Performance grade in sports:A

All total marks of Rohit Sharma:425



Record for student no:2

------------------------
------------------------

Roll:167

Name:Pritam Biswas

Date of birth:12/12/1989

Department:Computer Sc.

College:Vidyasagar

Marks obtained in theory
---------------------------
Subject1: 56
Subject2: 67
Subject3: 44
Subject4: 37
Subject5: 89
Subject6: 90


Performance grade in sports:D

All total marks of Pritam Biswas:403



Record for student no:3

------------------------
------------------------

Roll:160

Name:Paramita Roy

Date of birth:23/11/1991

Department:History

College:Rammohan

Marks obtained in theory
---------------------------
Subject1: 23
Subject2: 45
Subject3: 88
Subject4: 76
Subject5: 35
Subject6: 57


Performance grade in sports:C

All total marks of Paramita Roy:354

Discussions :
&#61548; Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.

&#61548; All the files in the C++ standard library declare all of its entities within the std namespace. That is why we have generally included the using namespace std; statement in all programs that used any entity defined in iostream.

&#61548; C++ strongly supports the concept of reusability. Inheritance is an way to reuse classes.

&#61548; There are many types or forms of inheritance- Single, multiple, multilevel, hierarchical and hybrid.

&#61548; In this program all derivation of derived classes from base class are made public.

&#61548; The duplication of inherited members due to multiple paths can be avoided by making the common base class(ancestor class) as virtual base class while declaring the direct or intermediate base classes. We have used this feature in this program to avoid duplication of inherited members from student class to result class.




Back to main directory:  C++ Assignment    Software Practical


Get Free Programming Tutorials and Solved assignments