Laboratory 2: Class creation and string manipulation
-
Topics to be explained by your TA.
-
Creating classes
in C++
-
*.cpp and *.h files
-
constructors / destructors
-
private and public elements
-
The string
class
-
#include <string>
-
string operations (getline, substr, find, length, size)
-
reading from a file with fixed length fields
SOLUTION
Solution Files
YOUR TASK
-
Download the following files into an empty directory.
-
Create an empty project (Lab2) and include the downloaded files in it.
-
Check out the Lab2.cpp file.
-
You will see that this c++ program uses a class names student.
-
Write both the header(student.h) and the implementation(student.cpp)
for this class student with the following specification:
- long int
number;
// the student's id
- string
name;
// the student's name
-
Student()
// constructor
- virtual
~Student()
// destructor
- long int
getNumber()
// get the name
- void setNumber(long int) // set the name
- string
getName()
// get the number
- void
setName(string)
// set the number
-
Include these files (student.h and student.cpp) in the
project
Lab2.
-
Complete the method void readStudent(istream& input,
Student&
s) inside lab2.cpp. This method should :
-
read a line from the input parameter and store it in a string
-
extract the name and number of the student from this string
-
set such name and number for student s
-
Compile and run the program above using Microsoft Visual Studio C++ .NET
-
Use the file students.dat as the input file
-
This file has a number of lines, each one storing a student where the
first
7 digits are the student number and the next 21 the student name:
-
9999999XXXXXXXXXXXXXXXXXXXXX