1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
#include "Text.h"
#include <iostream>
#include<cstring>
using namespace std;
// destructor
Text::~Text(){
delete[] this->text;
}
// Default Constructor
Text:: Text(){
this->text = new char; //c++ cant print null pointer
}
// copy constructor /constructor overloading
Text::Text(const Text &Text){
this->text = new char[strlen(Text.text) + 1]; // obviously points to nothing so allocate dynamic memory allocation with same size as that of passed arg
strcpy(this->text,Text.text); // copy the text
}
// conversion constructor / constructor overloading
Text::Text(const char* text){
this->text = new char[strlen(text) + 1]; // obviously points to nothing so allocate dynamic memory and copy
strcpy(this->text,text);
}
// setter
void Text::set(const char* text){
if (this->text == NULL){ // if NULL then allocate dynamic memory and copy
this->text = new char[strlen(text) + 1];
strcpy(this->text,text);
}else{ // if not NULL then deallocate, allocate dynamic memory and copy
delete[] this->text;
this->text = new char[strlen(text) + 1];
strcpy(this->text,text);
}
}
// overloading setter
void Text::set(const Text &Text){
delete[] this->text; // deallocate, allocate, copy
this->text = new char[strlen(Text.text) + 1];
strcpy(this->text,Text.text);
}
// overloading = operator
Text & Text::operator=(const Text &Text){ // Note the Precedence of this operator is from RIGHT to LEFT
set(Text); // could make this faster by checking if both have the same string
return *this; //A question....what if i return only this. thats not possible coz of const keyword.
// Why cant we use &this as return. If an object calls it also passes itself as an argument(obviously by //reference). But then *this would be the calling object not its reference.
// But when we return it is converted back to reference coz of Text & at return type.
}
// appending to Text_store of an object
void Text::append (const char* text){
// Store this->text in temp and deallocate this->text
char *temp = new char[strlen(this->text)];
strcpy(temp,this->text);
delete[] this->text;
// Allocate memory for new string
int len = strlen(temp) + strlen(text) + 1;
this->text = new char[len];
// Copy temp in this->text. Then concatinate text to this->text
strcpy(this->text,temp);
strcat(this->text,text);
// deallocate temp
delete [] temp;
}
// overloading appending to add Text_store of object passed as parameter to the calling object
void Text::append(const Text &Text){
// Store this->text in temp and deallocate this->text
char *temp = new char[strlen(this->text)];
strcpy(temp,this->text);
delete[] this->text;
// Allocate memory for new string
int len = strlen(temp) + strlen(Text.text) + 1;
this->text = new char[len];
// Copy temp in this->text. Then concatinate text to this->text
strcpy(this->text,temp);
strcat(this->text,Text.text);
// deallocate temp
delete [] temp;
}
// These are read only functions i.e. they can only read the private members and not modify them
int Text::length() const{
return strlen(this->text);
}
// These are read only functions i.e. they can only read the private members and not modify them
bool Text::isEmpty() const{
return (this->length()==0 ? true:false);
}
std::ostream& operator<<(std::ostream& os, const Text &Text){ //friend function for operator overloading
if(Text.text==NULL){ //had to do this because c++ cant print null pointer
os << "";
}else{
os << Text.text;
return os;
}
}
|