Tuesday, October 23, 2007

C++ notes

1. What is the output of printf(“%d”)?

1. %d helps to read integer data type of a given variable
2. when we write (“%d”, X) compiler will print the value of x assumed in the main
3. but nothing after (“%d”) so the output will be garbage
4. printf is an overload function doesnt check consistency of the arg list – segmentation fault


2. What will happen if I say delete this? - destructor executed, but memory will not be freed (other than work done by destructor). If we have class Test and method Destroy { delete this } the destructor for Test will execute, if we have Test *var = new Test()

1. pointer var will still be valid
2. object created by new exists until explicitly destroyed by delete
3. space it occupied can be reused by new
4. delete may only be applied to a pointer by new or zero, applying delete to zero = no FX
5. delete = delete objects
6. delete[] – delete array
7. delete operator destroys the object created with new by deallocating the memory assoc. with the object
8. if a destructor has been defined fir a class delete invokes that desructor


3. Difference between C structure and C++ structure - C++ places greater emphasis on type checking, compiler can diagnose every diff between C and C++

1. structures are a way of storing many different values in variables of potentially diff types under under the same name
2. classes and structures make program modular, easier to modify make things compact
3. useful when a lot of data needs to be grouped together
4. struct Tag {…}struct example {Int x;}example ex; ex.x = 33; //accessing variable of structure
5. members of a struct in C are by default public, in C++ private
6. unions like structs except they share memory – allocates largest data type in memory - like a giant storage: store one small OR one large but never both @ the same time
7. pointers can point to struct:
8. C++ can use class instead of struct (almost the same thing) - difference: C++ classes can include functions as members
9. members can be declared as: private: members of a class are accessible only from other members of their same class; protected: members are accessible from members of their same class and friend classes and also members of their derived classes; public: members are accessible from anywhere the class is visible
10. structs usually used for data only structures, classes for classes that have procedures and member functions
11. use private because in large projects important that values not be modified in an unexpected way from the POV of the object
12. advantage of class declare several diff objects from it, each object of Rect has its own variable x, y AND its own functions
13. concept of OO programming: data and functions are properties of the object instead of the usual view of objects as function parameters in structured programming


4. Difference between assignment operator and copy constructor

1. constructor with only one parameter of its same type that assigns to every nonstatic class member variable of the object a copy of the passed object
2. copy assignment operator must correctly deal with a well constructed object - but copy constructor initializes uninitialized memory
3. copy constructor takes care of initialization by an object of the same type x
4. for a class for which the copy assignment and copy constructor not explicitly declared missing operation will be generated by the compiler. Copy operations are not inherited - copy of a class object is a copy of each member
5. memberwise assignment: each member of the right hand object is assigned to the corresponding member of the left hand object
6. if a class needs a copy constructor it will also need an assignment operator
7. copy constructor creates a new object, assignment operator has to deal w/ existing data in the object
8. assignment is like deconstruction followed by construction
9. assignment operator assigns a value to a already existing object
10. copy constructor creates a new object by copying an existing one
11. copy constructor initializes a freshly created object using data from an existing one. It must allocate memory if necessary then copy the data
12. the assignment operator makes an already existing object into a copy of an existing one.
13. copy constructor always creates a new object, assignment never does


5. Difference between overloading and overriding?

1. Overload - two functions that appear in the same scope are overloaded if they have the same name but have different parameter list
2. main() cannot be overloaded
3. notational convenience - compiler invokes the functions that is the best match on the args – found by finding the best match between the type of arg expr and parameter
4. if declare a function locally, that function hides rather than overload the same function declared in an outer scope
5. Overriding - the ability of the inherited class rewriting the virtual method of a base class - a method which completely replaces base class FUNCTIONALITY in subclass
6. the overriding method in the subclass must have exactly the same signature as the function of the base class it is replacing - replacement of a method in a child class
7. writing a different body in a derived class for a function defined in a base class, ONLY if the function in the base class is virtual and ONLY if the function in the derived class has the same signature
8. all functions in the derived class hide the base class functions with the same name except in the case of a virtual functions which override the base class functions with the same signature


6. Virtual

1. single most important feature of C++ BUT virtual costs
2. allows derived classes to replace the implementation provided by the base class
3. without virtual functions C++ wouldnt be object oriented
4. Programming with classes but w/o dynamic binding == object based not OO
5. dynamic binding can improve reuse by letting old code call new code
6. functions defined as virtual are ones that the base expects its derived classes to redefine
7. virtual precedes return type of a function
8. virtual keyword appears only on the member function declaration inside the class
9. virtual keyword may not be used on a function definition that appears outside the class body
10. default member functions are nonvirtual


7. Dynamic Binding

1. delaying until runtime the selection of which function to run
2. refers to the runtime choice of which virtual function to run based on the underlying type of the object to which a reference or a pointer is based
3. applies only to functions declared as virtual when called thru reference or ptr
4. in C++ dynamic binding happens when a virtual function is called through a reference (|| ptr) to a base class. The face that ref or ptr might refer to either a base or a derived class object is the key to dynamic binding. Calls to virtual functions made thru a reference or ptr are resolved at run time: the function that is called is the one defined by the actual type of the object to which the reference or pointer refers


8. Explain the need for a virtual destructor

1. destructor for the base parts are invoked automatically
2. we might delete a ptr to the base type that actually points to a derived object
3. if we delete a ptr to base then the base class destructor is run and the members of the base class are cleared up. If the objectis a derived type then the behavior is undefined
4. to ensure that the proper destructor is run the destructor must be virtual in the base class
5. virtual destructor needed if base pointer that points to a derived object is ever deleted (even if it doesnt do any work)


9. Rule of 3

1. if a class needs a destructor, it will also need an assignment operator and copy constructor
2. compiler always synthesizes a destructor for us
3. destroys each nonstatic member in the reverse order from that in which the object was created
4. it destroys the members in reverse order from which they are declared in the class1. if someone will derive from your class2. and if someone will say new derived where derived is derived from your class3. and if someone will say delete p, where the actual objects type is derived but the pointer ps type is your class
5. make destructor virtual if your class has any virtual functions


10. Why do you need a virtual destructor when someone says delete using a Base ptr thats pointing to a derived object?

When you say delete p and the class of p has a virtual destructor the destructor that gets invoked is the one assoc with the type of the object*p not necessarily the one assoc with the type of the pointer == GOOD


11. Different types of polymorphism

1. types related by inheritance as polymorphic types because we can use many forms of a derived or base type interchangeably
2. only applies to ref or ptr to types related by inheritance.
3. Inheritance - lets us define classes that model relationships among types, sharing what is common and specializing only that which is inherently different
4. derived classes
   a. can use w/o change those operations that dont depend on the specifics of the derived type
   b. redefine those member functions that do depend on its type
   c. derived class may define additional members beyond those it inherits from its base class.
5. Dynamic Binding - lets us write programs that use objects of any type in an inheritance hierarchy w/o caring about the objects specific types
6. happens when a virtual function is called through a reference || ptr to a base class
7. The fact that a reference or ptr might refer to either a base or derived class object is the key to dynamic binding
8. calls to virtual functions made though a reference/ptr resolved @ runtime
9. the function that is called is the one defined by the actual type of the object to which ref/ptr refers


12. How to implement virtual functions in C

Keep function pointers in function and use those function ptrs to perform the operation


13. What are the different type of Storage classes?

1. automatic storage: stack memory - static storage: for namespace scope objects and local statics
2. free store: or heap for dynamically allocated objects == design patterns


14. What is a namespace?
1. every name defined in a global scope must be unique w/in that scope
2. name collisions: same name used in our own code or code supplied to us by indie producers == namespace pollution
3. name clashing - namespace provides controlled mechanism for preventing name collisions
4. allows us to group a set of global classes/obj/funcs
5. in order to access variables from outside namespace have to use scope :: operator
6. using namespace serves to assoc the present nesting level with a certain namespace so that objectand funcs of that namespace can be accessible directly as if they were defined in the global scope


15. Types of STL containers

Containers are objects that store other objects and that has methods for accessing its elements - has iterator - vector


16. Difference between vector and array

array: data structure used dto store a group of objects of the same type sequentially in memory.

vector: container class from STL - holds objects of various types - resize, shrinks grows as elements added - bugs such as accessing out of bounds of an array are avoided


17. Write a program that will delete itself after execution.

Int main(int argc, char **argv) { remove(argv[0]);return 0;}


18. What are inline functions?

1. treated like macro definitions by C++ compiler
2. meant to be used if there’s a need to repetitively execute a small block if code which is smaller
3. always evaluates every argument once
4. defined in header file
5. avoids function call overload because calling a function is slower than evaluating the equivalent expression
6. it’s a request to the compiler, the compiler can ignore the request

19. What is strstream?

Defines classes that support iostreams, array of char obj


20. Passing by ptr/val/refArg?

1. passing by val/refvoid c::f(int arg) – by value arg is a new int existing only in function. Its initial value is copied from i. modifications to arg wont affect the I in the main function
2. void c::f(const int arg) – by value (i.e. copied) the const keyword means that arg cant be changed, but even if it could it wouldnt affect the I in the main function
3. void c::f(int& arg) - -by reference, arg is an alias for I. no copying is done. More efficient than methods that use copy. Change in arg == change in I in the calling function
4. void c::f(const int& arg) - -by reference, int provided in main call cant be changed, read only. Combines safety with efficacy.
5. void c::f(const int& arg) const – like previous but final const that in addition the function f cant change member variables of cArg passing using pointers
6. void c::f(int *arg) – by reference changing *arg will change the I in the calling function
7. void c::f(const int *arg) – by reference but this time the I int in the main function cant be changed – read only
8. void c::f(int * const arg) – by reference the pointer arg cant be changed but what it points to (namely I of the calling function) can
9. void c::f(const int * const arg) by reference the pointer arg cant be changed neither can what it points to


21. Mutable keyword?

1. keyword is the key to make exceptions to const
2. mutable data member is allowed to change during a const member function
3. mutable data member is never const even when it is a member of a const object
4. a const member function may change a mutable member


22. Difference between calloc and malloc?

1. malloc: allocate s bytes
2. calloc: allocate n times s bytes initialized to 0


23. Difference between printf and sprintf?

1. sprintf: a function that puts together a string, output goes to an array of char instead of stdout
2. printf: prints to stdout


24. map in STL?

1. used to store key - value pairs, value retrieved using the key
2. store data indexed by keys of any type desire instead of integers as with arrays
3. maps are fast 0(log(n)) insertion and lookup time
4. std::mapEX:Std::map grade_list //grade_list[“john”] = b


25. When will we use multiple inheritance?

1. use it judiciously class
2. when MI enters the design scope it becomes possible to inherit the same name (function/typedef) from more than one base class == ambiguity
3. C++ first identifies the function thats the best match for the call
4. C++ resolves calls to overload before accessibility, therefore the accessibility of Elec_Gadget() checkout is never evaluated because both are good matches == ERROR
5. resolve ambiguity mp.Borrowable_Item::checkOut(); mp.Elec_Gadget::checkOut(); //error because trying to access private
6. deadly MI diamond: anytime you have an inheritance hierarchy w/ more than one path between a base class and a derived classEX:FileInput File Output FileIOFile//File and IOFile both have paths through InputFile and OutputFile


26. Multithreading

C++ does not have a notion of multithreading, no notion of concurrency


27. Why is the pre-increment operator faster than the post-increment operator?
pre is more efficient that post because for post the object must increment itself and then return a temporary containing its old value. True for even built in types


Programming Ring - New Post
Programming Ring - Old Post

Wednesday, October 10, 2007

Founder of Arsenal FC

I am big fan of Arsenal FC since 1997 - the year they started showing EPL in India. My daily routine is to go online on google news and read stories/news about Arsenal. I came across a very interesting article about founder of arsenal.

Below is complete reproduction of article without any modification. All the thoughts/words are of the original writter. I have put a link for people interested in reading the original article.

***********************************************

LAST month, as Thierry Henry and his new Barcelona team-mates held court at St Andrews, down the road in Burntisland, with rather less fanfare, another seminal figure in the Arsenal story was being accorded an overdue and much deserved tribute.

The Frenchman may not be too familiar with the name of David Danskin but he, and every other player who has donned a Gunners shirt down the years, owes Danskin a debt of gratitude. The Fifer is the man who laid the foundations of the London club, 121 years ago, and the first in the line of a little-known football dynasty that continues to this day.

On July 23, with former Arsenal and Scotland keeper Bob Wilson in attendance, Danskin's role as Arsenal's founder was formally acknowledged with the unveiling of a commemorative plaque near the site of the tenement where he grew up.

Danskin was born in 1863, the son of an iron-turner. His family had moved down the coast from Kirkcaldy to Burntisland following the opening of the Edinburgh and Northern Railway in 1847, when the town had become a major ferry port and railway terminus.

Like Lanarkshire and Ayrshire, Fife was already a strong football hotbed, and the source of many talented players who would later move south. As a teenager, Danskin captained a Kirkcaldy Wanderers team that featured two other future Arsenal players, Peter Connolly and Jack McBean.

In 1883, aged 20, he gave up his engine fitter apprenticeship in Kirkcaldy and moved to Kent, where he had been offered a job at the Woolwich Arsenal munitions factory.

Association football was viewed by many with suspicion in the rugby-dominated south of England, but Danskin was a determined evangeliser for the round-ball game. After several failed attempts, in October 1886, he persuaded a group of like-minded colleagues to club together and buy a football. It was the first important step towards the formation of Dial Square, named after one of the workshops in the factory, and the earliest incarnation of the team that would eventually become Arsenal.

The club was officially formed on December 1, 1886 at meeting at the Royal Oak public house in Woolwich, with Danskin and his fellow members agreeing that it should be rechristened Royal Arsenal. Ten days later, with full-back Danskin named captain, the team played its first ever fixture on a bog of a pitch on the Isle of Dogs in London's East End, thrashing Eastern Wanderers 6-0. Lining up behind Danskin was fellow founder member, Fred Beardsley, who doubled up as keeper for Nottingham Forest. Beardsley had managed to borrow some old shirts from his other club, which is why, to this day, Arsenal, like Forest, play in red.

Danskin featured regularly for the next three years With the team still making its mark, there was no silverware, but he was part of the team that reached the semi-final of the London Association Senior Challenge Cup in 1889, losing to local rivals Clapton Wanderers.

Plagued by injuries, Danskin reluctantly decided to stop playing later that year. However, he made a one-off return to the team in March 1890 as makeshift goalkeeper, standing in for Beardsley, who was away playing for Nottingham Forest. His passion for the game did not wane, and he remained involved as a coach, and even occasional referee. In June 1892, by which point the club had changed its name to Woolwich Arsenal and turned professional, he was elected on to the club's committee.

According to the 1901 census, by the turn of the century Danskin was earning a living as a mechanical engineer, and had also started his own business - making and selling bicycles under the brand name Cushie-Doo - from above the family home in Plumstead, a stone's throw from Woolwich Arsenal. In 1907, after being offered a senior job with the Standard Motor Company, he sold up and moved the family to Coventry. In March 1916, Danskin's wife Georgina died of emphysema. He remarried two years later, to Rose Clara Richardson, and the couple had three children.

When Arsenal won the FA Cup in 1936, Danskin's employers threw a special celebration dinner in his honour. Danskin, though, was by now suffering serious problems with his legs - probably the result of old football injuries - and had been forced to listen to the live radio broadcast of the final from his sickbed.

Told he would need to have a leg amputated, he replied: "I've got two legs now and if I'm going to die, I'll die with two legs."

Eventually he had to be admitted as a hospital in-patient. He died in Warwick on August 4, 1948, at the age of 85, and is buried in Coventry. For the best part of half a century his story died with him.

It was largely down to the efforts of his grandson, Richard Wyatt, that his connection to Burntisland has been identified. Wyatt, who lives in Canada, made his first pilgrimage to the town several years ago, and has since collaborated closely with the Burntisland Trust to piece together his grandfather's story.

"The curiosity about my grandfather's life has always been there," he said in an interview during that visit in 2004.

"But without the internet there were few starting points for my research in Canada. I always wanted to make the trip to Burntisland and see where he was born.

"It's amazing to think of all the great players and managers Arsenal have produced over the years and know they are linked back to my grandfather and Burntisland."

There is another contemporary twist to the Danskin tale. Twelve decades on, the family tradition is being kept alive, down under, in the person of Danskin's great-great nephew, Matt, an Australian youth international who was once on the books of Werder Bremen.

Matt Danskin's father, Andrew, himself a footballer with Leeds United before emigrating to Australia in the 1970s, explained the thread that links his son to the Arsenal founder: "David Danskin had a younger brother called William, born in 1874. William's son John William was my grandfather. He had ten children, one of whom was my father, also John William.

John William's branch of the family moved to the north-east of England in search of work, before settling in Yorkshire, where they provided three generations of Leeds United players.

"My grandfather's brother, Bob Danskin, played for Leeds and Bradford Park Avenue in the late 1920s and 1930s. My father also went on to play for the Leeds reserve team," added Andrew Danskin.

"I also played for Leeds United from 1967 to 1970. So there is a great soccer tradition in the family, and now my son, Matt, is continuing it."

In 2001, Matt Danskin was spotted playing for Western Australia Under-16s by a scout from Werder Bremen and went on to play for the Bundesliga club's U19 team. After failing to break into the senior squad, he headed home in 2004, signing for Perth SC of the Western Premier League. He has since featured for the Qantas Young Socceroos (the Australian under-20 team).

According to his father, the latest member of this remarkable football clan could now bring the Danskin story full circle.

"He has been hoping to get a contract in the Australian Hyundai A League; but if this doesn't happen, he could try his luck in the UK."


Arsenal Ring - New Post