C++
When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong
Requirements:
============================================================================
string.hpp:
#ifndef CS23001_STRING_INTERFACE_HPP #define CS23001_STRING_INTERFACE_HPP #include <iostream> //////////////////////////////////////////////////// // CLASS INV: str[length()] == 0 && // length() == capacity() && // capacity() == stringSize - 1 // class String { public: String (); //Empty string String (char); //String('x') String (const char[]); //String("abc") String (const String&); //Copy Constructor ~String (); //Destructor void swap (String&); //Constant time swap String& operator= (String); //Assignment Copy char& operator[] (int); //Accessor/Modifier char operator[] (int) const; //Accessor int capacity () const; //Max chars that can be stored (not including null) int length () const; //Actual number of chars in string String operator+ (const String&) const; String& operator+= (const String&); bool operator== (const String&) const; bool operator< (const String&) const; String substr (int, int) const; //The sub-string from staring position to ending position int findch (int, char) const; //Find location of charater starting at a position int findstr (int, const String&) const; //Find location of str starting at a position void test_String (); friend std::ostream& operator<<(std::ostream&, const String&); friend std::istream& operator>>(std::istream&, String&); private: String (int n ); //String(10) - capacity 10, empty string String (int , const char []); //String(10, "abc") - capacity 10 with "abc" void resetCapacity (int); //Resets capacity to N, keeps string intact char *str; //Pointer to char[] int stringSize; //Size includes NULL terminator }; String operator+ (const char[], const String&); String operator+ (char, const String&); bool operator== (const char[], const String&); bool operator== (char, const String&); bool operator< (const char[], const String&); bool operator< (char, const String&); bool operator<= (const String&, const String&); bool operator!= (const String&, const String&); bool operator>= (const String&, const String&); bool operator> (const String&, const String&); #endif
============================================================================
string.cpp:
#include <iostream> #include "string.hpp" #include <cassert> String::String() { // default constructor - empty string stringSize = 1; str = new char [stringSize]; str[0] = 0; } String::String(char ch) { stringSize = 2; str = new char [stringSize]; str[0] = ch; str[1] = '\0'; } //REQUIRES: str.length() < capacity() //String a("abc") //Takes character array and turns into string array String::String(const char X[]) { int i = 0; while (X[i] != '\0') ++i; stringSize = i; str = new char [stringSize + 1]; for(int j = 0; j < capacity(); ++j) str[i] = X[i]; } String::String(const String& rhs) { //copy constructor stringSize = rhs.stringSize; str = new char [stringSize]; for(int i = 0; i < capacity(); ++i) str[i] = rhs.str[i]; } String::~String() { //destructor delete[] str; } void String::swap (String& rhs) { //Constant time swap char * temporary = str; str = rhs.str; rhs.str = temporary; int hold = stringSize; stringSize = rhs.stringSize; rhs.stringSize = hold; } String& String::operator= ( String rhs) { // Assignment copy if (str == rhs.str) return *this; //check to see if they are already pointing to the same address delete [] str; stringSize = rhs.stringSize; str = new char [stringSize]; for (int i = 0; i < capacity(); ++i) str[i] = rhs.str[i]; return *this; } //REQUIRES: 0 <= i < length() // operator[] const --- allows access to const objects char String::operator[](int i) const { assert( (i > 0) && (i < length()) ); return str[i]; } //REQUIRES: 0 <= i < length() // operator[] --- allows access / modification to non-const objects char& String::operator[] (int i) { assert( (i >= 0) && (i < length() ) ); return str[i]; } int String::capacity() const { //capacity = stringSize -1; return (stringSize - 1); } //ENSURES: Retval == i where str[i] = 0 int String::length() const { int result = 0; while (str[result] != '\0') ++result; return result; } // retval == "xyzabc" where "xyx" + "abc" String String::operator+(const String& rhs) const { String result; int offset= length(); int i=0; while(rhs.str[i]!= '\0'){ result.str[offset + i]= rhs.str[i]; ++i; if(offset + i == capacity()) break; } result.str[offset + i]=0; return result; } String operator+(char lhs, const String& rhs) { return String(lhs) + rhs; } String operator+(const char lhs[], const String& rhs) { return String(lhs) + rhs; } String& String::operator+=(const String& rhs) { *this = operator+(rhs); return *this; } 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]; } bool operator==(char lhs, const String& rhs) { return String(lhs) == rhs; } bool operator==(char lhs[], const String& rhs) { return String(lhs) == rhs; } bool String::operator<(const String& rhs) const { int i = 0; while ((str[i] != 0) && (rhs.str[i] != 0) && (str[i] == rhs.str[i])) ++i; return str[i] < rhs.str[i]; } bool operator<(char lhs, const String& rhs) { return String(lhs) < rhs; } bool operator<(const char lhs[], const String& rhs) { return String(lhs) < rhs; } bool operator!=(const String& lhs, const String& rhs) { return !(lhs == rhs) || (lhs == rhs); } bool operator<=(const String& lhs, const String& rhs) { return (lhs < rhs) || (lhs == rhs); } bool operator>(const String& lhs, const String& rhs) { return (rhs < lhs); } bool operator>=(const String& lhs, const String& rhs) { return !(lhs < rhs); } std::ostream& operator<<(std::ostream& out, const String& rhs) { out << rhs.str; return out; } std::istream& operator>>(std::istream& in, String& rhs) { char placehold[540000]; in >> placehold; rhs = String(placehold); return in; } //REQUIRES: 0 <= start < length() //ENSURES: retval == i where str[i] == ch && i >= start // or retval == -1 if ch != s[start.length()-1] int String::findch(int start, char ch) const { if ( (start < 0) || (start >= length()) ) return -1; int i = start; while (str[i] != 0) { if (str[i] == ch) return i; ++i; } return -1; } /* int String::findch(char ch) const { return findch(0, ch); } */ int String::findstr(int pos, const String& rhs) const { int i = pos; if ((pos < 0) || (pos >= length() - rhs.length())) return -1; if (length() < rhs.length()) return -1; while ((str[pos] != 0) && (rhs.length() + pos - 1 <= length())) { if (rhs == substr(i, i + rhs.length() - 1)) return pos; ++i; } return -1; } //REQUIRES: 0 <= start <= end < length() //ENSURES: retval == s[start, ..., end] String String::substr(int start, int end) const { if (start < 0) return String(); if (start > end) return String(); if (end >= length()) return String(); String result; int i = start; while (i <= end) { result += str[i]; ++i; } return result; } String::String (int n) { //String(10) - capacity 10, empty string stringSize = n; str = new char [stringSize]; str[0] = 0; } String::String (int n, const char ch[]) { //String(10, "abc") - capacity 10 with "abc" stringSize = n; str = new char [n]; for (int i = 0; i < n; ++i) str[i] = ch[i]; }; void String::resetCapacity (int n ) { //Resets capacity to N, keeps string intact int smaller = stringSize; if (smaller > n) smaller = n; stringSize = n; char * tmp = new char [stringSize]; for (int i = 0; i < smaller; ++i) tmp[i] = str[i]; delete [] str; str = tmp; } void String::test_String() { String testing(5); assert(testing.length() == 0); assert(testing.capacity() == 5); String test(15); assert(test.length() == 0); assert(test.capacity() == 15); String tes(74); assert(test.length() == 0); assert(test.capacity() == 74); String CharArray(10, "abc"); assert(CharArray.length() == 3); assert(CharArray.capacity() == 10); }
============================================================================
Char constructor tests .cpp :
#include "string.hpp" #include <cassert> #include <iostream> //=========================================================================== int main () { { //------------------------------------------------------ // SETUP FIXTURE // TEST String s = "A"; // VERIFY assert(s == "A"); } { //------------------------------------------------------ // SETUP FIXTURE // TEST String s = "B"; std::cout<< s <<std::endl; ; // VERIFY assert(s == "B"); } { //------------------------------------------------------ // SETUP FIXTURE // TEST String s = "n"; // VERIFY assert(s == "n"); } std::cout << "Done testing Charactor Constructor." << std::endl; }
The constructor code has bugs. The fixed code for the problematic function is given below followed by the output snapshot. The buggy lines are commented out and the corrected lines follow it.
//REQUIRES: str.length() < capacity()
//String a("abc")
//Takes character array and turns into string array
String::String(const char X[]) {
int i = 0;
while (X[i] != '\0') ++i;
/* BUG:
stringSize = i;
str = new char [stringSize + 1];
for(int j = 0; j < capacity(); ++j)
str[i] = X[i];
*/
// NEW CODE START
stringSize = i + 1; // i is the number of characters in the string,
stringSize include the null
str = new char [stringSize];
for(int j = 0; j < capacity(); ++j)
str[j] = X[j];
str[capacity()] = 0; // null terminate
//NEW CODE END
}
C++ When running my tests for my char constructor the assertion is coming back false and...
HELP PLEASE ANSWER 3(c). Consider the following function object: #include <string> class CustomCompare public: bool operator (const atd:istringk 1hs, const std::string& rhs) if (1hs. length) 0 && rhs.lengthO 0) return false; int 1 lhs. length )-1; intr rhs. length)-1; return (ro&&10) I Write one C++ statement that defines a variable myCustomPQ, where myCustomPQ is a priority queue storing strings and using CustomCompare.
Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...
SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...
//codes in DynamicString.cpp #include "DynamicString.h" int DynamicString::myStringLen(const char* str){ //TODO::Implement me return -1; } DynamicString::DynamicString(){ //TODO::1::Implement me } DynamicString::DynamicString(const char* str){ //TODO::1::Implement me } int DynamicString::len() const{ //TODO::1::Implement me return -1; } const char* DynamicString::c_str() const{ //TODO::1::Implement me return nullptr; } char& DynamicString::char_at(int position){ //TODO::1::Implement me char* a = new char('a'); return *a; } char DynamicString::char_at(int position) const{ //TODO::1::Implement me return 'a'; } char& DynamicString::operator[](int position){ //TODO::1::Implement me char* a = new char('a'); return *a; } char DynamicString::operator[](int position) const{...
Please Implement ParkingLot.cpp below based off the completed Automobile Class and ClaimCheck class down below. Automobile.hpp #pragma once #include <iostream> #include <string> class Automobile { friend bool operator==( const Automobile& lhs, const Automobile& rhs ); friend std::ostream & operator<<( std::ostream& stream, const Automobile& vehicle ); private: std::string color_; std::string brand_; std::string model_; std::string plateNumber_; public: Automobile( const std::string & color, const std::string & brand, const std::string & model, const std::string & plateNumber ); }; bool operator!=( const Automobile& lhs, const...
C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Submit your completed source (.cpp) file. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator:...
Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp, and a test file to test the functionality of the class. Hint: read all the explanations in the header with attention. MyArray.h : #ifndef MYARRAY_H #define MYARRAY_H #include <iostream> using namespace std; class MyArray { friend ostream& operator<<( ostream & output, const MyArray & rhs); // to output the values of the array friend istream& operator>>( istream & input, MyArray & rhs); // to...
A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...
This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a function and three overloaded operators. You don't need to change anything in the Multiplex.h file or the main.cpp, though if you want to change the names of the movies or concession stands set up in main, that's fine. Project Description: A Multiplex is a complex with multiple movie theater screens and a variety of concession stands. It includes two vectors: screenings holds pointers...
C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...