Showing posts with label Object Oriented Programming. Show all posts

How to increase your programming skills?

Today in this article I will be sharing how to increase your coding skills. If you are wondering how you can become good at programming, you are at the correct page. So, lets begin the count down. So, lets begin the programming tips one by one.
  1. Format the code correctly: Learn to format your code correctly. If you are using any professional IDE like eclipse, you have to learn to format code correctly. In eclipse, press CTRL  + SHIFT + F. 
  2. Ensure braces always pair up: Make sure all your code braces pair up correctly. To ensure you avoid any error, immediately close your brace as soon you begin it. Some IDE does this for you automatically. 
  3. Build programs one step at a time: Try to build your program in smaller version. Do not try to complete the software at once. Try and run something beginner version and later elaborate your code.
  4. Google like crazy: Try to GOOGLE any problem you encounter. Try to copy and paste your error in Google. Dont forget to remove the line number from the error. 
  5. Smallest working program as possible: Try to solve any problem in smallest possible code. Do not try to elaborate your code. Smaller code can be better than writing same code in longer format. 
  6. Read stack traces from the top line down: When you debug your program, try to solve the problem from top to bottom. Bottom error might be evolved from top error. 
  7. Write software that interests you: Programming is monotonous job. Try to code what interests you. 
  8. Type rather than read: When you are reading any coding tutorial, try to code, run and implement than reading or writing code into your note. 
  9. Name variables, functions, subroutines descriptively: Show professionalism in writing name of variables. Instead of writing any string variable a for Name would be less descriptive than writing "Name" as variable. 
  10. Learn to touch type: If you can code the program without looking at your keyboard, that would make you a better programmer than looking at the keyboard. 
Learn more »

What do you mean by friend class and friend function?

Friend function and friend classes.
Private member of a class can not be accessed from outside the class. Non member function of a class cannot access the member of class. Using friend function we can achieve this. A fried function also acts as a bridging between two class. It can operate the object of two classes.
 Friend function is declared inside the class any where in private or public section with keyword friend.
 Friend function are defined outside the class as a normal function.
An example: Friend function operating objects of two classes.
#include<iostream.h>
class beta ;
class alpha
{
private:
int data ;
public:
alpha() { data=10;}
friend int frifunc(alpha ,beta);
};
class beta
{ private:
int data;
public:
beta() { data = 20; }
friend int frifunc(alpha, beta);
};
int frifunc(alpha a, beta b)
{ return (a.data + b.data);
}
void main ( )
{
alpha aa;
beta bb;
cout<<frifunc(aa, bb)<<endl;
}
output: 30
Friend class:
When a class is to be made friend to another class, we should declare that class as friend inside another. as
class A class B
{ // body of A { // Body of B
friend class B;
}; };
Here class B is friend class to A.
 When a class is declared as friend within another class entire function of friend class can access the private data of the class to which it becomes friend.
 Here in above example, all member function of B can access private data of class A.
Another way of declaration
class B;
class A
{
// Body of class A
friend B;
};
class B
{
// Body
};
//An Example:
#include<iostream.h>
class A
{
private:
int data;
public:
A(){data = 100;}
friend class B;
};
class B
{
public:
void func1(A a)
{cout<<"\ndata= "<<a.data;}
void func2(A b)
{
cout<<"\ndata = "<<b.data<<endl;
}
};
void main()
{
A a;
B b;
b.func1(a);
b.func2(a);
}
The output: data= 100
data = 100
Learn more »

Describe Polymorphism in object oriented programming

Polymorphism refers to "one name but different forms"
– Polymorphism is implemented by function over loading and operator overloading
– Giving same name to functions with different no or types of arguments
– This information is known to compiler at compile time and compiler is able to select the appropriate function at call time
– It is called compile time polymorphism-Early binding.
# If appropriate member function are chooses at run time rather than compile time, this is
known as runtime polymorphism or Late binding.
# For this mechanism we chose member function as virtual.
In C++, polymorphism indicates the form of a member function that can be changed at runtime. Such member functions are called virtual functions and the corresponding class is called polymorphic class. The objects of the polymorphic class, address by pointers, change at runtime and respond differently for the same message. Such mechanism requires postponement of binding of a function call to the member function declared as virtual until runtime.


Learn more »

Differentiate between Overloading and Overriding


OVERLOADING VS OVERRIDING
OVERLOADING
OVERRIDING
·         overloading is a compile time polymorphism.
·         overriding is a runtime polymorphism.
·         it means “add” more behavior.
·         it means “change” existing behavior.
·         in overloading the parameters should be unique i.e. the number of parameters should differ or the type would be different. This is because compiler should know the methods in compile time only.
·         In overriding, we re-implement or change the functionality of the base class method in derived class the number of parameter and return type should be same.
·         They appear in the same class or a subclass.
·         It appears in subclass.
·         There is relationship between methods available in the same class.
·         There is relationship between a superclass method and subclass method.
·         Overloading does not block inheritance from the superclass.
·         Overriding blocks inheritance.

Learn more »