Posts Tagged ‘class’

Function overloading by return type in C++

Commercial modern day languages often get criticized because they don’t support function overloading by return type. That is to say, functions cannot be distinguished by return type. For example:

int myfunction(int a, int b); float myfunction(int a, int b); char myfunction(int a, int b);

Are not valid. That is kind of bad, because the possibility to overload by return type often improves the quality of code.

But why can’t we overload by return type? Because the return type is not a part of a function’s signature. However, function overloading by type is not only possible in C++, it is actually pretty easy too.

Enter the cast operator. Remember this guy?

class C{ public: operator int() {…} };

“That’s cool, but it cannot be passed arguments!” you may say. True, but we don’t need it to be passed arguments, we can pass arguments using the constructor. Let’s review that with a simple example:

#include <iostream> using namespace std; class TestFunction{ private: int ia, ib; public: TestFunction(int a, int b): ia(a), ib(b){ } operator int(){ return ia * ib; } operator float(){ return (float)ia / (float)ib; } }; int main(){ int int_res = TestFunction(1, 13); //13 float float_res = TestFunction(1, 13); //0.0769231 cout << int_res << endl << float_res; return 0; } Output: 13 0.0769231

We should note, that an essential part of our program is the way we call the “function” like this:

float float_res = TestFunction(1, 13); //0.0769231

If we, for example call:

cout << TestFunction(1, 13);

we will get a compilation error, stating that the <<operator is ambiguous. Because the << operator expects a type from his right-side argument and our TestFunction expect a type from its left-side. Naturally an expression like:

cout << (float)TestFunction(1, 13);

is perfectly valid.

Of course this approach of overloading by return type, can be combined with the ordinary overloading by signature, by simply introducing a little more advanced logic in our constructor (overloading it, saving which constructor was called,  etc.).

VN:F [1.9.18_1163]
Rating: 0.0/10 (0 votes cast)

Learn the Basics of Floating in CSS

The float declaration is used to put elements beside one another such as several div’s. A common technique is for, say a blog, you have your content, and you have your sidebar on the right. Each one of those is their own container or div. To make them line up against each other rather than having the content on the top and the sidebar below it we need to float these div’s. Let’s start out by setting up the divs, with some dummy content we may see in a traditional blog.

So what this html is saying is that we have a container (the div “wrapper”) which has two divs inside of that. A left content div and a right content div. The left content has some dummy content and the right has an unordered list with some links. Lastly we have a div with a clear class, this will stop any elements from popping up next to floated elements Let’s modify this with some CSS.

We give our wrapper a width and center it. Next we set the width of our .leftcontent div and give it a little padding. Next we float that div to the left. This means that it will tell all other divs to ignore any undefined space around it. So since the width of .leftcontent is only 770px (compensate for padding) the .rightcontent div will pop up and line up horizontally with .leftcontent because it’s width is 210px which is less than the wrappers width which they are both contained in. Lastly we must clear the float so that no other elements try to pop up next to the divs that we want to float. The clear class is fairly straightforward, it is clearing the float so that nothing else “floats” up on the page. For this example I cleared left, you could also use the both value which would make your clear class more universal but is not needed for this example. Once you have done this your page should look similar to this.

Read the rest of this entry »

VN:F [1.9.18_1163]
Rating: 0.0/10 (10 votes cast)

How to write equals method in Java

When I started Java i heard that Java is truly object oriented language and everything in JAVA is object, later I found that though its not completely true but yes most of things are in Java is represented via OOPS concept. One of them is Introduction of java.lang.Object class and every class in Java extends this class by default. So when I started my blog and writing about my JAVA experience as Core JAVA Tutorials I thought lets write something which helps beginners in JAVA.

Object class holds some very interesting method which is applicable at object level and one of them is equals () which we are going to discuss in this article. equals () method is used to compare the equality of two object. default implementation of equals() class provided by Object class compares memory location and only return true if two reference variable are pointing to same memory location i.e. essentially they are same object. JAVA recommends to override equals method if equality is going to be define by logical way or via some business logic and some classes in Java standard library does override it e.g. String class whose implementation of equals() method return true if content of two strings are exactly same. you can read more about String in my post Why String is immutable in Java Since HashMap of JAVA relies on equals () and hashcode () method for comparing keys and values, java has provided a contract to follow while overriding equals () method which state that

1) if two objects are equal by equals method then there hashcode must be same.
2) if two objects are unequal by equals method then there hashcode could be same or different.

You can read more about HashMap in my post Difference between Hashtable and HashMap and How HashMap works in Java .

So this was the basic theory about equals () method in JAVA now we are going to discuss the approach on how to override equals () method, yes I know you all know this stuff :) but I have seen some of equals () code which can really be improved by following correct. For illustration purpose we will see an example of Person class and discuss How to write equals () method for that class.

Here is my approach for writing equals() method: Read the rest of this entry »

VN:F [1.9.18_1163]
Rating: 0.0/10 (10 votes cast)

Using namespaces in C++

Namespaces can be considered as abstract containers that hold different entities – functions, variables, classes and objects. Since C++ provides a single global namespace, without the use of user defined namespaces the chances of global name clashes is pretty big.
Consider the following situation.
We wish to use to different libraries and include them like this: Read the rest of this entry »

VN:F [1.9.18_1163]
Rating: 0.0/10 (10 votes cast)

Creating a type-safe list by using a generic collection in C#

Safe types are the key to creating code which is easy to maintain. The language and the framework find most of the bugs during compilation (and not run-time after the product has been delivered). In versions previous to .NET 2.0 almost all of the collections were an object of type System.Object and because everything derives System.Object those collections couldn’t have any type, which meant they were not type-safe.
.NET 2.0 gives us an easy way to create type-safe lists by using a new library of collections which you can find in the namespace System.Collections.Generic. Generic collection means a collection, which allows you to specify its members’ type at the time of its declaration. After the generic collection has been declared, the compiler will allow only objects of the specified types to be inserted in it. The generic collection declaration has this general form:

List<type> name;
Unlike collections in C# 1.x, there is no need to use casts when accessing the objects in the generic collections and you are guaranteed that no object that cannot be casted to the specified type which will be held in the collection can be inserted in it. This makes your code safer, easier to maintain and simpler to use.
Let’s review the following example, which will give us a good idea of the way generic collections work: Read the rest of this entry »

VN:F [1.9.18_1163]
Rating: 0.0/10 (0 votes cast)