Sunday, March 28, 2010

Art of programming in C++ (part -1)

C++ is a very complicated language. Learning it thoroughly will take years. You must understand this properly.
First book on C++ should be Thinking in C++. This is easy to get as its free. The only problem with this book is it doesn't tell you how to create abstractions in C++.
But learning a programming language is like learning to speak. First you learn to speak and then make it proper by learning the rules of language. So you must program also. Thinking in C++ doesn't give you that idea. So you must program.
In my experience people find operator overloading as tough part.
Why its tough? I think because of its syntax? Its hard to remember the rules
So when you learn C++ you must program some data structures in it. (For example try implenting link list/tree in C++).(Caution: This will be tough if you are programming in C++ for first time).
It will help you to think logically using C++ as a a language.

A gentle test in C++ to see if you are level 0 inside C++ lanaguage.
Can you write the code of the following:

1. Implement link list in the C++.
2. Implement binary search tree in C++.
3. Implement binary search in C++.

//Design problems.
1. Write a singleton class.
2. Write a factory class.

See if you can spot some errors:

1.
class A
{
public:
virtual void f(){}
};

class B : public A
{
public:
virtual void f(){}

};

void g(A * p)
{
memset(p,0,sizeof(A));
p->f();
}

Any error has been observed?

2. Write a code which stops object from being copied.
(Hint: Inside member functions also)

3. Write a code which stops the object construction if an event has occured in the network.
(Hint: You can know about the event from a function which is given. But there shouldn't be any leak in code).

More about this in next blog.

No comments:

Post a Comment