Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Complete student.cpp and studentRoll.cpp so that they run completely without any memory leaks. student.cpp #include student.h #include #include Student::Student(const char * const name, int perm)
Complete student.cpp and studentRoll.cpp so that they run completely without any memory leaks.
student.cpp
#include "student.h" | |
#include | |
#include | |
Student::Student(const char * const name, int perm) { | |
this->setName("another stub"); | |
} | |
int Student::getPerm() const { | |
return -42; | |
} | |
const char * const Student::getName() const { | |
return "stub"; | |
} | |
void Student::setPerm(const int permNumber) { | |
} | |
void Student::setName(const char * const name) { | |
this->name = new char[strlen("stub")+1]; | |
strcpy(this->name,"stub"); | |
} | |
Student::Student(const Student &orig) { | |
this->setName("yet another stub"); | |
this->setPerm(-42); | |
} | |
Student::~Student() { | |
} | |
Student & Student::operator=(const Student &right) { | |
// The next two lines are standard, and you should keep them. | |
// They avoid problems with self-assignment where you might free up | |
// memory before you copy from it. (e.g. x = x) | |
if (&right == this) | |
return (*this); | |
// TODO... Here is where there is code missing that you need to | |
// fill in... | |
// KEEP THE CODE BELOW THIS LINE | |
// Overloaded = should end with this line, despite what the textbook says. | |
return (*this); | |
} | |
std::string Student::toString() const { | |
return "tostring stub"; | |
} |
studentRoll.cpp
#include | |
#include "studentRoll.h" | |
StudentRoll::StudentRoll() { | |
head = tail = NULL; | |
} | |
void StudentRoll::insertAtTail(const Student &s) { | |
// STUB | |
} | |
std::string StudentRoll::toString() const { | |
return "stub"; | |
} | |
StudentRoll::StudentRoll(const StudentRoll &orig) { | |
// STUB | |
head = tail = NULL; | |
} | |
StudentRoll::~StudentRoll() { | |
// STUB | |
} | |
StudentRoll & StudentRoll::operator =(const StudentRoll &right ) { | |
// The next two lines are standard, and you should keep them. | |
// They avoid problems with self-assignment where you might free up | |
// memory before you copy from it. (e.g. x = x) | |
if (&right == this) | |
return (*this); | |
// TODO... Here is where there is code missing that you need to | |
// fill in... | |
// KEEP THE CODE BELOW THIS LINE | |
// Overloaded = should end with this line, despite what the textbook says. | |
return (*this); | |
} | |
Tests that implement student.cpp and studentRoll.cpp
testStudent02.cpp
#include "student.h" | |
#include | |
#include "tddFuncs.h" | |
using namespace std; | |
int main() { | |
cout << "Running tests from: " << __FILE__ << endl; | |
const char * const origName="Phill Conrad"; | |
Student s(origName,1234567); | |
ASSERT_EQUALS(origName,s.getName()); | |
ASSERT_EQUALS(1234567,s.getPerm()); | |
const char * const name1="Chris Gaucho"; | |
const char * const name2="Adam Twelve"; | |
const char * const name3="Betty Boop"; | |
Student s1(s); | |
Student s2(s); | |
Student s3(s); | |
s.setName(name1); | |
ASSERT_EQUALS(name1,s.getName()); | |
s2.setName(name2); | |
ASSERT_EQUALS(name2,s2.getName()); | |
s3.setName(name3); | |
ASSERT_EQUALS(name3,s3.getName()); | |
ASSERT_EQUALS(origName,s1.getName()); | |
return 0; | |
} |
testStudentRoll02.cpp
#include "student.h" | |
#include "studentRoll.h" | |
#include | |
#include "tddFuncs.h" | |
using namespace std; | |
int main() { | |
cout << "Running tests from: " << __FILE__ << endl; | |
const char * const name1="One One"; | |
const char * const name2="Two Two"; | |
const char * const name3="Tre Tre"; | |
Student s1(name1,1111111); | |
Student s2(name2,2222222); | |
Student s3(name3,3333333); | |
StudentRoll sr0; | |
StudentRoll sr1(sr0); | |
sr1.insertAtTail(s1); | |
StudentRoll sr2(sr1); | |
sr2.insertAtTail(s2); | |
StudentRoll sr3; | |
sr3.insertAtTail(s1); | |
sr3 = sr2; | |
sr3.insertAtTail(s3); | |
ASSERT_EQUALS("[]",sr0.toString()); | |
ASSERT_EQUALS("[[One One,1111111]]",sr1.toString()); | |
ASSERT_EQUALS("[[One One,1111111],[Two Two,2222222]]",sr2.toString()); | |
ASSERT_EQUALS("[[One One,1111111],[Two Two,2222222],[Tre Tre,3333333]]",sr3.toString()); | |
return 0; | |
} |
EDIT: tddFuncs.h
#ifndef TDDFUNCS_H | |
#define TDDFUNCS_H | |
#include | |
#include | |
std::string convertLineNumber(int lineNumber); | |
// template requires == and << | |
template | |
T actual, | |
std::string message, | |
int lineNumber) { | |
std::string line=convertLineNumber(lineNumber); | |
if (expected==actual) { | |
std::cout << "PASSED: " << message << line << std::endl;; | |
} else { | |
std::cout << " FAILED: " << message << line << std::endl | |
<< " Expected: " << expected << " Actual: " << actual << std::endl; | |
} | |
} | |
// specialized because char * doesn't work properly on == | |
void assertEquals(const char * const expected, | |
const char * const actual, | |
std::string message, | |
int lineNumber=-1); | |
// specialized for the same reason, and because expected is often a string literal | |
void assertEquals(const char * const expected, | |
std::string actual, | |
std::string message, | |
int lineNumber=-1); | |
#define ASSERT_EQUALS(expected,actual) \ | |
assertEquals(expected,actual,#actual " at " __FILE__ , __LINE__ ) | |
void assertTrue(bool expression, std::string message=""); | |
#define ASSERT_TRUE(expression) assertTrue(expression,#expression) | |
#endif // TDDFUNCS_H |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started