This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Constructor Method• A constructor is a special method
that assigns initial values to instance variables.1. Constructors must have the same name as the class itself.2. Constructors do not have a return type—not even void.3. Constructors are invoked when an object is created.
• The constructor is automatically called whenever an object is created.
Student’s Default Constructor
Student::Student(void)
{
myFirst = "None";
myLast = “None”;
myCredits = 0;
myGPA = 0.0;
myClassYear = "None";
}
A class normally provides a constructor without formal parameters (arguments) (e.g., Student(void)).
• #include "Student.h"• #include <iostream>• #include <string>• using namespace std;
• This version of the constructor method receives parameters that are used to set the initial instance variable values.
• For class Student: pre-conditions: receives:– String for setting name– int for setting credit hours earned– double for setting GPA
Initializing Constructor
• Add this constructor after the default Student() constructor method.
• The name of this method is also Student
• Pre-conditions: Receives– string for specifying name– int for giving the credit hours earned– double for giving the GPA
Student’s Initializing Constructor
Student::Student(string last, string first,
int credits, double gpa)
{
myFirst = first;
myLast = last;
myCredits = credits;
myGPA = gpa;
if (myCredits >= 92)
myClassYear = "Senior";
else if (myCredits >= 60)
myClassYear = "Junior";
else if (myCredits >= 28)
myClassYear = "Sophomore";
else
myClassYear = "Freshman";
}
#include "Student.h"
#include <iostream>
#include <string>
using namespace std;
int main ()
{
Student sue("Doe","Sue", 16, 4.0);
cout << "Name " << sue.getMyLast()
<< “, “ << sue.getMyFirst << endl;
cout << "Credits "
<< sue.getMyCredits() << endl;
cout << "GPA " << sue.getMyGPA()
<< endl;
cout << "Year "
<< sue.getMyClassYear() << endl;
return 0;
}
Initializing Constructor in Action
• Declare another Student object named Joe.
• Use the initializing constructor to create and initialize instance variables for your new Student with a name of “Joe”, credits of 60 and a gpa of 3.8.
• Use accessors to retrieve and print out all of its instance variable values.
Using the Initializing Constructor
#include "Student.h"
#include <iostream>
#include <string>
using namespace std;
int main ()
{
Student joe(“Blow”,"Joe", 32, 3.92);
cout << "Name " << joe.getMyLast()
<< “, “ << joe.getMyFirst
<< endl;
cout << "Credits "
<< joe.getMyCredits() << endl;
cout << "GPA " << joe.getMyGPA()
<< endl;
cout << "Year "
<< joe.getMyClassYear()
<< endl;
return 0;
}
Initializing Constructor in Action
Printing a Student’s Data
• Wouldn’t it be easier if our main didn’t have to call four “getter” methods and do four cout commands to get and display a Student’s information.
• What could we add to class Student to help save us from doing all these steps?
toString Method
• Write public method inside class Student
• Pre-conditions: None
• Post-conditions: prints out the value of each Student attribute with appropriate messages.
toString Method
string Student::toString(void)
{
stringstream credits, gpa;
credits << myCredits;
gpa << myGPA;
string c, g;
c = credits.str();
g = gpa.str();
string stuString = "Name: " + myLast +
“, “ + myFirst +
"\nCredits: " + c +
"\nGPA: " + g +
"\nClass: " + myClassYear +
"\n";
return stuString;
}
toString in action
#include "Student.h"
#include <iostream>
#include <string>
using namespace std;
int main ()
{
Student joe(“Blow”,"Joe", 32, 3.92);
cout << joe.toString();
}
• Pre-condition: Receives an integer value giving number of credits earned.
• Post-conditions: If given number of credits is > 0, then add that number of credits to the Student’s credits instance variable. Returns updated number of credit hours.