Posts

Showing posts from December, 2012

Basic concepts in Oops - 2

Hi everyone, this is the continuation of the previous post. Here i am discussing some more important concepts in oops. 6) Destructor Destructors are invoked automatically when an object goes out of scope or when delete operation is used to destroy a dynamically created object. The method "destructor" is used in C++ to clean up an object's state. Garbage collector does the job of destroying unused objects in Java. 7) Inheritance Inheritance is the property by which objects of one class can access the properties of other class. There are two types of inheritance - single inheritance and multiple inheritance. In single inheritance one or more class inherit from a single base class. In multiple inheritance one or more class inherits from more than one base class. Following shows an example of inheritance. class Vehicle { int vehicleId; int milage; Vehicle(int a, int b) { vehicleId=a; milage=b; } } class Car extends Vehicle { int yearOfManufacture; Car(int a, int

Basic Concepts in Oops - 1

This blog post will teach about the basic concepts in Object oriented programming. I have written this post in the simplest possible way. 1) Object Objects are primary run-time entities in an object - oriented programming. Every objects has its own features and properties. Objects occupy space in memory. An object refers to an instance in class. 2) Class Class is a group of objects that have identical properties. Objects are variables of class type. A class is actually a model and a true specimen of object. In other words, class is a blueprint from which objects are created. Every object is defined using a class. public class Vehicle { int vehicleId; int mileage; int cost; } In the above example, Vehicle is a class and vehicleId, mileage and cost are its attributes. Vehicle vehicle = new Vehicle(); Here, vehicle is an object of the Class. 3) Instantiation The process of creating an object from the class is called instantiation. An object is the instance of t

Program to check whether a system is big-endian or little-endian

Hi everyone, this post will teach you how to check whether a system is big-endian  or little-endian. I have written the program in one of most simplest way. The program is very easy, provided you know what is meant by Endianness. Endianness defines the order in which system stores the bytes of a multibyte value. Endianness includes big-endian and little-endian systems. Big-endian system stores the Most significant byte (MSB) first and little-endian systems store the Least significant byte (LSB) first. Most of the personal laptop and systems which we use now are little-endian types, while most of the Power PC's use big-endian type of storing data. The term "big-endian" came from the famous novel "Gullivers Travels". How to check the endianness of a system Since you understood the concept, implementation won't be that difficult. It is as follows:- Assign num=1 Create a char pointer which point to "num". Check the de-refernced pointer. I

How to find whether 2 strings are anagram or not using java

2 strings are anagrams, if one string is a jumbled form of other string. In technical terms, we can say like, every characters in the first string should appear int the second string and the count of each character should be the same. Eg:- "ajva" is an anagram of "java". But "avaja" is not an anagram of "java" because the latter contains more number of "a". There are many methods to find out whether 2 strings are anagrams or not. One of the method is to sort both the strings and check whether they are equal. Minimum time complexity taken in this method is   O(n log n). The other method is to store the count of each of the characters of the strings in 2 separate maps. And then check the count of each characters. This method will use some extra storage spaces, but the time taken is in O(n). The program is as follows: import java.util.HashMap; import java.util.Map; public class Anagram { public static void

Sets in C++ made easy

This tutorial will helps you in learning about Sets in C++. I have written in the simplest possible way. Like Maps, sets are also a type of associate containers which stores unique elements. The element value in the set is the key itself. Set Sets are associative containers that stores unique elements. All elements are ordered in set in C++. Use Sets are mainly used whenever we want to store unique items of a group or activities relating to unique elements. Eg - Program to print unique words from a file. Defining a Set Sets in java are defined as follows: - set<string> set1; Instead of string, we can use int, float and other data types. Adding an entry to a Set Entry/ Values can be added to set in the following ways: - set1. insert("Sujith"); Checking whether a value is present in the Set Entry/ Values can be checked in the Set for their presence. It is similar to those in Map. We use count for checking whether a value is there or not. The entr