classes

Part 1:
Write a class called (Student) which contains the following data members:
- Private members:
• First name (as a string).
• Last Name (as a string).
• GPA (as a double).
• ID (as an integer).
- Public members:
• A default constructor that will assign all the private data to its default values (ID = 0, GPA = 0.00, First and last name should be equal to an empty string).
• All the accessors and mutators functions (set and get functions) for the previous private members. Make sure you validate the gpa (the GPA should not exceed 4.00, if it does then assign it to its default value).
• A function that prints the student’s data as the following:
Name: First_Name Last_Name
ID: Student’s_ID
GPA: Current_GPA


Note: Divide your code into three different files (header, implementation, main).

Part 2:

Write a main program that contains 3 students, read their information from a file (called data.txt) and store them into the objects. Then your program should print the information of the students with the highest GPA and check if he’s an honored student or not (if GPA is greater than or equal to 3.67 then he’s an honored student).

So how read their information from a file (called data.txt) and store them into the objects ??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
 
#include "student.h"
#include<fstream>

int main()
{
    student s1,s2,s3;
    ifstream in;
    in.open ("Data.txt");
    s1.
    
    return 0;
}
#include <iostream>
using namespace std;

class student
{
   private:
      string fname;
      string lname;
      int id;
      double gpa;
   public:
      student();
      student(string,string, int, int);
      ~student();
      void setFname(string);
      string getLname();
      void setLname(string);
      string getLname();
      void setID(int);
      int getID();
      void setGPA(double);
      double getGPA();
      void print();
      void honorList();
};



      student::student()
      {
          fname = " ";
          lname=" ";
          id = 0;
          gpa = 0.0;
      }

      student::student(string f,string l, int a, double g)
      {
          setFname(f);
          setLname(l);
          setID(a);
          setGPA(g);
      }

      void student::seFname(string f)
      {
          if(s.length() >= 2)
            fname = f;
      }

      string student::getFname()
      {
          return fname;
      }

      void student::setLname(int l)
      {
            lname = l;
      }

      int student::getLname()
      {
          return lname;
      }

      void student::setGPA(double g)
      {
          if(g >= 0 && g <= 4)
            gpa = g;
      }

      double student::getGPA()
      {
          return gpa;
      }

      void student::print()
      {
          cout << "Name: " << fname << lname<<endl;
          cout << "ID " << id << endl;
          cout << "GPA: " << gpa << endl;
      }

    /*    should be in the main
	  void student::honorList()
      {
          if (gpa >= 3.5)
            cout << name << " is on the honor list" << endl;
          else
            cout << name << " is NOT on the honor list" << endl;
      }*/



1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream in;
    in.open ("Data.txt");

string text;
if (in.is_open())
	{
		while (getline(myfile, text))
                {
                   //// eache line from the file is inserted to text 'till the end of file, you need to take the data you need using differet str funcion and etc....
// then send it to the object using set fuctions, or after you got all your data, you can use the constructor to create new object with that data
                 }

Last edited on
sorry i didn't get it can u explain more
Why do you have a constructor with arguments? Your assignment only specifies the default constructor.

By the way " " is not an empty string, it is a string with one character. You really should get used to using constructor initialization lists.

1
2
3
4
5
// Default constructor set names to empty stings and id and gpa to zero.
student::student() :   fname(), lname(), id(0), gpa(0.0)
{
    // BLANK BODY.
}


When you declare functions you should also get used to including the names, since this will help you document your code.

1
2
3
4
5
...
  public:
...
     void setFname(string first_name);
...

You should pass non-trivial classes, like std::string, by reference. And don't forget about const correctness.
1
2
3
4
...
     void setFname(const string& first_name); // Pass by const reference because this string will not change.
      string getFname() const; // This function should not alter the state of the class, to have tell the compiler to insure that no modification is made.
...


Topic archived. No new replies allowed.