SlideShare a Scribd company logo
1 of 9
Download to read offline
I need help designing the following program. This EXACT problem I don't think was posted
previously.
/**
* Driver File
* File Name: LabProj6Driver.cpp
*
* Description: This program demonstrates a basic String class that implements
* dynamic allocation and operator overloading.
*
*/
#include <iostream>
#include "mystring2.h"
using namespace std;
/************************ Function Prototypes ************************/
/*
* Function: PrintString
* Usage: PrintString(str);
*
* Prints out the value and length of the String object passed to it.
*/
void PrintString(const char *label,
const String &str); // overloaded ostream operator << is used in the definition.
/*************************** Main Program **************************/
int main()
{
String str1, str2("init2"), str3 = "init3"; // Some String objects. Using constructor for copy
char s1[100], s2[100], s3[100]; // Some character strings.
// Print out their initial values...
cout << "Initial values:" << endl;
PrintString("str1", str1);
PrintString("str2", str2);
PrintString("str3", str3);
// Store some values in them...
cout << "nEnter a value for str1 (no spaces): ";
cin >> s1;
str1 = s1;
cout << "nEnter a value for str2 (no spaces): ";
cin >> s2;
str2 = s2;
cout << "nEnter a value for str3 (no spaces): ";
cin >> s3;
str3 = s3;
cout << "nAfter assignments..." << endl;
PrintString("str1", str1);
PrintString("str2", str2);
PrintString("str3", str3);
// Access some elements...
int i;
cout << "nEnter which element of str1 to display: ";
cin >> i;
cout << "Element #" << i << " of str1 is '" << str1[i]
<< "'" << endl;
cout << "nEnter which element of str2 to display: ";
cin >> i;
cout << "Element #" << i << " of str2 is '" << str2[i]
<< "'" << endl;
cout << "nEnter which element of str3 to display: ";
cin >> i;
cout << "Element #" << i << " of str3 is '" << str3[i]
<< "'" << endl;
// Append some strings...
cout << "nEnter a value to append to str1 (no spaces): ";
cin >> s1;
// str1.append(s1); // Actually, the cstring is converted to String object here by the constructor
str1 += s1; // same as above
cout << "nEnter a value to append to str2 (no spaces): ";
cin >> s2;
str2 += s2;
cout << "nEnter a value to append to str3 (no spaces): ";
cin >> s3;
str3 += s3;
cout << "nAfter appending..." << endl;
PrintString("str1", str1);
PrintString("str2", str2);
PrintString("str3", str3);
// Compare some strings...
cout << "nComparing str1 and str2..." << endl;
cout << """;
cout<< str1; // test the overloading of ostream operator <<
cout << "" is ";
if (str1 < str2) { // test the overloading of comparison operator <
cout << "less than";
} else if (str1 > str2) {
cout << "greater than";
} else {
cout << "equal to";
}
cout << " "";
cout << str2;
cout << """ << endl;
cout << "ntest the = operator, after str1 = str2; "<< endl;
str1 = str2;
PrintString("str1", str1);
PrintString("str2", str2);
str1 += s1;
cout << "nAfter str1 = str1 + s1: "<< endl;
PrintString("str1", str1);
PrintString("str2", str2);
String str4(str3);
cout << "ntest the copy constructor, after str4(str3);"<< endl;
PrintString("str3", str3);
PrintString("str4", str4);
cout << "nafter appending str3 by str2" << endl;
str3 += str2;
PrintString("str3", str3);
PrintString("str4", str4);
cout<< "nstr2, str4 are not changed. Type any letter to quit." << endl;
char q;
cin >> q;
return 0;
}
/*********************** Function Definitions **********************/
void PrintString(const char *label,
const String &str)
{
cout << label << " holds "";
cout << str; // << is overloaded
cout << "" (length = " << str.length() << ")" << endl;
}
/* Program Sample Output:
=========================
Initial values:
str1 holds "" (length = 0)
str2 holds "init2" (length = 5)
str3 holds "init3" (length = 5)
Enter a value for str1 (no spaces): red
Enter a value for str2 (no spaces): yellow
Enter a value for str3 (no spaces): blue
After assignments...
str1 holds "red" (length = 3)
str2 holds "yellow" (length = 6)
str3 holds "blue" (length = 4)
Enter which element of str1 to display: 1
Element #1 of str1 is 'e'
Enter which element of str2 to display: 2
Element #2 of str2 is 'l'
Enter which element of str3 to display: 3
Element #3 of str3 is 'e'
Enter a value to append to str1 (no spaces): rose
Enter a value to append to str2 (no spaces): house
Enter a value to append to str3 (no spaces): sky
After appending...
str1 holds "redrose" (length = 7)
str2 holds "yellowhouse" (length = 11)
str3 holds "bluesky" (length = 7)
Comparing str1 and str2...
"redrose" is less than "yellowhouse"
test the = operator, after str1 = str2;
str1 holds "yellowhouse" (length = 11)
str2 holds "yellowhouse" (length = 11)
After str1 = str1 + s1:
str1 holds "yellowhouserose" (length = 15)
str2 holds "yellowhouse" (length = 11)
test the copy constructor, after str4(str3);
str3 holds "bluesky" (length = 7)
str4 holds "bluesky" (length = 7)
after appending str3 by str2
str3 holds "blueskyyellowhouse" (length = 18)
str4 holds "bluesky" (length = 7)
str2, str4 are not changed. Type any letter to quit.
q
*/
=========================================================
======================================
//File: mystring1.cpp
// ================
// Implementation file for user-defined String class.
#include <cstirng>
#include "mystring1.h"
#pragma warning(disable:4996) // disbale the unsafe warning message to use strcpy_s(), etc
String::String()
{
contents[0] = '0';
len = 0;
}
String::String(const char s[])
{
len = strlen(s);
strcpy(contents, s);
}
void String::append(const String &str)
{
strcat(contents, str.contents);
len += str.len;
}
bool String::operator ==(const String &str) const
{
return strcmp(contents, str.contents) == 0;
}
bool String::operator !=(const String &str) const
{
return strcmp(contents, str.contents) != 0;
}
bool String::operator >(const String &str) const
{
return strcmp(contents, str.contents) > 0;
}
bool String::operator <(const String &str) const
{
return strcmp(contents, str.contents) < 0;
}
bool String::operator >=(const String &str) const
{
return strcmp(contents, str.contents) >= 0;
}
String String::operator +=(const String &str)
{
append(str);
return *this;
}
void String::print(ostream &out) const
{
out << contents;
}
int String::length() const
{
return len;
}
char String::operator [](int i) const
{
if (i < 0 || i >= len) {
cerr << "can't access location " << i
<< " of string "" << contents << """ << endl;
return '0';
}
return contents[i];
}
ostream & operator<<(ostream &out, const String & s) // overload ostream operator "<<" -
External!
{
s.print(out);
return out;
}
=====================================================================
============================
//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 String &str) const;
bool operator <(const String &str) const;
bool operator >=(const String &str) const;
String operator +=(const String &str);
void print(ostream &out) const;
int length() const;
char operator [](int i) const; // subscript operator
private:
char contents[MAX_STR_LENGTH+1];
int len;
};
ostream & operator<<(ostream &out, const String & r); // overload ostream operator "<<" -
External!
#endif /* not defined _MYSTRING_H */
CMPSC122 Lab Proj 6 - List and "Big Three" Project. C + + String Class (20 points - Submit
your source files named mystring2.h and mystring2.cpp online before the due time next week)
Objective: To gain experience with dynamic data structures (allocation, automatic expansion,
deletion) and the "big three" concepts: Destructors, Copy constructors, and Assignment
operators. Project Description: Two files mystringl.h and mystringl.cpp, define a String class
implementation by using a static array as the data member. The purpose of this assignment is to
rewrite this class by replacing the static array with a pointer (dynamic array). You are required to
modify the String class accordingly: Data: we wish to allow our String class to accommodate
strings of different sizes. To that end, instead of storing a string's characters in a fixed-size array,
each String object should contain the following data as part of its internal representation: (1) A
character pointer is meant to point to a dynamically allocated array of characters. (2) A length
field that will hold the current length of the string at any given moment. Operations: same as the
methods in String class, plus the "big three" (destructor, copy constructor, and assignment
operator overloading). Modularity: Write the String class code in a modular fashion, i.e., a file
mystring 2. h (the header file) should hold the class definition (use conditional compilation
directives) and a file mystring2.cpp (the implementation file) should hold the method definitions.
Download: LabProj6Driver.cpp -- A driver program provides a main program to test your new
class. Note: 1. In your implementation of String's methods, you may use the library functions
provided by < cstring>, which operate on the C-style string. Our textbook contains a good review
of it from pages 212 to 214 and Appendix D5 if you have the textbook. However, the difference
with the last assignment is that you need to allocate memory first in the implementations of
constructors. Do not change the implementations of overloading operators. 2. You need to
upload the two source files: your mystring2.h and mystring 2. c pp Grading Policy: (total 20
points) Grading of this programming assignment will reflect two factors, weighted as shown
below: 1. (17 points) Correctness -- does the program run correctly and follow the requirement?
2. (3 points) Style -- does the code follow the "Documentation and Submission Guidelines"? Is
the code well-structured, and readable? Do you paste your output as the comment after the main
function?

More Related Content

Similar to I need help designing the following program- This EXACT problem I don'.pdf

Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdfCan anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
arjunhassan8
 
Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++
Yandex
 
Router Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditionsRouter Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditions
Morteza Mahdilar
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
JawadTanvir
 
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdfProgramming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
ssuser6254411
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docx
adampcarr67227
 
06 -working_with_strings
06  -working_with_strings06  -working_with_strings
06 -working_with_strings
Hector Garzo
 
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
MARRY7
 

Similar to I need help designing the following program- This EXACT problem I don'.pdf (20)

Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdfCan anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
Can anyone fix this code, I use Visual StudiosThe Errors im getti.pdf
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++
 
Computer Programming- Lecture 7
Computer Programming- Lecture 7Computer Programming- Lecture 7
Computer Programming- Lecture 7
 
lecture-5 string.pptx
lecture-5 string.pptxlecture-5 string.pptx
lecture-5 string.pptx
 
Router Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditionsRouter Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditions
 
C q 3
C q 3C q 3
C q 3
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdfProgramming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docx
 
06 -working_with_strings
06  -working_with_strings06  -working_with_strings
06 -working_with_strings
 
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
Functions
FunctionsFunctions
Functions
 
Ds lec 14 filing in c++
Ds lec 14 filing in c++Ds lec 14 filing in c++
Ds lec 14 filing in c++
 
Javascripting.pptx
Javascripting.pptxJavascripting.pptx
Javascripting.pptx
 
Nitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptxNitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptx
 
The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184
 
Computer Programming- Lecture 6
Computer Programming- Lecture 6Computer Programming- Lecture 6
Computer Programming- Lecture 6
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 

More from shreeaadithyaacellso

In C++ Plz and In What Order Do I Put It In- LAB- Playlist (output li.pdf
In C++ Plz and In What Order Do I Put It In-  LAB- Playlist (output li.pdfIn C++ Plz and In What Order Do I Put It In-  LAB- Playlist (output li.pdf
In C++ Plz and In What Order Do I Put It In- LAB- Playlist (output li.pdf
shreeaadithyaacellso
 
I got the codes written down below- Basically- I am trying to implemen.pdf
I got the codes written down below- Basically- I am trying to implemen.pdfI got the codes written down below- Basically- I am trying to implemen.pdf
I got the codes written down below- Basically- I am trying to implemen.pdf
shreeaadithyaacellso
 
I need hlep I have posted this proble 4 times have gotten no help can.pdf
I need hlep I have posted this proble 4 times have gotten no help can.pdfI need hlep I have posted this proble 4 times have gotten no help can.pdf
I need hlep I have posted this proble 4 times have gotten no help can.pdf
shreeaadithyaacellso
 

More from shreeaadithyaacellso (20)

In c++ Polymorphism is one of the hallmarks of OOP languages- What are (1).pdf
In c++ Polymorphism is one of the hallmarks of OOP languages- What are (1).pdfIn c++ Polymorphism is one of the hallmarks of OOP languages- What are (1).pdf
In c++ Polymorphism is one of the hallmarks of OOP languages- What are (1).pdf
 
In Chapter 4 of LS- there is a case of Zappos and it describes how Zap.pdf
In Chapter 4 of LS- there is a case of Zappos and it describes how Zap.pdfIn Chapter 4 of LS- there is a case of Zappos and it describes how Zap.pdf
In Chapter 4 of LS- there is a case of Zappos and it describes how Zap.pdf
 
In C++ Plz LAB- Playlist (output linked list) Given main()- complete.pdf
In C++ Plz  LAB- Playlist (output linked list) Given main()- complete.pdfIn C++ Plz  LAB- Playlist (output linked list) Given main()- complete.pdf
In C++ Plz LAB- Playlist (output linked list) Given main()- complete.pdf
 
In C Programming- not C++ Write a function which takes two formal pa.pdf
In C Programming- not C++   Write a function which takes two formal pa.pdfIn C Programming- not C++   Write a function which takes two formal pa.pdf
In C Programming- not C++ Write a function which takes two formal pa.pdf
 
In chapter 17 you learned about the big picture of circulation through.pdf
In chapter 17 you learned about the big picture of circulation through.pdfIn chapter 17 you learned about the big picture of circulation through.pdf
In chapter 17 you learned about the big picture of circulation through.pdf
 
In cell M11- enter a formula that will calculate the total amount due.pdf
In cell M11- enter a formula that will calculate the total amount due.pdfIn cell M11- enter a formula that will calculate the total amount due.pdf
In cell M11- enter a formula that will calculate the total amount due.pdf
 
In C++ Plz and In What Order Do I Put It In- LAB- Playlist (output li.pdf
In C++ Plz and In What Order Do I Put It In-  LAB- Playlist (output li.pdfIn C++ Plz and In What Order Do I Put It In-  LAB- Playlist (output li.pdf
In C++ Plz and In What Order Do I Put It In- LAB- Playlist (output li.pdf
 
In C++ Complete the program with the bold requirements #include #i.pdf
In C++  Complete the program with the bold requirements   #include  #i.pdfIn C++  Complete the program with the bold requirements   #include  #i.pdf
In C++ Complete the program with the bold requirements #include #i.pdf
 
I am studying for a test tomorrow in my 494 Population Biology class-.pdf
I am studying for a test tomorrow in my 494 Population Biology class-.pdfI am studying for a test tomorrow in my 494 Population Biology class-.pdf
I am studying for a test tomorrow in my 494 Population Biology class-.pdf
 
I got the codes written down below- Basically- I am trying to implemen.pdf
I got the codes written down below- Basically- I am trying to implemen.pdfI got the codes written down below- Basically- I am trying to implemen.pdf
I got the codes written down below- Basically- I am trying to implemen.pdf
 
I am trying to write a program that will converts a 32bit float number.pdf
I am trying to write a program that will converts a 32bit float number.pdfI am trying to write a program that will converts a 32bit float number.pdf
I am trying to write a program that will converts a 32bit float number.pdf
 
I am stuck on this question- please show the solution step by step- Re.pdf
I am stuck on this question- please show the solution step by step- Re.pdfI am stuck on this question- please show the solution step by step- Re.pdf
I am stuck on this question- please show the solution step by step- Re.pdf
 
I et X and Y he two indenendent R(3n-1-3) random variables- Calculate.pdf
I et X and Y he two indenendent R(3n-1-3) random variables- Calculate.pdfI et X and Y he two indenendent R(3n-1-3) random variables- Calculate.pdf
I et X and Y he two indenendent R(3n-1-3) random variables- Calculate.pdf
 
I am working on a coding project- and it asked me to combine five sets.pdf
I am working on a coding project- and it asked me to combine five sets.pdfI am working on a coding project- and it asked me to combine five sets.pdf
I am working on a coding project- and it asked me to combine five sets.pdf
 
I am needing to do a Point Factor Job Evaluation for a IT Company whos.pdf
I am needing to do a Point Factor Job Evaluation for a IT Company whos.pdfI am needing to do a Point Factor Job Evaluation for a IT Company whos.pdf
I am needing to do a Point Factor Job Evaluation for a IT Company whos.pdf
 
i cant figure out this excel equation For the Year Ended December 31-.pdf
i cant figure out this excel equation For the Year Ended December 31-.pdfi cant figure out this excel equation For the Year Ended December 31-.pdf
i cant figure out this excel equation For the Year Ended December 31-.pdf
 
I am writing a program that will allow the user to move the crosshairs.pdf
I am writing a program that will allow the user to move the crosshairs.pdfI am writing a program that will allow the user to move the crosshairs.pdf
I am writing a program that will allow the user to move the crosshairs.pdf
 
I need simple java code for the below scenario- UML Diagrams(Class-Use.pdf
I need simple java code for the below scenario- UML Diagrams(Class-Use.pdfI need simple java code for the below scenario- UML Diagrams(Class-Use.pdf
I need simple java code for the below scenario- UML Diagrams(Class-Use.pdf
 
I need hlep I have posted this proble 4 times have gotten no help can.pdf
I need hlep I have posted this proble 4 times have gotten no help can.pdfI need hlep I have posted this proble 4 times have gotten no help can.pdf
I need hlep I have posted this proble 4 times have gotten no help can.pdf
 
I need question 1 answer in java language- question 1)Create an applic.pdf
I need question 1 answer in java language- question 1)Create an applic.pdfI need question 1 answer in java language- question 1)Create an applic.pdf
I need question 1 answer in java language- question 1)Create an applic.pdf
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 

I need help designing the following program- This EXACT problem I don'.pdf

  • 1. I need help designing the following program. This EXACT problem I don't think was posted previously. /** * Driver File * File Name: LabProj6Driver.cpp * * Description: This program demonstrates a basic String class that implements * dynamic allocation and operator overloading. * */ #include <iostream> #include "mystring2.h" using namespace std; /************************ Function Prototypes ************************/ /* * Function: PrintString * Usage: PrintString(str); * * Prints out the value and length of the String object passed to it. */ void PrintString(const char *label, const String &str); // overloaded ostream operator << is used in the definition. /*************************** Main Program **************************/ int main() { String str1, str2("init2"), str3 = "init3"; // Some String objects. Using constructor for copy char s1[100], s2[100], s3[100]; // Some character strings. // Print out their initial values... cout << "Initial values:" << endl; PrintString("str1", str1); PrintString("str2", str2); PrintString("str3", str3); // Store some values in them...
  • 2. cout << "nEnter a value for str1 (no spaces): "; cin >> s1; str1 = s1; cout << "nEnter a value for str2 (no spaces): "; cin >> s2; str2 = s2; cout << "nEnter a value for str3 (no spaces): "; cin >> s3; str3 = s3; cout << "nAfter assignments..." << endl; PrintString("str1", str1); PrintString("str2", str2); PrintString("str3", str3); // Access some elements... int i; cout << "nEnter which element of str1 to display: "; cin >> i; cout << "Element #" << i << " of str1 is '" << str1[i] << "'" << endl; cout << "nEnter which element of str2 to display: "; cin >> i; cout << "Element #" << i << " of str2 is '" << str2[i] << "'" << endl; cout << "nEnter which element of str3 to display: "; cin >> i; cout << "Element #" << i << " of str3 is '" << str3[i] << "'" << endl; // Append some strings... cout << "nEnter a value to append to str1 (no spaces): "; cin >> s1; // str1.append(s1); // Actually, the cstring is converted to String object here by the constructor str1 += s1; // same as above cout << "nEnter a value to append to str2 (no spaces): "; cin >> s2; str2 += s2;
  • 3. cout << "nEnter a value to append to str3 (no spaces): "; cin >> s3; str3 += s3; cout << "nAfter appending..." << endl; PrintString("str1", str1); PrintString("str2", str2); PrintString("str3", str3); // Compare some strings... cout << "nComparing str1 and str2..." << endl; cout << """; cout<< str1; // test the overloading of ostream operator << cout << "" is "; if (str1 < str2) { // test the overloading of comparison operator < cout << "less than"; } else if (str1 > str2) { cout << "greater than"; } else { cout << "equal to"; } cout << " ""; cout << str2; cout << """ << endl; cout << "ntest the = operator, after str1 = str2; "<< endl; str1 = str2; PrintString("str1", str1); PrintString("str2", str2); str1 += s1; cout << "nAfter str1 = str1 + s1: "<< endl; PrintString("str1", str1); PrintString("str2", str2);
  • 4. String str4(str3); cout << "ntest the copy constructor, after str4(str3);"<< endl; PrintString("str3", str3); PrintString("str4", str4); cout << "nafter appending str3 by str2" << endl; str3 += str2; PrintString("str3", str3); PrintString("str4", str4); cout<< "nstr2, str4 are not changed. Type any letter to quit." << endl; char q; cin >> q; return 0; } /*********************** Function Definitions **********************/ void PrintString(const char *label, const String &str) { cout << label << " holds ""; cout << str; // << is overloaded cout << "" (length = " << str.length() << ")" << endl; } /* Program Sample Output: ========================= Initial values: str1 holds "" (length = 0) str2 holds "init2" (length = 5) str3 holds "init3" (length = 5) Enter a value for str1 (no spaces): red Enter a value for str2 (no spaces): yellow Enter a value for str3 (no spaces): blue After assignments... str1 holds "red" (length = 3) str2 holds "yellow" (length = 6) str3 holds "blue" (length = 4)
  • 5. Enter which element of str1 to display: 1 Element #1 of str1 is 'e' Enter which element of str2 to display: 2 Element #2 of str2 is 'l' Enter which element of str3 to display: 3 Element #3 of str3 is 'e' Enter a value to append to str1 (no spaces): rose Enter a value to append to str2 (no spaces): house Enter a value to append to str3 (no spaces): sky After appending... str1 holds "redrose" (length = 7) str2 holds "yellowhouse" (length = 11) str3 holds "bluesky" (length = 7) Comparing str1 and str2... "redrose" is less than "yellowhouse" test the = operator, after str1 = str2; str1 holds "yellowhouse" (length = 11) str2 holds "yellowhouse" (length = 11) After str1 = str1 + s1: str1 holds "yellowhouserose" (length = 15) str2 holds "yellowhouse" (length = 11) test the copy constructor, after str4(str3); str3 holds "bluesky" (length = 7) str4 holds "bluesky" (length = 7) after appending str3 by str2 str3 holds "blueskyyellowhouse" (length = 18) str4 holds "bluesky" (length = 7) str2, str4 are not changed. Type any letter to quit. q */ ========================================================= ======================================
  • 6. //File: mystring1.cpp // ================ // Implementation file for user-defined String class. #include <cstirng> #include "mystring1.h" #pragma warning(disable:4996) // disbale the unsafe warning message to use strcpy_s(), etc String::String() { contents[0] = '0'; len = 0; } String::String(const char s[]) { len = strlen(s); strcpy(contents, s); } void String::append(const String &str) { strcat(contents, str.contents); len += str.len; } bool String::operator ==(const String &str) const { return strcmp(contents, str.contents) == 0; } bool String::operator !=(const String &str) const { return strcmp(contents, str.contents) != 0; } bool String::operator >(const String &str) const { return strcmp(contents, str.contents) > 0; } bool String::operator <(const String &str) const { return strcmp(contents, str.contents) < 0; }
  • 7. bool String::operator >=(const String &str) const { return strcmp(contents, str.contents) >= 0; } String String::operator +=(const String &str) { append(str); return *this; } void String::print(ostream &out) const { out << contents; } int String::length() const { return len; } char String::operator [](int i) const { if (i < 0 || i >= len) { cerr << "can't access location " << i << " of string "" << contents << """ << endl; return '0'; } return contents[i]; } ostream & operator<<(ostream &out, const String & s) // overload ostream operator "<<" - External! { s.print(out); return out; } ===================================================================== ============================ //File: mystring1.h // ================ // Interface file for user-defined String class.
  • 8. #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 String &str) const; bool operator <(const String &str) const; bool operator >=(const String &str) const; String operator +=(const String &str); void print(ostream &out) const; int length() const; char operator [](int i) const; // subscript operator private: char contents[MAX_STR_LENGTH+1]; int len; }; ostream & operator<<(ostream &out, const String & r); // overload ostream operator "<<" - External! #endif /* not defined _MYSTRING_H */ CMPSC122 Lab Proj 6 - List and "Big Three" Project. C + + String Class (20 points - Submit your source files named mystring2.h and mystring2.cpp online before the due time next week) Objective: To gain experience with dynamic data structures (allocation, automatic expansion, deletion) and the "big three" concepts: Destructors, Copy constructors, and Assignment operators. Project Description: Two files mystringl.h and mystringl.cpp, define a String class implementation by using a static array as the data member. The purpose of this assignment is to
  • 9. rewrite this class by replacing the static array with a pointer (dynamic array). You are required to modify the String class accordingly: Data: we wish to allow our String class to accommodate strings of different sizes. To that end, instead of storing a string's characters in a fixed-size array, each String object should contain the following data as part of its internal representation: (1) A character pointer is meant to point to a dynamically allocated array of characters. (2) A length field that will hold the current length of the string at any given moment. Operations: same as the methods in String class, plus the "big three" (destructor, copy constructor, and assignment operator overloading). Modularity: Write the String class code in a modular fashion, i.e., a file mystring 2. h (the header file) should hold the class definition (use conditional compilation directives) and a file mystring2.cpp (the implementation file) should hold the method definitions. Download: LabProj6Driver.cpp -- A driver program provides a main program to test your new class. Note: 1. In your implementation of String's methods, you may use the library functions provided by < cstring>, which operate on the C-style string. Our textbook contains a good review of it from pages 212 to 214 and Appendix D5 if you have the textbook. However, the difference with the last assignment is that you need to allocate memory first in the implementations of constructors. Do not change the implementations of overloading operators. 2. You need to upload the two source files: your mystring2.h and mystring 2. c pp Grading Policy: (total 20 points) Grading of this programming assignment will reflect two factors, weighted as shown below: 1. (17 points) Correctness -- does the program run correctly and follow the requirement? 2. (3 points) Style -- does the code follow the "Documentation and Submission Guidelines"? Is the code well-structured, and readable? Do you paste your output as the comment after the main function?