C++.. PART4

Ok … so let’s go further becasue it seems that this was a quite long-time approach. Anyway I don’t want to wander into useless details but on the other hand I don’t want to miss something that could be important.

18. Class vs Struct – this will be just a discussion about C structs and C++ classes, so emphasizing the main aspects of object oriented programming.
C structures are the part of C language which makes the transition to C++ and object oriented programming. In C they were designed to be more some data
containers, that’s why in C structs you cannot add functions (if it is to take into consideration differences between C++ structs and C++ classes only
the default access type will be considered: public for struct, private for class). When defined a C++ class does not take memory storage, it happens
only when it is instantiated.

But how things are really passing concerning classes’ declaration, definition and instantiation? Basically classes’ definitions are kept in header files and their
declarations and instantiations in source files, in other words everything that does not have an active role in programs’ execution is kept in headers and the program itself, the part that is indeed executed is kept in .c files. This is not mandatory, this is not requested by the standard, but is just a guideline.

Also a frequent practice in designing classes is to declare its members as private and corresponding functions, which access them as public. Actually
this is the very nature of encapsulation, classes’ characteristics are not visible outside of the class but can be modified from within by some functions
which are accessible from outside.

19. Constructors, Destructors (actually only one destructor may exist) – all of them can be found in C++ structs, classes and unions. C++ structs and classes are same thing, with the mention about the default type of access, but unions do not participate in inheritance.

Like I read in a “C++ guide for dummies” 🙂 in the chapter dedicated to constructors and destructors, it is nice to have some guests for a dinner but there is always some work to be done before they arrive, like preparing the meals, preparing the table, and some afterwards, cleaning the dishes, cleaning the table.

Same applies for classes and their objects. Some work needs to be done for object initialization and some work when this is distroyed and as programmers are lazyer than any existing housewife 🙂 , they really want to have “someone” to handle those things.

Constructor is the place where an object is initilized (Attention ! constructor does not allocate memory ! this is quite frequent mistake), everytime an object of a certain class is created (statically, only by invoking class name, or dinamically, using new) corresponding constructor is called … and everytime when an object is destroyed its corresponding destructor is called.

Below there is a list with some of their (constructors and destructor) characteristics:
* Constructors are just functions with the same name as the class.
* Constructors are intended to initialize the members of the class when an instance of that class is created.
* Constructors are not called directly (there are few exceptions but generally they can’t be called directly)
* Constructors are never virtual.
* Multiple constructors for the same class can be defined. They must have different parameters to distinguish them, but they have no return type

* Destructors have the same name as the class but prefixed with the sign ~ (bitwise negation)
* Only one destructor may exist for a class
* Destructors have no return type
* Destructors may be declared virtual

20. Inheritance

As it obviously sounds: some classes can be of a certain type, already defined. Just take the stupid example with the Car class. Generally all cars have some thing in common, wheels, chassis, windows, doors and so on. Some specific types of cars have more characteristics than the common ones. Their manufacturers want to enhance them with more things like ABS system, or a different type of gearbox or any kind of navigation system or cruise control features, in order to gain a bigger market slice. All of them inherit the general characteristics of a car but, in addition they are plugged in with more sophisticated and specific stuff. This is what inheritance is all about: a child class gets something from parent class but in addition it can have more things.

When a class inherits another one it is said that this one is derived from a base class, so usually the derived class is bigger than the base one. Derived class implicitly contains the members and methods from base class. There are three ways in which a class cn inherit, those correspond to the access modifiers: public (everything coming from the base class is visible in the derived class), protected, private … those will be discussed a little bit more in depth at #22.

21. Access modifiers Of course you all know about the three access modifiers: Public (everyone can see it and access it), Protected (some kind of public but only for derived classes) and private (cannot see it and access it outside class scope, available only for internal members)

22. Private inheritance needs to be treated separately because is slightly different from the common used and encoutered public inheritance. A good thing to keep in mind is also the fact that this is the default inheritance type, if you forget to write something after

class Derived: Base

… then this is private inheritance.

The idea is that a method (or member) which was declared public or protected in the base class becomes private in the derived class. What is this meaning? You can use it only in the derived class but nowhere else. That method or member does participate in the child class creation but is hidden from the outside world.

When bases classes are privately (or protected) inherited, there is not a “is-a” relationship in between child and parent classes but rather a “is an implementation of“. Also the Derived type is not a sub-type of the Base type. All I can say about this is that it is used when you want to mask or somehow to override some things from the Base class. There could be such situation that you do not want to inherit something and you do not want your derived objects to be exclusively from a base type.

Enough for today … hope that was an interesting lecture and you discovered some things which you didn’t knew before.


One Response to C++.. PART4

  1. After reading you site, Your site is very useful for me .I bookmarked your site!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: