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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
#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){
//delete 1
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){
//delete
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
//delete 1
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;
//delete 1
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
//delete 1
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
//delete 1
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
//delete 1
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;
}
}
/*
int main()
{
Text t1("Welcome to C++"); // conversion constructor 4
Text t2; // defalt constructor
Text t3(t1); // copy constructor
cout << "t1: " << t1 << endl; // operator<< overload 7
cout << "t2: " << t2 << endl;
cout << "t3: " << t3 << endl;
t2.set(" Programming"); // set t2's text to " Programming" 11
cout << "t2: " << t2 << endl;
t3.set(t1); // set t3's text to t1's text 14
cout << "t3: " << t3 << endl;
t1.append(" Programming"); // append the c-string " Programming" to t1's text 17
cout << "t1: " << t1 << endl;
t3.append(t2); // append t2's text to t3's text 20
cout << "t3: " << t3 << endl;
cout<< t3.isEmpty()<<endl;
cout<<t2.length()<<endl;
Text t4;
t4 = t3 = t2;
Text t5;
t2.append("?->");
cout << t2;
return 0;
}
*/
|