C++ Xcode OOP Friend Functions Tutorial

C++ XCode OOP Friend Functions Tutorial

C++ XCode OOP Friend Functions Tutorial

Our C++ XCode OOP Friend Functions Tutorial presented here builds off the previous C++ XCode OOP Composite Objects Tutorial below. Friend Functions can be used to modify private data members of classes which have explicitly allowed access to these private data members by the Friend Functions.

In object-oriented programming, a friend function that is a “friend” of a given class is allowed access to private and protected data in that class that it would not normally be able to as if the data was public. [1] Normally, a function that is defined outside of a class cannot access such information. Declaring a function a friend of a class allows this, in languages where the concept is supported.

A friend function is declared by the class that is granting access, explicately stating what function from a class is allowed access. A similar concept is that of friend class.

Friends should be used with caution. Too many functions or external classes declared as friends of a class with protected or private data may lessen the value of encapsulation of separate classes in object-oriented programming and may indicate a problem in the overall architecture design. Generallly though, friend functions are a good thing for encapsulation, as you can keep data of a class private from all except those who you explicately state need it, but this does mean your classes will become tightly coupled.

Click on picture to see some C++ using XCode programming showing a Friend Function in use changing the private data members of two classes to move the centre point of a circle by a passed offset in X and Y.

Link to some downloadable code … rename to main.cpp for use.

Link to Friend Function information … from Wikipedia from which quote above comes.


Previous C++ XCode OOP Composite Objects Tutorial below:

C++ XCode OOP Composite Objects Tutorial

C++ XCode OOP Composite Objects Tutorial

Here is a tutorial that introduces you to some aspects of OOP … oops, forgot to say what OOP stands for … Object Oriented Programming.

Object-oriented programming (OOP) is a programming paradigm that represents concepts as “objects” that have data fields (attributes that describe the object) and associated procedures known as methods. Objects, which are usually instances of classes, are used to interact with one another to design applications and computer programs.

To me, the beauty of OOP is that it can relate to the nouns in our life. Think of an object that you can touch, like a wristwatch … it has features like its type of design, its size, its weight (ie. its properties), and it has objects that go to make it up, like cogs and wheels (ie. its components), and it has its related classes of objects like clock, alarm clock, oven timer etcetera (ie. its characteristics), and its more fundamental essence, the class of watch (ie. its inheritance).

In this tutorial we touch on the “components” aspect of an object circle, whose class we are going to call acircle, and we will introduce a component class of point as a component of a circle. This is the tip of the iceberg of discussing objects and OOP, but is, perhaps, the easiest concept to start getting your head around how good OOP techniques can be for imagining what your programming should achieve.

By the way, do as I say rather than I do … when you have a classname give it a capital letter. You’ll see why when you program a lot … was a bad male human and didn’t do that in this tutorial. Non-snap. Non-snap. Non-snap. Somehow, don’t think non-snap will catch on quite as well!

Let’s see some C++ using XCode for this …

Link to Object Oriented Programming information … from Wikipedia from which quote just above comes.
Link to Object Oriented Programming inventor information … Kristen Nygaard.
Link to some downloadable code … rename to main.cpp for use.

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, OOP, Tutorials, Xcode and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>