C++ Workshop - Aggregation & Friends (Ch. 16)

Started by
2 comments, last by Dbproguy 15 years, 10 months ago

Welcome to the GDNet C++ Workshop – Ch. 16

For a complete introduction to this workshop, please look here. Workshop Overview This workshop is designed to aid people in their journey to learn beginning C++. This workshop is targeted at highly motivated individuals who are interested in learning C++ or who have attempted to learn C++ in the past, but found that without sufficient support and mentoring they were unable to connect all the pieces of this highly complex but powerful programming language. This is a 'guided' self-teaching C++ workshop. Each student is responsible for taking the time to read the material and learn the information. The community and tutors that arise out of this workshop are here for making the learning process run more smoothly, but are not obligated to baby-sit a person's progress. Because everyone will be working from the same textbook (Teach Yourself C++ in 21 days 5th Ed.), students may find it easier to get answers to the specific questions they might have. There is no minimum age requirement, and there is no previous programming experience required. Additionally, this workshop does not attempt to defend C++ as a language, nor does it attempt to demonstrate that C++ is either more or less useful then other programming languages for any particular purpose. People who intend to start a discussion about the differences between C++ and ANY other languages (except as are relevant to a particular discussion), are encouraged to do so elsewhere. This workshop is for educational, not philosophical discussions. Quizzes & Exercises Each week will have quizzes and exercises posted in the weekly threads. Please try and answer them by yourself. As well, please DO NOT post the answers to Quizzes and Exercises within this thread. Once it becomes acceptable to post the answers to quizzes and exercises, an additional thread will be created each week specifically for the purpose of posting quiz answers. If you try with reasonable effort but are unable to answer the questions or complete the exercises, feel free to post a clarification question here on the thread. Tutors, myself, or others will do the best we can to point you in the right direction for finding the answer.

Chapter 16 – Advanced Inheritance

Introduction Hello all! Welcome to another week of the C++ Workshop. This week we'll be exploring the differences between inheritance (is-a) and aggregation (has-a) relationships, and the appropriate times to use both. Additionally, we'll be looking at private inheritance and one method to deal with the fact that aggregate members have the same access restrictions as any other objects - friend classes. And even though this is a long chapter, at roughly 50pp, it's not a relatively difficult chapter, as much of the content is code samples. As usual, feel free to post any questions you have regarding the chapter in this thread and myself or another friendly individual will try and answer your questions. Finally, project 2 coming soon! Outline of the Reading - Chapter 16
  1. Aggregation
  2. Implementation in Terms of Inheritance Versus Aggregation/Delegation
  3. Private Inheritance
  4. Adding Friend Classes
  5. Friend Functions
  6. Friend Functions and Operator Overloading
  7. Overloading the Insertion Operator
Additional Resources None...

Good Luck!

[Edited by - jwalsh on May 30, 2007 5:27:11 PM]
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Advertisement
Greetings All!

It's once again QUIZ TIME!!! That's right, listed below are a set of quiz questions to help you test your knowledge and understanding of the material contained in chapters 16.

In addition to the questions and exercises below, make sure that as you're reading the book you enter the examples into your compiler, build the program, and run the executable. I know this is a time consuming process, but the repeat use of keywords, syntax, and semantics will help ingrain the information into your long-term memory. My advice is to create a simple "driver" project with a function main. As you read, enter the examples into function main, test it, and then erase it for use again in the next example.

PLEASE DO NOT POST THE ANSWERS TO THESE QUESTIONS OR EXERCISES. If you are unable to answer these questions, please ask for assistance, but DO NOT POST THE ANSWERS. Any question which is not marked with [Extra Credit] can be answered by reading your textbook. Questions which are marked [Extra Credit] either have been answered in the thread previously, or can be answered by doing a bit of research.

I will create an answer thread for these questions immediately, so that people will have a chance to get the answers more quickly.

Chapter 16 Quiz

1. Does a class that contains objects of another class gain special access to the member variables of those objects?
2. Does an object which is a member of a parent class have access to the parent’s variables or methods?
3. What is aggregation?
4. What is delegation?
5. What happens to the public functions and members of a base class when inherited into a derived class using private inheritance?
6. Is private inheritance “Interface Inheritance” or “Implementation Inheritance”? Why?
7. What’s one method of making private members and functions available to specific classes, without exposing them to all clients?
8. Is friendship inherited?
9. Is friendship commutative? That is, does making classA a friend of classB automatically make the opposite true?
10. Does “friendship” reduce encapsulation? Why or why not?
11. Does “friendship” reduce modality? Why or why not?
12. Do you have to declare an entire class a friend? What other options are there?
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Well, it's been a long while, but, I haven't given up on this Workshop yet:

1. Does a class that contains objects of another class gain special access to the member variables of those objects?
A) No, a class that aggregates other objects does not have special access to those object’s member data and functions.

2. Does an object which is a member of a parent class have access to the parent’s variables or methods?
A) No, aggregated members don’t have any special access to the members of the class which they are aggregated. The only ability they have to access the instance that aggregates them is to have a copy of the owner class “this” pointer passed to them at creation or at some point thereafter. If this is done, they have the same normal access to that object as they would to any other.

3. What is aggregation?
A) Aggregation is declaring an object as a member of another class contained by that class, this is also referred to as has-a.

4. What is delegation?
A) Delegation is using the members of an aggregated class to perform functions for the containing class.

5. What happens to the public functions and members of a base class when inherited into a derived class using private inheritance?
A) All the functions and members are then treated as if they were declared to be private, regardless of their actual access level in the base.

6. Is private inheritance “Interface Inheritance” or “Implementation Inheritance”? Why?
A) Implementation Inheritance, reason for this is that the class is invisible and therefore that class can’t be accessed directly, only by the derived class.

7. What’s one method of making private members and functions available to specific classes, without exposing them to all clients?
A) One way is to declare that certain class to be a friend. This extends the interface of your class to include the friend class.

8. Is friendship inherited?
A) No, friendship is not inheritable.

9. Is friendship commutative? That is, does making class A a friend of class B automatically make the opposite true?
A) No, not automatically, though you can make Class A a friend of class B and class B a friend of class A by explicitly writing it in each class it’s declaration.

10. Does “friendship” reduce encapsulation? Why or why not?
A) No, because the friend declaration makes the declared friend a part of the class interface and does not have to undermine encapsulation.

11. Does “friendship” reduce modality? Why or why not?
A) Yes, reason is that using friend implies a commitment to parallel maintenance of both classes, which could reduce modularity.

12. Do you have to declare an entire class a friend? What other options are there?
A) No, you can declare any function, regardless of whether it is a member function of another class, to be a friend.
I had my internet down for a few days, but it's back up. While it was down I read up to chapter 16 (And started 17, I'm reading it now!) and for me this was hard to take in at first but as usual, after a second look I got it all. I figure most the stuff I don't understand, as it comes up in future books I'll understand it more and more each time. Thanks for the workshop btw
--Dbproguy - My Blog - Tips, Opinions and Reviews about C++, Video Games, and Life

This topic is closed to new replies.

Advertisement