Question

Write a C++ program that includes the following: Define the class Student in the header file Stu...

Write a C++ program that includes the following:

  • Define the class Student in the header file Student.h.
#ifndef STUDENT_H
#define STUDENT_H

#include <string>
#include <iostream>

using namespace std;

class Student
{
public:
  // Default constructor
  Student()
  {
  
  }

  // Creates a student with the specified id and name. 
  Student(int id, const string& name)
  {
  
  }

  // Returns the student name.
  string get_name() const
  {
  
  }

  // Returns the student id.
  int get_id () const
  {
  
  }

  // Sets the student name.
  void set_name(const string& name)
  {
  
  }

  // Sets the student id.
  void set_id(int id)
  {
  
  }
 
  // Prints the student id and name.
  void print_student() const
  {
  
  }

private:
  // student name
  string name;

  // student id
  int id;
};

struct NodeType
{
   Student student;
   NodeType* next;
   
   NodeType(): student(), next(nullptr)
   {}
        
   NodeType(const Student& s): student(s), next(nullptr)
   {}
};

#endif

  • Impletment the class Student in the file Student.h.
  • The main() function is contained in the file lab02.cpp. The main() function,
    1. Declares a pointer students which points to NodeType.
    2. Prompts the user to enter a student information (id and name) and adds this student in the linked structure, stops adding the students when the user enters 0 as a student id.
    3. Prompts the user to enter the student id to be removed, and remove the student from the linked structure.
    4. Displays all students added in the linked structure.

The expected result:

Enter the student id: 1101
Enter the student name: Taylor
The student is added

Enter the student id: 1102
Enter the student name: Smith
The student is added

Enter the student id: 1103
Enter the student name: Alice
The student is added

Enter the student id: 1104
Enter the student name: Tom
The student is added

Enter the student id: 0

The student id to be removed: 1102
The student is removed

The students are:
1104 Tom
1103 Alice
1101 Taylor 

Compile

This lab exercise should be put under cse330/lab02 subdirectory.

$g++ -c Student.h
$g++ -c lab02.cpp
$g++ lab02.o -o lab02
$./lab02

Hand In

  • Student.h: the header file.
  • lab02.cpp: the client test file containing main() funcion.
  • lab02result: the script file which captures the result.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//updated Student.h file

#ifndef STUDENT_H

#define STUDENT_H

#include <string>

#include <iostream>

using namespace std;

class Student

{

public:

// Default constructor

Student()

{

              id=0;

              name="";

}

// Creates a student with the specified id and name.

Student(int id, const string& name)

{

              this->id=id;

              this->name=name;

}

// Returns the student name.

string get_name() const

{

              return name;

}

// Returns the student id.

int get_id () const

{

              return id;

}

// Sets the student name.

void set_name(const string& name)

{

              this->name=name;

}

// Sets the student id.

void set_id(int id)

{

              this->id=id;

}

// Prints the student id and name.

void print_student() const

{

              cout<<id<<" "<<name<<endl;

}

private:

// student name

string name;

// student id

int id;

};

struct NodeType

{

   Student student;

   NodeType* next;

  

   NodeType(): student(), next(nullptr)

   {}

       

   NodeType(const Student& s): student(s), next(nullptr)

   {}

};

#endif

// main program

#include<iostream>

#include "Student.h"

using namespace std;

//method to de allocate memory once the program is about to finish

void free_memory(NodeType *node){

                //looping and deleting all nodes until it becomes empty

                while(node!=nullptr){

                                NodeType *temp=node;

                                node=node->next;

                                delete temp;

                }

}

int main() {

                //defining a NodeType pointer to head node

                NodeType *head=nullptr;

                //a pointer denoting current position

                NodeType *current=nullptr;

                //variables for handling student id and name

                int studId=-1;

                string name;

                //loops until student id is 0

                do{

                                //asking for id

                                cout<<endl<<"Enter the student id: ";

                                cin>>studId;

                                //if id is not 0, asking for name

                                if(studId!=0){

                                               cout<<"Enter the student name: ";

                                               cin>>name;

                                               //creating a student

                                               Student stud(studId,name);

                                               //adding as head if head is null

                                               if(head==nullptr){

                                                               head=new NodeType(stud);

                                                               //setting current to head

                                                               current=head;

                                               }else{

                                                               //creating a new node

                                                               NodeType *node=new NodeType(stud);

                                                               //adding next to current and updating current

                                                               current->next=node;

                                                               current=node;

                                               }

                                }

                }while(studId!=0);

               

                //asking for the id to remove

                cout<<endl<<"The student id to be removed: ";

                cin>>studId;

               

                //a flag to denote if deletion was successful

                bool found=false;

                //checking if head is not empty

                if(head!=nullptr){

                                //checking if head node has the required student id

                                if(head->student.get_id()==studId){

                                               //updating head node   

                                               head=head->next;

                                               found=true;

                                }else{

                                               current=head;

                                               //loops until the end of list, or until the student to remove is found

                                               while(current->next!=nullptr){

                                                               if(current->next->student.get_id()==studId){

                                                                               //found, removing current->next node

                                                                               current->next=current->next->next;

                                                                               found=true;

                                                                               break;

                                                               }

                                                               current=current->next;

                                               }

                                }

                }

               

                //displaying a message stating the result of removal

                if(found){

                                cout<<endl<<"The student is removed"<<endl;

                }else{

                                cout<<endl<<"The student is not found"<<endl;                

                }

               

                //looping and printing all students

                cout<<endl<<"The students are: "<<endl;

                current=head;

                while(current!=nullptr){

                                current->student.print_student();

                                current=current->next;

                }

               

                //deallocating memory to prevent memory leakage

                free_memory(head);

                return 0;

}

/*OUTPUT*/

Enter the student id: 1102

Enter the student name: Bob

Enter the student id: 1010

Enter the student name: James

Enter the student id: 1234

Enter the student name: Oliver

Enter the student id: 1108

Enter the student name: John

Enter the student id: 0

The student id to be removed: 1010

The student is removed

The students are:

1102 Bob

1234 Oliver

1108 John

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that includes the following: Define the class Student in the header file Stu...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Use C++ to create a class called Student, which represents the students of a university. A...

    Use C++ to create a class called Student, which represents the students of a university. A student is defined with the following attributes: id number (int), first name (string), last name (string), date of birth (string), address (string). and telephone (area code (int) and 7-digit telephone number(string). The member functions of the class Student must perform the following operations: Return the id numb Return the first name of the student Modify the first name of the student. . Return the...

  • Write the functions needed to complete the following program as described in the comments. Use the...

    Write the functions needed to complete the following program as described in the comments. Use the input file course.txt and change the mark of student number 54812 to 80. /* File: course.cpp A student's mark in a certain course is stored as a structure (struct student as defined below) consisting of first and last name, student id and mark in the course.  The functions read() and write() are defined for the structure student.   Information about a course is stored as a...

  • My main() file does not call to the class created in a .hpp file. It comes...

    My main() file does not call to the class created in a .hpp file. It comes out with the error "undefined reference to "___" const. I want to solve this by only changing the main() .cpp file, not the .hpp file. .cpp file .hpp file Thank you! include <iostream> include <string> tinclude "CourseMember.hpp using namespace std int main0 CourseMember CourseMember(90, "Jeff", "Goldblum"): // initializing the constructor cout<< "Member ID is <<CourseMember.getID) << endl; 1/ calling getID) accessor cout <<"First Name...

  • A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double...

    A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double GPA(=(4.0*NumberOfAs+3.0*NumberOfBs+2.0*NumberOfCs+1.0*NumberOfDs)/( As+Bs+Cs+Ds+Fs)); Public data member, void setFirstName(string name); string getFirstName() const; void printFirstName() const; void getLastName(string name); string getLastName() const; void printLastName() const; void computeGPA(int NumberOfAs,int NumberOfBs,int NumberOfCs,int NumberOfDs,int NumberOfFs); double getGPA() const; double printGPA() const; A destructor and an explicit default constructor initializing GPA=0.0 and checking if GPA>=0.0 by try{}catch{}. Add member function, bool operator<(const Student); The comparison in this function based on...

  • Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h...

    Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h (Given. Just use it, don't change it!) Student.cpp (Partially filled, need to complete) 1. Assignment description In this assignment, you will write a simple class roster management system for ASU CSE100. Step #1: First, you will need to finish the design of class Student. See the following UML diagram for Student class, the relevant header file (class declaration) is given to you as Student.h,...

  • In c programming . Part A: Writing into a Sequential File Write a C program called...

    In c programming . Part A: Writing into a Sequential File Write a C program called "Lab5A.c" to prompt the user and store 5 student records into a file called "stdInfo.txt". This "stdInfo.txt" file will also be used in the second part of this laboratory exercise The format of the file would look like this sample (excluding the first line) ID FIRSTNAME LASTNAME GPA YEAR 10 jack drell 64.5 2018 20 mina alam 92.3 2016 40 abed alie 54.0 2017...

  • (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and...

    (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and is implemented with a header node and a tail node. // SortedLinkedList.h // SortedLinkedList.h // A collection of data are stored in the list by ascending order #ifndef SORTEDLIST_H #define SORTEDLIST_H using namespace std; template <typename T> class SortedList { private: // The basic single linked list node type. // Nested inside of SortedList. struct NodeType { T data; NodeType* next; NodeType* prev; NodeType(const...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT