Showing posts with label Programming Concepts. Show all posts
Showing posts with label Programming Concepts. Show all posts

Wednesday, January 22, 2014

CODE SNIPPET: Exception Class in C++

Here is a code snippet on how to create an exception class in C++

#include <exception>

class E : public exception {
const char * msg;
E(){};
public:
E(const char * s) throw() : msg(s) {}
const char * what() const throw() { return msg; }

};

Wednesday, June 5, 2013

When to use Inheritance, Composition, Interface, Aggregation

Composition

To identify composition associations, look for classes that cannot exist without other classes. Look for "wholes" and "parts". Ask, "Is this class part of another class?". The key question to ask is - Is this class destroyed when another class is destroyed?

Aggregation

The key difference between an aggregation and a composition is, in an aggregation, the part is not destroyed when the whole is destroyed. The part may be independent of the whole but the whole requires the part. For eg: A car class would require an engine class, chassis class. To identify aggregation, ask - Is this class part of another class and is it independent of the other class?

Inheritance

Inheritance provides the mechanism for new classes to be formed from the attributes and behavior of exisitng classes. Inheritance is best suited for relatively shallow class hierarchies. Use Inheritnace when:
  • The inheritance hieracrchy represents an is-a relaltionship and not a has-a(composition/aggregation) relationship.
  • The same behavior is applied to different data typoes.
  • The class hierarchy is reasonably shallow, and unlikely to become much deeper over time.

Interface

Like an inherited classm an interface provides a common specification for behavior, but, uunlike an inherited class, an interface cannot be created. The behavior has been abstracted out my any particular class and specifically implemented by all classes that need it.  Use interface inheritance when:
  • Unrelated object types need to provide the same behavior
  • Multiple inheritance is needed.
  • Inheritance is prohibited. For example C# structures cannot inherit from classes, but can implement interfaces.

Thursday, May 30, 2013

Resources for programmers and coders

I found these resources helpful while learning how to code:

  1. When to use classes? This article explains concepts of class, when to create a class, inheritance, composition and aggregation. It provides a visual approach and goes step-by-step from easy to harder concepts.
  2. C# Cheat Sheet: I made a cheat sheet when I was learning C#. It helped me remember common syntax, some important concepts and contains code snippets for a memory refresh.
  3. Learn something new: This website has tons of resources ranging from concepts on algorithms to practicing languages such as C# .You are sure to find something new to learn or brush up your skills http://www.codeproject.com/script/Content/SiteMap.aspx
  4. http://www.experts-exchange.com/tutorials/ tons of free tutorials.

Search This Blog