The purpose of this assignment is to give you practice with constuctors, friend
ID: 3818418 • Letter: T
Question
The purpose of this assignment is to give you practice with constuctors, friend functions, static data members, static member functions and overloaded operators.
Program Overview
You are to write a String class with the necessary constructors and overloaded operator functions that makes use of the requisite main() function to produce the output below.
Requirements
The program will be tested using the main() listed below.
The String class must contain:
one non-static data member, a char* pointer.
one static data member to keep a count of the number of a characters contained in all of the String objects in existence.
3 constructors. The default constuctor, a copy constructor and one other constructor.
necessary overloaded operator functions
a static member function that returns the number of a characters in all of the String objects in existence.
a overloaded insertion operator friend function, used to print a String object.
For this assignment you may NOT use the string class.
The Required main() function
Program Output
Your output should look like the following.
Program Hint
Write and test one function at a time. Perform thorough testing, not just the the required main().
int main(){
// Constructors
String A("apple");
String B("banana");
String C("cantaloupe");
String D(B);
String E;
// static member function
cout << "Number of a's = " << String::a_count() << endl << endl;
// Overloaded insertion operator
cout << "A = " << A << endl;
cout << "B = " << B << endl;
cout << "C = " << C << endl;
cout << "D = " << D << endl;
cout << "E = " << E << endl << endl;
// Relational operators
cout << boolalpha;
cout << "A < B " << (A < B) << endl;
cout << "B < A " << (B < A) << endl;
cout << "A == B " << (A == B) << endl << endl;
// Assignment operator
A = B;
cout << "A = " << A << endl;
cout << "A == B " << (A == B) << endl << endl;
// Size (bang) operator
cout << "A size = " << !A << endl;
cout << "E size = " << !E << endl << endl;
// Unary * operator
cout << "C text = " << *C << endl << endl;
// Plus operator
cout << "A + B = " << A + B << endl << endl;
// Plus equal operator
A += C;
cout << "A = " << A << endl << endl;
// Index operator
cout << "A[3] = " << A[3] << endl << endl;
// static member function
cout << "Number of a's = " << String::a_count() << endl;
}
Explanation / Answer
Hi,
To count the total number of a's, you can make a function as you have written in above problem and in that function you can write the logic as:
for(char c=s[0];c<s.length();c++)
{
count++;
}
and after that you can return the count from the function.For the overloaded constructor, you can follow the steps:
public constructor(par 1,par 2)
{}
Like this ,you can make a overloaded constructor and add the above written logic in it.
I hope it will solve your problem.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.