Few Prime Ministers have been so comprehensively misunderstood by their contemporaries as David Cameron. The press never got the measure of him, and was taken aback both by his successes and his failures.

He was born in 1966, entered the Commons in 2001 and, a mere four years later, decided to run for the leadership of the Conservative Party, which had just lost its third general election in a row.

About the time Cameron threw his cap into the ring, a book called The Strange Death of Tory England appeared, in which Geoffrey Wheatcroft suggested the Conservatives were “in terminal decline”.

Since the crushing defeat of 1997, three leaders – William Hague, Iain Duncan Smith and Michael Howard – had failed to get the party back into power. The conventional wisdom, during most of the leadership contest of 2005, was that David Davis would be the next to have a go.

But Cameron came through and won. In class terms, this was unexpected. For as Wheatcroft wrote of the leadership contest in 1965, which was won by Edward Heath:

“Altogether a mark of the defeat of the old guard was that there was no ‘White’s candidate’: the famous aristocratic Tory club in St James’s Street was baffled, unable to find one of its own to run; Macmillan’s old governing class was losing its nerve, or its will to win.”

Cameron was a member of White’s. This alone was enough to confuse most journalists, especially as he subsequently resigned from the club, where his father had served as chairman.

The press informed the world that Cameron had been educated at Eton and Oxford, and while at the latter, had been a member of the Bullingdon Club. His background was taken to mean he must suffer the drawback of being an out-of-touch toff: a sort of walking anachronism, who would be useless at getting on with ordinary people.

One may note that the typical pundit went to a fee-paying school, but a less famous one than Eton, and to Oxford, but was not elected to the Bullingdon. How tempting for the hard-pressed commentator to overlook his or her privileged start in life, while suggesting Cameron’s was a fatal weakness.

And Cameron himself seemed a bit embarrassed by his background. Either he or one of his supporters arranged for the official group photographs of the Bullingdon to be withdrawn from circulation.

But the media’s obsession with the Bullingdon, though tiresome, was also useful to Cameron, for it led his opponents to underestimate him. His professionalism was ignored.

This was developed after Oxford, in the less glamorous setting of the Conservative Research Department, where Cameron worked in his twenties for senior ministers, including Margaret Thatcher and John Major.

Here he added to his natural gifts such a thorough grounding in politics, and such a professional group of friends, that he was able in 2005 to win, at the age of only 39, the Conservative leadership, and to avoid falling flat on his face once he had done so.

As leader he set out to modernise the party, notably by increasing the number of women and ethnic minority candidates. Intellectuals found much of what he did unsatisfying, for he refused to define what he was doing in the ideological terms which alone made sense to them.

They did not value, or in most cases even notice, the Anglican tradition of behaviour in which he was brought up, absorbed via his mother from her family, the Mounts. According to another member of that family – the writer Ferdinand Mount, who worked for Margaret Thatcher when she was Prime Minister – “a high moral tone came naturally to them”.

As an Anglican, Cameron was free from dogma and amenable to compromise, so felt no need to define himself either as a Thatcherite or (which would have been fatal when he ran for the Tory leadership) as an anti-Thatcherite. On the great question of the European Union, he could pursue a via media, or middle course, where the dogmatists were unable to detect one.

And for Cameron it was the easiest thing in the world to strike, on behalf of all people of good will, a high moral tone, and when needed (i.e. when someone else had behaved badly) a note of asperity. In another life, he could have been a successful contributor to Thought For The Day.

This ability to articulate the opinions of one’s own time is essential if one is to operate at a high level in either politics or journalism. As Bagehot put it, in his study of an earlier Tory Prime Minister, Sir Robert Peel:

“A constitutional statesman is in general a man of common opinions and uncommon abilities.”

Or as one of Cameron’s early political contemporaries, the late Professor Maurice Fraser, said of him:

“A lot of people were clever, others were perfectly solid but more pedestrian, but he had judgment – he was political to his fingertips – he always knew what you could say and what you couldn’t.”

Cameron knew exactly how to express “common opinions”, or opinions which were just about to become common, for example on same-sex marriage. That reform, implemented three years into his prime ministership, enraged many Conservatives, but gave him a chance to show a wider audience how progressive he was being.

In this he resembled Harold Macmillan, Prime Minister from 1957-63, who always sought to demonstrate that the Conservatives were more progressive than Labour, and for a long time succeeded.

One cannot pretend that Cameron managed to form a close emotional connection with the British people. He seldom if ever sounded convincing when he tried to be passionate, for he would not give enough of himself away.

He liked to be in control, and set a high value on competence. At his best, he made very difficult things look easy.

At the 2010 general election, the Conservatives under Cameron won 108 more seats than in the previous election. This was not quite enough for an overall majority.

But Cameron wasted no time in vain regrets. On the morning after the general election, he proposed on generous terms a coalition with the Liberal Democrats, whose manifesto had already been subjected by the Conservatives to searching analysis, in preparation for just such an eventuality.

Professionalism has its uses. Within five days, a coalition government had been formed which to the astonishment of many commentators, was to endure for five years.

A start was made on reducing the Budget deficit, and on major reforms in such fields as welfare and education. Cameron also conceded a referendum on Scottish independence, and in 2014 the Scots voted by 55 to 45 per cent to stay in the United Kingdom.

Like John Major in the 1990s, Cameron faced the danger that his prime ministership would be ruined by civil war within the Conservative Party over Europe. He dealt with this by promising, in his Bloomberg speech in January 2013, that if re-elected in 2015, he would within two years hold a referendum on Britain’s membership of the European Union.

Essentially, he was saying to his Eurosceptic wing, and also to UKIP, which at this period was doing alarmingly well: “You can have your referendum, but I’m going to win it.”

Before the EU referendum could take place, Cameron had to win the 2015 general election. The pundits and the pollsters were convinced he would fail to do this: another instance of their inability to read his prime ministership, for he astonished them by gaining another 25 seats, and thus a narrow overall majority in the Commons.

His former Liberal Democrat partners lost almost all their seats. Cameron had killed them with kindness.

Control and competence are undramatic virtues, and few people spotted the methodical shamelessness with which Cameron used his powers of patronage to strengthen his grip on power. In the creation of privy counsellors and peers, he was profligate on an eighteenth-century scale.

This does not mean he handed out, like a latter-day Walpole, offices of enormous profit to their holders, or that he gave most of the best places to members of his own family.

In twenty-first century politics, people can be bought for so much less than in the eighteenth century: most often, just by some soothing of their vanity. One has only to read Alan Clark’s diaries to see how an intelligent person can be consumed by the desire for a footling ministerial post.

And no one in modern times had a greater genius than Cameron for distributing those footling ministerial posts to the combination of people best calculated to keep him in power. He gave hope to everyone, young and old, male and female, left and right, bright and dim, that their time would come.

But he still had to hold the referendum. He decided to get it over and done with as quickly as possible, before it could become a verdict on himself – a danger he also tried to avert by announcing that he would in any case step down before 2020.

The pollsters, the pundits and even the City predicted he would get away with this gamble. We now know they were wrong. Within hours, Cameron drew the unavoidable conclusion and announced he was stepping down.

His prime ministership has been marked by an exceptional degree of grace under pressure. No one could have been better at accepting the findings of the Bloody Sunday inquiry. But his good manners tended to conceal his intense competitiveness.

How his reputation will stand in years to come, no one can say. For although the referendum result was greeted, in some quarters, as a humiliation on a par with Suez, we cannot yet know how it will play out.

Cameron may yet go down as the the Prime Minister who enabled us, albeit inadvertently, to recover our freedom.