exam 1 class definitions and code questions Flashcards

(51 cards)

1
Q

what is the preferred way to do operator+ overloading?

A

overload the += in the class and + outside of the class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

overload the operator+= for the String class as a method.

A

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
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

overload the operator+ for the String class as a function

A

String operator+(String lhs, const String& rhs){
return lhs += rhs;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

overload the operator== for the String class, include requires and ensures

A

//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];
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

overload the operator< for the String class as a method

A

//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];
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

overload the operator== for the String class as a function. Use parameters char[] and String.

A

bool operator==(const char lhs[], const String& rhs){
return String(lhs) == rhs;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

overload the operator== for the String class as a function. Use parameters char and String.

A

bool operator== (char lhs, const String& rhs){
return String(lhs) == rhs;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

overload the operator<= for the String class as a function. Use parameters String and String.

A

bool operator<=(const String& lhs, const String& rhs){
return !(rhs < lhs);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

overload the operator> for the String class as a function. Use parameters String and String.

A

bool operator>(const String& lhs, const String& rhs){
return rhs < lhs;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

overload the operator>= for the String class as a function. Use parameters String and String.

A

bool operator>=(const String& lhs, const String& rhs){
return !(lhs < rhs);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

overload the operator!= for the String class as a function. Use parameters String and String.

A

bool operator!=(const String& lhs, const String& rhs){
return !(lhs == rhs);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

make a method for the String class called findChar. Use parameters int and char. Include requires and ensures

A

//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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

make a method for the String class called substr. Use parameters int and int. Include requires and ensures

A

//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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what are the private variables of the point class?

A

double x;
double y;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

make a method for the point class called init that has two doubles as parameters.

A

void Point::init (double newX, double newY){
x = newX;
y = newY;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

make a default constructor for the Point class.

A

Point::Point(){
x = 0;
y = 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

make a method for the Point class called setX that has one double as a parameter.

A

void Point::setX(double newX){
x = newX;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

make a method for the Point class called setY that has one double as a parameter.

A

void Point::setY(double newY){
y = newY;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

make a method for the Point class called getX that takes no parameters.

A

double Point::getX(){
return x;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

make a method for the Point class called getY that takes no parameters.

A

double Point::getY(){
return y;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

make a method for the Point class called add that has a Point as a parameter.

A

Point Point::add(const Point& rhs) const{
Point result;
result.x = x + rhs.x;
result.y = y + rhs.y;
return result;
}

22
Q

make a method for the Point class called sub that has a Point as a parameter.

A

Point Point::sub(const Point& rhs) const{
Point result;
result.x = x - rhs.x;
result.y = y - rhs.y;
return result;
}

23
Q

make a constructor for the Point class that takes two doubles as parameters.

A

Point::Point(double newX, double newY){
x = newX;
y = newY;
}

24
Q

overload the + operator for the point class.

A

Point Point::operator+(Point rhs){
Point result;
result.x = x + rhs.x;
result.y = y + rhs.y;
return result;
}

25
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; }
26
overload the << operator for the Point class. Assume it is a friend function.
std::ostream& operator<<(std::ostream& out, Point rhs){ out << "(" << rhs.x << ", " << rhs.y << ")"; return out; }
27
what are the private variables for the Set class?
bool element[SET_DOMAIN], an array of bools, where an element is true if it is in the set and false if not
28
make the default constructor for the Set class.
Set::Set(){ for(int i = 0; i < SET_DOMAIN; i++){ element[i] = false; } }
29
make a constructor for the Set class that takes one int as a parameter.
Set::Set(int input){ Set() if(input >= 0 && input < SET_DOMAIN){ element[input] = true; } }
30
make a constructor for the Set class that takes two ints as parameters.
Set::Set(int input1, int input2){ Set(); if(input1 >= 0 && input1 < SET_DOMAIN){ element[input1] = true; } if(input2 >= 0 && input2 < SET_DOMAIN){ element[input2] = true; } }
31
make a method card for the Set class that returns the cardinality of the set and takes no parameters.
int Set::card() const{ int result = 0; for(int i = 0; i < SET_DOMAIN; i++){ if(element[i]){ ++result; } } return result; }
32
overload operator+ for the Set class that takes a Set object as a parameter. operator+ functions as the union of two sets
Set Set::operator+(const Set& rhs) const{ Set result; for(int i = 0; i < SET_DOMAIN; i++){ result.element[i] = (element[i] || rhs.element[i]); } return result; }
33
overload operator[] for the Set class that takes an int as a parameter.
bool Set::operator[](int position) const{ if(position >= 0 && position < SET_DOMAIN){ return element[position]; } return false; }
34
overload operator* for the Set class that takes a Set as a parameter. operator* functions as the intersection of the two Sets.
Set Set::operator*(const Set& rhs) const{ Set result; for(int i = 0; i < SET_DOMAIN; i++){ result[i] = element[i] && rhs.element[i]; } return result; }
35
overload operator+ for the Set class that takes an int and a Set object as parameters. Assume this is not a method and operator+ has already been overridden as a method for the Set class.
Set operator+ (int lhs, const Set& rhs){ return Set(lhs) + rhs; }
36
overload operator* for the Set class that takes an int and a Set object as parameters. Assume this is not a method and operator* has already been overridden as a method for the Set class.
Set operator* (int lhs, const Set& rhs){ return Set(lhs) * rhs; }
37
overload operator- for the Set class that takes an int and a Set object as parameters. Assume this is not a method and operator- has already been overridden as a method for the Set class.
Set operator- (int lhs, const Set& rhs){ return Set(lhs) - rhs;
38
overload operator== for the Set class that takes a Set object as a parameter.
bool Set::operator==(const Set& rhs) const{ int i = 0; while(element[i] == rhs.element[i] && i < SET_DOMAIN){ ++i; } return element[i] == rhs.element[i]; }
39
overload operator== for the Set class that takes an int and a Set object as parameters. Assume this is not a method and operator== has already been overridden as a method for the Set class.
bool operator== (int lhs, const Set& rhs){ return Set(lhs) == rhs; }
40
overload operator!= for the Set class that takes an Set object and a Set object as parameters. Assume this is not a method and operator== has already been overridden as a method for the Set class.
bool operator!=(const Set& lhs, const Set& rhs){ return !(lhs == rhs); }
41
overload operator<= as a method for the Set class that takes a Set object as a parameter.
bool Set::operator<=(const Set& rhs){ for(int i = 0; i < SET_DOMAIN; i++){ if(element[i] && !rhs.element[i]){ return false; } } return true; }
42
overload operator< for the Set class that takes an Set object and a Set object as parameters. Assume this is not a method and operator== and operator<= have already been overridden as a method for the Set class.
bool operator<(const Set& lhs, const Set& rhs){ return (lhs <= rhs) && (lhs != rhs); }
43
overload operator>=for the Set class that takes an Set object and a Set object as parameters. Assume this is not a method and operator<= has already been overridden as a method for the Set class.
bool operator >=(const Set& lhs, const Set& rhs){ return rhs <= lhs; }
44
overload operator<< for the Set class as a function, not a friend function.
std::ostream& operator<<(std::ostream& out, const Set& rhs){ out << "{ "; bool printComma = false; for(int i = 0; i < SET_DOMAIN; i++){ if(rhs[i]){ if(printComma){ out << ", "; } out << i; printComma = true; } } return out; }
45
what are the private members of the String class?
char str[256]
46
make a default constructor for the String class.
String::String(){ str[0] = 0; }
47
create a constructor for the String class that takes in a char as a parameter.
String::String(char c){ str[0] = c; str[1] = 0; }
48
create a constructor for the String class that takes in a char array as a parameter.
String::String(const char& arr[]){ int i = 0; while(arr[i] != 0){ str[i] = arr[i]; ++i; } }
49
create a method for the String class called length that takes no parameters and returns the length of the string.
int String::length(){ int result = 0; while(str[result] != 0){ ++result; } return result; }
50
overload operator[] for the String class so that it returns the char in str that is in the position of the integer that is entered.
char String::operator[](int i) const{ assert(i >= 0); assert(i < length()); return str[i]; }
51
overload operator[] for the String class so that it returns a char reference of the char in str that is in the position of the integer that is entered.
char& String::operator[](int i){ assert(i >= 0); assert(i < length()); return str[i]; }