Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need the .cpp for the following functions (code needs to be in c++): DNAString.h: #include class DNAStrand { public: /** * @brief DNAStrand Builds a

Need the .cpp for the following functions (code needs to be in c++):

DNAString.h:

#include  
class DNAStrand {
public: 
 /**
 * @brief DNAStrand Builds a DNAStrand using the given C-String
 * @param startingString A null terminated char array
 *
 * Throws a logic_error exception if the startingString contains anything but A, C, G, or T
 */
 DNAStrand(const char* startingString); 
 /**
 * @brief DNAStrand Copy Ctor
 * @param other DNAStrand to copy
 */
 DNAStrand(const DNAStrand& other); 
 /**
 * @brief operator = Assignment Operator for DNAStrand
 * @param other DNAStrand to duplicate
 * @return Reference to the string that was copied into
 */
 DNAStrand& operator=(const DNAStrand& other); 
 /**
 * @brief Destructor
 */
 ~DNAStrand(); 
 /**
 * @brief operator == Check if two DNAStrands have same contents
 * @param other DNAStrand to compate to
 * @return true if identical, false if not
 */
 bool operator==(const DNAStrand& other) const; 
 /**
 * @brief operator [] Access individual nucleotide from DNAStrand
 * @param index location to access
 * @return char value at index. Not returned by referece - no write access
 *
 * Throws an out_of_range exception if the index is less than 0 or too large
 */
 char operator[](int index) const; 
 /**
 * @brief operator + Combine two DNAStrands
 * @param other DNAStrand to merge with this
 * @return DNAStrand with contents of current DNAStrand followed by contents of other
 */
 DNAStrand operator+(const DNAStrand& other) const; 
 //Declare as friend... declared after class
 friend std::ostream& operator 
 /**
 * @brief getLength gets length of string
 * @return length
 */
 int getLength() const; 
 /**
 * @brief getComplement gets Complement of the DNAStrand
 * @return DNAStrand that is complement of this
 *
 * The complement is the DNAStrand that pairs with this one to make a double strand.
 * Any A's in originals must become T's in complement and vice verse
 * C's become G's and vice verse.
 *
 * Original: ACCTG
 * Complement: TGGAC
 */
 DNAStrand getComplement() const; 
 /**
 * @brief reverse gets reversed version of this DNAStrand
 * @return Reversed version: AAACGT become TGCAAA
 */
 DNAStrand reverse() const; 
 /**
 * @brief substr Gets a DNAStrand that contains the requested portion of a DNAStrand
 * @param start index of first base to grab
 * @param length number of bases to include
 * @return DNAstrand
 *
 * Throws an out_of_range exception if the index or length are less than 0
 * or start + length end up past the end of the array
 */
 DNAStrand substr(int start, int length) const; 
 /**
 * @brief findFirstCopyOf Finds the location first copy of given DNAStrand within this string
 * @param other DNAStrand to find within this
 * @return location of first match, or -1 if not found
 */
 int findFirstCopyOf(const DNAStrand& other) const; 
private:
 /**
 * @brief DNAStrand Helper ctor - use in other functions to make a DNAStrand of given size that
 * then can be modified
 * @param length Size DNA String to create
 */
 DNAStrand(int length); 
 char* bases = nullptr; ///Tracks array contining a char array
 int length = 0; ///Length of the char array and DNAStrand
}; 
/**
 * @brief operator  
 * @param dest stream to output to
 * @param source DNAStrand to output
 * @return dest stream returned by reference for chaining of  
 */
std::ostream& operator 
image text in transcribed

image text in transcribed

DNAStrand substr(int start, int length) const

Get a new DNAStrand that contains the indicated portion of this DNAStrand. You will probably need/want the DNAString(int length) constructor as a helper for this. This should throw an out_of_range exception if the index or length are negative, or if the start + length would go past the end of the strand.

Function Tips: Design Note: Most of the functions we will write are non-modifying. Once we make a DNAStrand, there is no way to change it other than the assignment operator. If we want to do something like add two strands, the originals will remain unmodified and we will end up with a new strand that represents them stuck together. Objects that can't be changed are known as immutable. Immutable objects are safer to pass around with pointers or references (since they are essentially always const). They also write parallel code (no worries that one thread will be working with an object while another modifies it). The downside of immutable objects is we end up making lots of copies since every "change" really just makes a new object. If we wanted the class to be completely immutable, we would declare an assignment operator in the.h but not write it in the .cpp. That would prevent the use of the = operator which would mean that we could not use it to change an existing object. Remember: you may NOT use std::string in this project. You can use functions from the cstring library. DNAStrand(const char* startingString) - Our basic constructor - it takes a char array (C-String) and sets the length and creates an array that holds the bases listed in the C-String. You can use strlen from to figure out the length of the parameter, or write a loop that looks for the \O char. (See week 8 of 161 for cstring material). int getLength() const - Just return length. bool operator==(const DNAStrand& other) const - Checks to see if the strands contain an identical list of bases. (i.e. "ATTAG" and "ATTAG"). Needed by the first test and most others). Hints: You can't just compare the arrays themselves with ==, that will just see if the two DNAStrands are pointing to the same memory. You have to compare the individual items in the arrays. What is an easy way to tell that two strands can't possibly be identical? DNAStrand() - Destructor. Not tested directly - but every test will leak memory without this. Verify it works by running your program with drmemory. DNAStrand(const DNAStrand& other) and DNAStrand& operator (const DNAStrand& other) - Copy constructor and assignment operator. Tested in two unit tests. These are another big area for memory issues - test with DrMemory! char operator[(int index) const - Gets a particular base. Return type is char instead of char& so that we can only read the bases and can't modify them. (Using something like strand[2] = 'T' won't work unless [] returned a char&). This needs to throw an out_of_range exception if given a bad index DNAStrand(int length) - This private constructor builds a DNAStrand with the given length and should allocate an array big enough to hold that many chars (and not initialize them or init to all o). Having this constructor will make other functions easier to write as they will be able to do something like: DNAStrand temp (20); // I know it should be 20 long //use loop to set temp's bases return temp; It is private as we don't want other code using this special helper constructor. There are no tests for this (since it is private). But you will probably want to use it as part of any function that returns a DNAStrand. operator +, and getComplement - Check the.h for documentation. Note that all of these return a new object that has the desired DNAStrand. Don't try to modify the current strand. Each has their own test. You will probably need/want the DNAString(int length) constructor as a helper for these. operator to figure out the length of the parameter, or write a loop that looks for the \O char. (See week 8 of 161 for cstring material). int getLength() const - Just return length. bool operator==(const DNAStrand& other) const - Checks to see if the strands contain an identical list of bases. (i.e. "ATTAG" and "ATTAG"). Needed by the first test and most others). Hints: You can't just compare the arrays themselves with ==, that will just see if the two DNAStrands are pointing to the same memory. You have to compare the individual items in the arrays. What is an easy way to tell that two strands can't possibly be identical? DNAStrand() - Destructor. Not tested directly - but every test will leak memory without this. Verify it works by running your program with drmemory. DNAStrand(const DNAStrand& other) and DNAStrand& operator (const DNAStrand& other) - Copy constructor and assignment operator. Tested in two unit tests. These are another big area for memory issues - test with DrMemory! char operator[(int index) const - Gets a particular base. Return type is char instead of char& so that we can only read the bases and can't modify them. (Using something like strand[2] = 'T' won't work unless [] returned a char&). This needs to throw an out_of_range exception if given a bad index DNAStrand(int length) - This private constructor builds a DNAStrand with the given length and should allocate an array big enough to hold that many chars (and not initialize them or init to all o). Having this constructor will make other functions easier to write as they will be able to do something like: DNAStrand temp (20); // I know it should be 20 long //use loop to set temp's bases return temp; It is private as we don't want other code using this special helper constructor. There are no tests for this (since it is private). But you will probably want to use it as part of any function that returns a DNAStrand. operator +, and getComplement - Check the.h for documentation. Note that all of these return a new object that has the desired DNAStrand. Don't try to modify the current strand. Each has their own test. You will probably need/want the DNAString(int length) constructor as a helper for these. operator

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions