I need help with this program.
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
|
#define DEBUG
#define SHOW_INVARIANTS
#include <iostream>
using namespace std;
#include <string.h>
#ifndef DEBUG
#define ASSERT(x)
#else
#define ASSERT(x) if (!(x)) { cout << "Greska!! Provera " << #x << " je propala\n"; cout << " u redu " << __LINE__ << "\n"; cout << " u datoteci " << __FILE__ << "\n"; }
//
//
//
//
//
#endif
//klasa STRING
class String
{
public:
//konstruktori
String();
String(const char * const);
String(const String &);
~String();
//operatori
char & operator[](int offSet);
char operator[](int offSet) const;
String & operator=(const String &);
int GetLen() const { return itsLen; }
const char * GetString() const { return itsString; }
bool Invariants() const; //Koristi se kod ASSERT za proveru
private:
String(int);
int itsLen;
char * itsString;
};
String::String()
{
itsString=new char[1];
itsString[0]='\0';
itsLen=0;
ASSERT(Invariants());
}
String::String (const char * const cString)
{
itsLen=strlen(cString);
itsString=new char[itsLen+1];
for (int i=0; i<itsLen; i++)
itsString[i]=cString[i];
itsString[itsLen]='\0';
ASSERT(Invariants());
}
String::String(const String & rhs)
{
itsLen=rhs.GetLen();
itsString=new char[itsLen+1];
for (int i=0; i<itsLen; i++)
itsString[i]=rhs[i];
itsString[itsLen]='\0';
ASSERT(Invariants());
}
String::String(int value)
{
itsLen=value;
itsString=new char[itsLen+1];
for (int i=0; i<=itsLen; i++)
itsString[i]='\0';
ASSERT(Invariants());
}
String::~String()
{
ASSERT(Invariants());
delete [] itsString;
itsLen=0;
}
char & String::operator[](int offSet)
{
ASSERT(Invariants());
if (offSet>itsLen)
{
ASSERT(Invariants());
return itsString[itsLen-1];
}
else
{
ASSERT(Invariants());
return itsString[offSet];
}
}
char String::operator[](int offSet) const
{
ASSERT(Invariants());
char val;
if (offSet>itsLen)
val=itsString[itsLen-1];
else
val=itsString[itsLen];
ASSERT(Invariants());
return val;
}
String & String::operator=(const String & rhs)
{
ASSERT(Invariants());
if (this==&rhs)
return *this;
delete [] itsString;
itsLen=rhs.GetLen();
itsString=new char[itsLen+1];
for (int i=0; i<itsLen; i++)
itsString[i]=rhs[i];
itsString[itsLen]='\0';
ASSERT(Invariants());
return *this;
}
bool String::Invariants() const
{
#ifdef SHOW_INVARIANTS
//cout << "Invarijante su testirane.\n";
#endif
return (itsLen && itsString);
}
//klasa ANIMAL
class Animal
{
public:
//konstruktori
Animal():itsAge(1),itsName("John Q. Animal") { ASSERT(Invariants()); }
Animal(int, const String &);
~Animal() {}
//funkcije pristupa
int GetAge() const { ASSERT(Invariants()); return itsAge;}
void SetAge(int age) { ASSERT(Invariants()); itsAge=age; ASSERT(Invariants()); }
const String & GetName() const { ASSERT(Invariants()); return itsName; }
void SetName(const String & name) { ASSERT(Invariants()); itsName=name; ASSERT(Invariants()); }
bool Invariants() const;
private:
int itsAge;
String itsName;
};
Animal::Animal(int age, const String & theName):
itsAge(age), itsName(theName)
{
ASSERT(Invariants());
}
bool Animal::Invariants() const
{
#ifdef SHOW_INVARIANTS
//cout << "Invarijante su testirane.\n";
#endif
return (itsAge>0 && itsName.GetLen());
}
int main()
{
Animal sparky(5,"Sparky");
cout << "\n" << sparky.GetName().GetString() << " ima ";
cout << sparky.GetAge() << " godina.\n";
sparky.SetAge(8);
cout << "\n" << sparky.GetName().GetString() << " ima ";
cout << sparky.GetAge() << " godina.\n";
return 0;
}
|
It's example from the book, but I had to change some lines for this code to work.
Program can run but the name of Animal isn't visible. It display all the values like it should do, except the name.
I think it is problem with GetString() function in String class but I am not sure. First I thought that I didn't correctly called function, but when I tried to use GetLen() it worked all right (returning int). Does somebody knows what is the problem and how to solve it?
--------------------------------------------------------------------
Ok I made it to work so it is showing now the name of Animal, but I still don't see what's the difference and why didn't work before.
This was changed:
Line 145 in
|
Animal(int, const char *);
|
and also line 160 in
1 2
|
Animal::Animal(int age, const char * theName):
|
I suppose that "Sparky" in line 177 is threated as (const char), but constructor of class Animal asks for (const String &) so it creates const String from const char, right?
Then (itsName(theName)) is actually String(const String &) and itsName is object of class String.
Ok, different constructor of class String is called but the result should be the same right (object of class String)?
Then why GetLen() works and GetString() doesn't?