0% found this document useful (0 votes)
3 views2 pages

MyString Academic Notes

Uploaded by

waniyasaeed2129
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

MyString Academic Notes

Uploaded by

waniyasaeed2129
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MYSTRING CLASS (Object Oriented Programming

– C++)

1. Introduction
MyString is a user-defined string class implemented using dynamic memory and pointers. It mimics
basic string behavior without using the C++ string library.

2. Data Members
char *str stores the address of the dynamically allocated character array. int length stores the
number of characters excluding the null terminator.

3. Default Constructor
It sets length to 0, allocates one character, and stores '\0'. This creates an empty safe string.

4. Overloaded Constructor
It counts the characters of the input, allocates memory of size length+1, copies characters one by
one, and appends '\0'.

5. Destructor
It releases dynamically allocated memory using delete[].

6. display()
It prints the string stored in str.

7. getLength()
It returns the length data member.

8. Copy Constructor
It creates a deep copy by allocating new memory and copying characters.

9. Assignment Operator
It deletes old memory, allocates new memory, and copies characters.

10. Stream Insertion Operator <<


It allows MyString objects to be printed using cout.
11. Concatenation Operator +
It creates a new MyString by joining two strings into a newly allocated array.

12. Equality Operator ==


It compares lengths and characters to check equality.

13. Less Than Operator <


It compares strings lexicographically using ASCII values.

14. Less Than or Equal Operator <=


It returns true if either < or == is true.

15. += Operator
It appends another string to the current object, reallocating memory if needed.

16. setStr()
It deletes old data and stores a new C-string dynamically.

17. Stream Extraction Operator >>


It reads user input and stores it using dynamic memory.

18. main() Dry Run


The main function creates objects, concatenates strings, compares them, modifies strings, and
takes user input to demonstrate functionality.

You might also like