You are on page 1of 10

Lab10 Grades2 Project

=======================
This assignment is project Grades2, as part of solution Grades.
For this project the class Grades is going to:
-

be implemented with non-static-functions only,


and get the className from the user;
adds a constructor with default values,
a destructor, and
type struct as new data-members.

1. Restart solution Grades.


2. Run project Grades1 to make sure the code is not corrupted.
3. Add to solution a new project, Grades2.
4. Set project Grades2 as the Start Up.
5. The Editor-header still lists the files of project Grades1.
You will also display the three new files for Grades2.
After you copy some code from Grades1 to Grades2 files, you
can remove from Editor-header the list of the Grades1 files.
6. For the new Grades2 project:
- declare class Grades in a new empty file Grades2.h.
Copy from Grades1 just the private string className.
Include string as Library directive.
- The class members to be defined in a new empty file Grades2.cpp.
- Implement main(), in a new empty file TestGrades2.cpp, just
as a template.

Set the className from user : implement function SetClassName()


===================================================================
7. Declare and implement a new public (instance, non-static)
function
string SetClassName()
- prompt the user to get a class name;
- use a local variable name of type string;
- implement a while(true)
use getline(cin, name)
and check, for now, for only C++ and C# as legal names.
- and return the name to caller.
Include Grades2.h and Library information.

8. In main():

Update main()
===============

- instantiate an object myClass:


Grades myClass = Grades();
// invisible constructor
- call SetClassName() for object myClass
- and display the name:
"In main(): The class name set by user is "
Note: include Grades2.h and Library information.
9. Compile and run, using legal or illegal class names.
Implement function CheckIfLegalName()
=====================================
10. Instead of checking for the legal name received from user,
inside the function SetClassName() itself, the previous
implementation can be changed by calling a new, private
function
bool CheckIfLegalName(name)

11. Update class Grades first with the private function


bool CheckIfLegalName(string)
Implement function CheckIfLegalName()
=====================================
12. Implement the function by storing the names of some programming
classes using an array of strings and the range-for loop, like:
bool Grades::CheckIfLegalName(string s)
{
string str[] = { "C", "C++", "C#", "Java" };

for ( string x : str)


{
if(x == s)
return true;
}
return false;
Update function SetClassName()
================================

13. Comment out the existing code of SetClassName().


Copy and restructure the code.
Inside the while loop, instead of the check for only C++ and C#,
as legal names, insert the call to CheckIfLegalName() and make
a decision based on the bool returned:
if(b == true)
// legal name
{
courseName = name;
break;
}
else

{
}

cout << "Illegal course name. Try again !\n";


continue;

NOTE: inside the .cpp file, there is no restriction to have function


CheckIfLegalName() located above function SetClassName().
14. Compile and run, to check the syntax and the logic.
Include a parameter-constructor
===============================
15. To keep track of the number of objects instantiated from main(),
add first to class Grades a public int objectID, as the
object-number (its ID).
16. Declare as public a parameter-constructor
Grades(string, size_t);
And implement it to:
- initialize the className;
- initialize the objectID;
- display: Inside parameter-constructor;
- display the assigned values.
Update main()
===============
17. Comment out the existing call of the default-invisibleinstance-constructor
Grades myClass = Grades();
and replace it with a call to the parameter-constructor, like:
Grades myClass {"Programming", 1};
18. Compile and run.

For a second-object, add a top-level-function void SelectClass()


=================================================================
19. You will use new functions, for this second-object.
A top level public function
void SelectClass()
is going to call two functions:
the existing void SetClassName()
and a new one
void SetClassInfo()
to set a full description of a selected programming-class.
Implement first function SelectClass()
======================================
20. Add to class Grades the prototype of the public function
void SelectClass().

and implement it to call first just the existing SetClassName().


For testing, add, after the above call:
For the second object the class name from user is
21. Compile and run, to test the syntax implemented so far.
Update main()
===============
22. Instantiate a second object:
Grades mySecondClass{ "CIS", 2 };
Compile and run: is the parameter-constructor called ?

23. Next, call the SelectClass() for object 2.


SelectClass() will itself call SetClassName().
24. Compile and run.
New data-members of class Grades
==================================
25. The new members of class Grades are going to be three
private structs, used to fully define the second object
mySecondClass, inside a new function void SetClassInfo().
The top-level-struct:
struct Course
{
string semester;
string time;
Textbook textbook;
College college;
};

// as a nested struct
// as a nested struct

has two of its members, Textbook and College, as two


nested structures:
struct TextBook
{
string title;
string author;
string year;
};
struct College
{
string name;
string city;
};

26. And the associated struct-variables:


Course program;
Textbook textbook;
College college;
also to become private-instance-members of class Grades.
27. Compile and run, to test the syntax implemented so far.
Adding function void SetClassInfo()
====================================
28. Function SetClassInfo(), to be also called by SelectClass(),
after the call to SetClassName().
It can be private because it is called from a public
function.
SetClassInfo() is going to get input from user to initialize
all members of type-struct:
semester
time
textbook title
author
year
college name
city
Declare SetClassInfo() first, inside class Grades.
29. Implement the function to use the structs-associated-variables,
to access the structs-members like:
program.semester
program.college.name

// for struct Course


// for struct College inside Course

30. And as one other possible exception-implementation, use goto


loops, for empty-input or illegal name implementations.

Code example for first section, semester:


void Grades::SetClassInfo()
{
restartSemester:
cout << "Enter semester; Spring or Fall: ";
getline(cin, program.semester);
if( program.semester == "" )
{
cout << "No selection ? ";
goto restartSemester;
}
if ((program.semester != "Spring") &&
(program.semester != "Fall")
{
cout << "Illegal selection ! ";
goto restartSemester;
}
// you must also implement validation for all following
// user-inputs
cout << "Enter time; Day or Evening: ";
getline(cin, program.time);
cout << "Enter textbook title: ";
getline(cin, program.textbook.title);
cout << "Enter textbook author: ";
getline(cin, program.textbook.author);
cout << "Enter textbook edition-2012/2013: ";
getline(cin, program.textbook.year);
cout << "Enter College name; Mission or WestValley: ";
getline(cin, program.college.name);
cout << "Enter College city; Santa Clara or Saratoga: ";
getline(cin, program.college.city);

31. Add the call to SetClassInfo() inside function SelectClass().


32. Implement one section at a time.
Compile and run. Check the syntax and logic of your input code.
Implement Function DisplayClassInfo()
-------------------------------------

33. This public function, of type void, when called from main()
will display information about the current class-selection,
from the settings of the two calls from SelectClass():
- update first the .h file with this public void function;
- implement the function to display:
className; college name; city; semester; time; textbook
title, author, edition;
- call it from main().
34. Compile and run.
35. At the end of DisplayClassInfo() add now one more code
segment, to display the publishing-year, like:
if(program.textbook.year == "2012")
cout << " - Eighth" << endl;
else if(program.textbook.year == "2013")
cout << " - Ninth" << endl;
36. Compile and run.
Instantiate a third object
==========================
37. Instantiate a third object inside main():
Grades myThirdClass { "CIT", 3 };
38. Compile and run. Is the parameter-constructor called ?

Include a destructor
====================
39. Include as public the destructor
~Grades();
and implement it as
Grades::~Grades()
{
cout << "Destructor called for object "
<< objectID << endl;
}
The destructor will be automatically called by run-time when
the objects are detected as no longer being used by your code.
40. Compile and run.
Is the destructor automatically called when main() ends ?
Are the objects automatically destroyed in reverse order of
instantiation ?
41. Update the Header at the top of the source-file.
SEND TO INSTRUCTOR
- THE SOURCE/HEADER FILES
- THE FILE WITH CAPTURED OUTPUT-SCREEN

You might also like