Effective C++ Ed. 3rd: Item 22. Declare data members private


Declare members private for :
  1. Syntactic Consistency: Every call-able member would be a function.
  2. Granular Accessibility: Using "setter" and "get" functions
  3. Encapsulation:
    • Implement functions the way it suits best. For example: On embedded devices: it must use less memory whereas on other machines, it may be allowed to take more memory to speed it up more.
    • Private data members put more restriction on their interaction with the users as compared to protected and public data members. So removing private data member (to change some implementation) leads to less broken code relatively.
prev | next
Cheers and try hosting at Linode.

Ubuntu Server: How to list all the groups and users

Linux is a multi-user operating system.  This means that the administrator will have to be careful in how users are managed. List of users is maintained in /etc/passwd along with more information like the group the belong to etc. To view its contents, run: cat /etc/passwd
The each line of output is like:
www-data:x:33:33:www-data:/var/www:/bin/sh
Here there are 7 items mentioned below in order from left to right:

Effective C++ Ed. 3rd: Item 21. Don't try to return a reference when you must return an object

  1. Remember not to return reference to a local object/variable in any function. As local variable/objects dies as soon as function ends.
  2. Remember not to return a dynamically allocated object (using new operator). User of the function may forget corresponding delete call.
  3. Remember not to return reference to a local static object/variable in any function say f().
    In this case f(obj1, ...) = = f(obj2, ...) will always comes true.
  4. Instead return the object by value.
prev | next
Cheers and try hosting at Linode.

Effective C++ Ed. 3rd: Item 20. Prefer pass-by-reference-to-const to pass-by-value

  • For User-defined types(yes, for even small ones) Instead of
    Way 1:
    returnType FunctionName(ArgType1 arg1, ...);

    use
    Way 2:
    returnType FunctionName(const ArgType1& arg1, ...);

    This would reduce the cost of calling copy constructor and destructor for each argument passed as shown above.
  • If a derived object of class ArgType1 is passed by Way1, the copy the called function will get will contain only information defined in Base Class ie. ArgType1 here due to Object Slicing. It can be prevented using Way2.

Note: Iterators and function objects of STL and build-in types are better off passed by value due to their design.
prev | next
I hope this helps
Cheers and try hosting at Linode.
Monish

Effective C++ Ed. 3rd: Item 19. Treat Class Design As Type Design

What all you must consider to provide in a class you write? Check the following:
  1. Constructors and Destructor as well as memory allocation and deallocation fuctions.
  2. Object Initialization vs. Object Assignment.
  3. Copy constructor to pass objects by value.
  4. Error checking for object values outside legal range allowed, specially in constructors, assignment operators and "setter" functions.
  5. Declare virtual functions and virtual destructor depending upon: if you want to allow others to inherit this class.
  6. Type Conversion functions and constructors.
  7. Provide required functionality by defining functions and Overloaded operators.
  8. Disallow suitable standard funtions. (COPY CTOR etc.)
  9. Use access specifiers depending upon who will be allowed to access particular members.
  10. What kind of guarantee does the class provide in terms of exception safety, resource usage?
  11. Make your class generic using class template if  required.
  12. Is this class is what you need? Can't this be achieved through a function?
prev | next
Hope this helps
Cheers and try hosting at Linode.
Monish

Strong Passwords

A strong password is defined as any password which meets the following criteria:
  • At least fifteen (15) characters in length.
  • Does not contain your user name, real name, organization name, family member's names or names of your pets.
  • Does not contain your birth date.
  • Does not contain a complete dictionary word.
  • Is significantly different from your previous password.
  • Should contain three (3) of the following character types.
    • Lowercase Alphabetical (a, b, c, etc.)
    • Uppercase Alphabetical (A, B, C, etc.)
    • Numerics (0, 1, 2, etc.)
    • Special Characters (@, %, !, etc.) 

Examples

Google: Ranking System

Google maintains much more information about web documents than typical search engines. Every hitlist includes position, font, and capitalization information. Additionally, they factor in hits from anchor text and the PageRank of the document. Combining all of this information into a rank is difficult. They designed the ranking function so that no particular factor can have too much influence. First, consider the simplest case -- a single word query. In order to rank a document with a single word query, Google looks at that document's hit list for that word. Google considers each hit to be one of several different types (title, anchor, URL, plain text large font, plain text small font, ...), each of which has its own type-weight. The type-weights make up a vector indexed by type. Google counts the number of hits of each type in the hit list. Then every count is converted into a count-weight. Count-weights increase linearly with counts at first but quickly taper off so that more than a certain count will not help. We take the dot product of the vector of count-weights with the vector of type-weights to compute an IR score for the document. Finally, the IR score is combined with PageRank to give a final rank to the document.

Secure hosted files

Operating System: Ubuntu Server 10.04
Web Server: apache2
As apache2 accesses file as user: www-data and group: www-data, First change the owner using following command:
chown www-data: path/file
and then to making a particular file only readable only to user:www-data
chmod 500 path/file
For scripts and images or logo use 500 (r _ x   _ _ _   _ _ _)
For images which you only want to save but don't want them to accessible to users/www-data, use
300 (_ w x   _ _ _   _ _ _)

Remember to give necessary access to the user which updates the hosted files. Another alternative to secure files is Apache Directives and Containers in http.conf of apache server.

Hope this helps
Cheers and try hosting at Linode.
Monish


Hope this helps
Cheers and try hosting at Linode.

NP = P?

Computer science is largely concerned with a single question: How long does it take to execute a given algorithm? But computer scientists don’t give the answer in minutes or milliseconds; they give it relative to the number of elements the algorithm has to manipulate.
P denotes set of problems which can be solved in polynomial time i.e. having solution times proportional to O(n raise to k), k is any constant. Whereas, NP is class of problems whose solution can be verified in polynomial time and its solution times for some inputs might go near exponential times or so...
The question “Does NP equal P?” means “If the solution to a problem can be verified in polynomial time, can it be found in polynomial time?”
Click here to read more...

P, NP, and NP-Completeness: The Basics of Computational Complexity

Java Program to illustrate - changing contents within the same JFrame object

This can be helpful when one need to make a GUI based java software. When you need that the contents of the same frame object get replaced by contents of another container/panel when a user clicks on the say, 'NEXT' button. Following program will give you hints about how you could do that..
Program Name: GUItest.java
import javax.swing.*;
public class GUItest extends JFrame
{
    GUItest()
    {
        setSize(300, 200);
        setVisible(true);
//        setLayout(new GridLayout(1,1));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add((new JPanel()).add(new JButton("Hello")));
        this.getContentPane().removeAll();
        this.add((new JPanel()).add(new JButton("World!!")));
    }
    public static void main(String args[])
    {
        GUItest frame1 = new GUItest();
    }
}