×
Traktatov.net » Освой самостоятельно С++ за 21 день » Читать онлайн
Страница 435 из 448 Настройки

5: {

6:    public:

7:       // конструкторы

8:       String();

9:       String(const char *const):

10:      String(const String &);

11:      ~String();

12:

13:      // перегруженные операторы

14:      char & operator[](int offset);

15:      char operator[](int offset) const:

16:      String operator+(const String&);

17:      void operator+=(const String&);

18:      String & operator= (const String &);

19:      friend ostream& operator<<

20:         ( ostream& _theStream,String& theString);

21:      friend ist.ream& operator>>

22:         ( istream& _theStream,String& theString);

23:      // Общие функции доступа

24:      int GetLen()const { return itsLen; }

25:      const char * GetString() const { return itsString; }

26:      // static int ConstructorCount:

27:

28:   private:

29:      String (int); // закрытый конструктор

30:      char * itsString;

31:      unsigned short itslen;

32:

33: };

34:

35: ostream& operator<<( ostream& theStream,String& theStnng)

36: {

37:    theStream << theString.GetString();

38:    return theStream;

39: }

40:

41: istream& operator>>( istream& theStream,String& theString)

42: {

43:    theStream >> theString.GetString();

44:    return theStream;

45: }

46:

47: int main()

48: {

49: StringtheString("npHBeT, мир.");

50: cout << theString;

51:     return 0;

52: }

5. Жучки: что неправильно в этой программе?

1: #include

2:

3: class Animal;

4:

5: void setValue(Animal& , int);

6:

7:

8: class Animal

9: {

10:   public:

11:      int GetWeight()const { return itsWeight; }

12:      int GetAge() const { return itsAge; }

13:   private:

14:      int itsWeight;

15:      int itsAge;

16: };

17:

18: void setValue(Animal& theAnimal, int theWeight)

19: {

20:    friend class Animal;

21:    theAnimalitsWeight = theWeight;

22: }

23:

24: int main()

25: {

26:    Animal peppy;

27:    setValue(peppy,5):

28:    return 0;

29: }

Нельзя помещать объявление friend в функцию. Нужно объявить функцию другом в объявлении класса.

6. Исправьте листинг, приведенный в упражнении 5, и откомпилируйте его.

1: #include

2:

3: class Animal;

4:

5: void setValue(Animal& , int);

6:

7:

8:  class Animal

9:  {

10:    public:

11:       friend void setValue(Animal&, int);

12:       int GetWeight()const { return itsWeight; }

13:       int GetAge() const { return itsAge; }

14:    private:

15:       int itsWeight;

16:       int itsAge;

17: };

18:

19: void setValue(Animal& theAnimal, int theWeight)

20: {

21:    theAnimal.itsWeight = theWeight;

22: }

23:

24: int main()

25: {

26:    Animal peppy;

27:    setValue(peppy,5);

28:    return 0;

29: }

7. Жучки: что неправильно в этой программе?

1: #include

2:

3: class Animal;

4:

5: void setValue(Animal& , int):

6: void setValue(Animal& , int.int);

7:

8: class Animal

9: {

10:   friend void setValue(Animal& ,int):

11:   private:

12:      mt itsWeight;

13:      int itsAge;

14: };

15:

16: void setValue(Animal& theAnimal, int theWeight)

17: {

18:    theAnimal.itsWeight = theWeight;

19: }

20:

21:

22: void setValue(Animal& theAnimal, int theWeight, int theAge)

23: {

24:    theAnimal.itsWeight = theWeight:

25:    theAnimal.itsAge = theAge;