Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment 6 - A Triple String Class (C++ Programming) Make sure you have read a

ID: 3796506 • Letter: A

Question

Assignment 6 - A Triple String Class (C++ Programming)

Make sure you have read and understood

both modules A and B this week, and

module 2R - Lab Homework Requirements

A Simple Class: TripleString

Understand the Application

The assignment is to first create a class called TripleString. TripleString will consist of three private member strings as its basic data. There will be few instance methods to support that data.

Once defined, we will use it to instantiate TripleString objects that can be used in our main() method. In a later assignment, the TripleString class will help us create a more involved application.

TripleString will contain three private member strings as its main data: string1, string2, and string3. We will also add a few public static member such as const int MAX_LEN and MIN_LEN. These represents the maximum and minimum length that our class will allow any of its strings to be set to. We can use these static members in the TripleString method whose job it is to test for valid strings (see below).

The Program Spec

Class TripleString Spec

Private Class Instance Members:

string string1

string string2

string string3

All legal strings should be between 1 and 50 characters.

As stated in the modules, we never want to see a literal in our methods. So the class should have static members to hold values for the limits described above, as well as default values for any field that is construct-ed using illegal arguments from the client. These are put in the public static section, but their initialization belongs in a different location. See modules for details.

Public Class Static Constants (declare to be const):

MIN_LEN = 1

MAX_LEN = 50

DEFAULT_STRING = " (undefined) "

Public Instance Methods

Default Constructor

TripleString() -- a default constructor that initializes all members to DEFAULT_STRING.

Paramteter-Taking Constructor

TripleString(string str1, string str2, string str3) -- a constructor that initializes all members according to the passed parameters. It has to to be sure each string satisfies the class requirement for a member string. It does this, as in the modules, by calling the mutators and taking possible action if a return value is false. If any passed parameter does not pass the test, a default string should be stored in that member.

Mutators/Accessor

set()s and get()s for these members. Mutators in our course are named using the convention as follows: setString1( ... ), setString3( ... ), etc. Likewise with accessors: getString2(). We need an accessor and mutator for each individual string member, so three pairs of methods in this category. Mutators make use of the private helper method validString(). When a mutator detects an invalid string, no action should be taken. The mutator returns false and the existing String stored in that member prior to the call remains in that member, not a new default string.

string toString() - a method that returns a string which contains all the information (three strings) of the TripleString object. This string can be in any format as long as it is understandable and clearly formatted.

Private Static Helper Methods

bool validString( string str ) -- a helper function that the mutators can use to determine whether a string is legal. This method returns true if the string's length is between MIN_LEN and MAX_LEN (inclusive). It returns false, otherwise.

Where it All Goes

There are now a variety of program elements, so let's review the order in which things appear in your .cpp file:

includes and namespace

class prototype(s)

static member initialization

global-scope method prototype(s) [You may not need them for this assignment.]

main() definition

global-scope method definition( ) [You may not need them for this assignment.]

class method definition(s)

In other words, Foothill.cpp will look like this:

#include <iostream>

#include <string>

#include <sstream>

using namespace std;

class TripleString

{

// prototype

};

// define static constants

// client ---------------------------------------------------------------------

int main()

{

// main client "driver"

}

// TripleString class methods

// constructors

TripleString::TripleString()

{

// constructor defintion

}

TripleString::TripleString(string string1, string string2, string string3)

{

// constructor definition

}

// mutators and other class definitions..

As you see, TripleString class methods are defined after, not within, the TripleString class prototype. Those methods are also defined after the main() method.

The Foothill main()

Instantiate four or more TripleString objects, some of them using the default constructor, some using the constructor that takes parameters.

Immediately display all objects.

Mutate one or more members of every object.

Display all objects a second time.

Do two explicit mutator tests. For each, call a mutator in an if/else statement which prints one message if the call is successful and a different message if the call fails.

Make two accessor calls to demonstrate that they work.

More:

Be sure that all output is descriptive. Use labels to differentiate your output sections and full sentences in your mutator/accessor tests.

No user input should be done in this program.

OUTPUT should be:

TripleStrings after instantiation ----------
(undefined), (undefined), (undefined)
(undefined), (undefined), (undefined)
hello, Foothill, World
1, 2, 3

TripleStrings after changes ----------
(undefined), (undefined), (undefined)
string one, another string, the third string
Pickles, Foothill, World
Foothill College, 2, 3

Mutator Tests ----------

Correctly rejected blank string

Accessor Tests ----------
TripleString String 3: World
TripleString String 2: 2

Press any key to continue . . .

Explanation / Answer

#include <iostream>
#include <string.h>
#include <sstream>
using namespace std;
class TripleString
{
private:
string string1;
string string2;
string string3;
bool validString(string str){
int length = str.size();
if(length>=MIN_LEN && length<=MAX_LEN){
return true;
}
else
return false;
};
public:
static int MAX_LEN;
static int MIN_LEN;
static string DEFAULT_STRING;
// declare constructors
TripleString();
TripleString(string string1, string string2, string string3);
// declare accessors
string getString1();
string getString2();
string getString3();
// declare mutators
bool setString1(string string1);
bool setString2(string string2);
bool setString3(string string3);
string toString();
  
};
// define static constants
int TripleString::MAX_LEN = 50;
int TripleString::MIN_LEN = 1;
string TripleString::DEFAULT_STRING = "(undefined)";
// client ---------------------------------------------------------------------
int main()
{
// Object using default constructor
cout<< "TripleStrings after instantiation ----------"<< std::endl;
TripleString T1;
std::cout << T1.toString() << std::endl;
TripleString T2("","","");
std::cout << T2.toString() << std::endl;
TripleString T3("Hello","FootHill","World");
std::cout << T3.toString() << std::endl;
TripleString T4("1","2","3");
std::cout << T4.getString1()<<", "<< T4.getString2()<<", "<<T4.getString3() << std::endl;
  
cout<< "TripleStrings after changes ----------"<< std::endl;
T2.setString1("string one");
T2.setString2("another string");
T2.setString3("third string");
std::cout << T1.toString() << std::endl;
std::cout << T2.toString() << std::endl;
std::cout << T3.toString() << std::endl;
std::cout << T4.getString1()<<", "<< T4.getString2()<<", "<<T4.getString3() << std::endl;

cout<< "Mutator Tests ----------"<< std::endl;
if(T3.setString1("")){
std::cout << T3.toString() << std::endl;
} else{
cout<< "Correctly rejected blank string"<< std::endl;
}
  
cout<< "Accessors Tests ----------"<< std::endl;
std::cout << "TripleString object 3 String 3:" << T3.getString3() << std::endl;
std::cout <<"TripleString object 2 String 2:" << T2.getString2() << std::endl;
  
  
return 0;
}
// TripleString class methods
// constructors
TripleString::TripleString()
{
// constructor defintion
string1 = DEFAULT_STRING;
string2 = DEFAULT_STRING;
string3 = DEFAULT_STRING;
}
TripleString::TripleString(string str1, string str2, string str3)
{
// constructor definition
if(validString(str1)){
string1 = str1;
}
else{
string1=DEFAULT_STRING;
}   
if(validString(str2)){
string2 = str2;
}
else{
string2=DEFAULT_STRING;
}
if(validString(str3)){
string3 = str3;
}
else{
string3=DEFAULT_STRING;
}
}

// Mutators and Accessors definition
bool TripleString::setString1(string str1){
if(validString(str1)) {
string1 = str1;
return true;
}else{
return false;
}
}
bool TripleString::setString2(string str){
if(validString(str)) {
string2 = str;
return true;
}else{
return false;
}
}
bool TripleString::setString3(string str){
if(validString(str)) {
string3 = str;
return true;
}else{
return false;
}
}
// Accessors definition
string TripleString::getString1(){
return string1;
}
string TripleString::getString2(){
return string2;
}
string TripleString::getString3(){
return string3;
}
string TripleString::toString(){
return string1+ ", "+string2+", "+string3;
}