exam 1 class definitions and code questions Flashcards
(51 cards)
what is the preferred way to do operator+ overloading?
overload the += in the class and + outside of the class
overload the operator+= for the String class as a method.
String& String::operator+=(const String& rhs){
int offset = length();
int rhsLength = rhs.length();
int i = 0;
while(rhs.str[i] != 0){
if(offset + i >= CAPACITY){
break;
}
str[offset + i] = rhs.str[i];
++i;
}
str[offset + i] = 0;
return *this; //dereferencing
}
overload the operator+ for the String class as a function
String operator+(String lhs, const String& rhs){
return lhs += rhs;
}
overload the operator== for the String class, include requires and ensures
//REQUIRES: 0 <= length() && rhs.length() < CAPACITY
// && i <= length()
// && i <= rhs.length()
//ENSURES: retval == true if str == rhs.str
// && retval == false if not
bool String::operator==(const String& rhs) const{
int i = 0;
while((str[i] != 0) && (str[i] == rhs.str[i]){
++i;
}
return str[i] == rhs.str[i];
}
overload the operator< for the String class as a method
//REQUIRES: 0 <= length() && rhs.length() < CAPACITY
//&& i <= length()
//&& i <= rhs.length()
//ENSURES: retval == true if lhs < rhs
// && retval == false if not
bool String::operator<(const String& rhs) const{
int i = 0;
while((str[i] != 0) && (str[i] == rhs.str[i])){
++i;
}
return str[i] < rhs.str[i];
}
overload the operator== for the String class as a function. Use parameters char[] and String.
bool operator==(const char lhs[], const String& rhs){
return String(lhs) == rhs;
}
overload the operator== for the String class as a function. Use parameters char and String.
bool operator== (char lhs, const String& rhs){
return String(lhs) == rhs;
}
overload the operator<= for the String class as a function. Use parameters String and String.
bool operator<=(const String& lhs, const String& rhs){
return !(rhs < lhs);
}
overload the operator> for the String class as a function. Use parameters String and String.
bool operator>(const String& lhs, const String& rhs){
return rhs < lhs;
}
overload the operator>= for the String class as a function. Use parameters String and String.
bool operator>=(const String& lhs, const String& rhs){
return !(lhs < rhs);
}
overload the operator!= for the String class as a function. Use parameters String and String.
bool operator!=(const String& lhs, const String& rhs){
return !(lhs == rhs);
}
make a method for the String class called findChar. Use parameters int and char. Include requires and ensures
//REQUIRES: 0 <= start < length()
//ENSURES: retval == i if str[i] == toFind
//or retval == -1 if not
int String::findChar(int start, char toFind) const{
int i = 0;
if(start < 0){
start = 0;
}
if(start >= length()){
return -1;
}
i = start;
while(str[i] != 0){
if(str[i] == toFind){
return i;
}
}
return -1;
}
make a method for the String class called substr. Use parameters int and int. Include requires and ensures
//REQUIRES: 0 <= start <= end < length()
//ENSURES: retval == str[start, …, end]
String String::substr(int start, int end) const{
String result;
if(start < 0){
start = 0;
}
if(end >= length()){
end = length() - 1;
}
if(end < start){
return result;
}
for(int i = start: i <= end; i++){
result += str[i];
}
return result;
}
what are the private variables of the point class?
double x;
double y;
make a method for the point class called init that has two doubles as parameters.
void Point::init (double newX, double newY){
x = newX;
y = newY;
}
make a default constructor for the Point class.
Point::Point(){
x = 0;
y = 0;
}
make a method for the Point class called setX that has one double as a parameter.
void Point::setX(double newX){
x = newX;
}
make a method for the Point class called setY that has one double as a parameter.
void Point::setY(double newY){
y = newY;
}
make a method for the Point class called getX that takes no parameters.
double Point::getX(){
return x;
}
make a method for the Point class called getY that takes no parameters.
double Point::getY(){
return y;
}
make a method for the Point class called add that has a Point as a parameter.
Point Point::add(const Point& rhs) const{
Point result;
result.x = x + rhs.x;
result.y = y + rhs.y;
return result;
}
make a method for the Point class called sub that has a Point as a parameter.
Point Point::sub(const Point& rhs) const{
Point result;
result.x = x - rhs.x;
result.y = y - rhs.y;
return result;
}
make a constructor for the Point class that takes two doubles as parameters.
Point::Point(double newX, double newY){
x = newX;
y = newY;
}
overload the + operator for the point class.
Point Point::operator+(Point rhs){
Point result;
result.x = x + rhs.x;
result.y = y + rhs.y;
return result;
}