Category Archives: C++

Getting started with C++

How does one get started with C++? Where should an aspiring developer start? What books should they read? These are some questions that do the rounds of many developers who want to dip their toes in the magical world of C++. This post is an attempt to address some of the questions while also trying to provide my own perspective. If you find the links and my suggestions useful, please do let me know. If you do not find them useful, kindly ignore.

C++11 a.k.a Modern C++ is a new language. It feels different from the old C++ (C++98) and places great emphasis on developer productivity enhancing facilities, focus on runtime performance, writing safe, efficient and portable code. Hence it is very, very important to learn the nuances of Modern C++ v/s trying to learn the old C++ and attempt to fit the old in the new.

Here are my recommended books.

  1. C++ Primer by Stanley Lippman et.al. The latest edition has been completely re-written for the C++11 standard.
  2. The C++ Programming Language 4th Ed by Bjarne Stroustrup. The 4th edition has been revised and updated for the C++11 standard.
  3. The C++ Standard Library, 2nd Ed by Nicolai Josuttis. The 2nd edition has been revised and updated for the C++11 standard. This book focuses on the Standard Library that is part and parcel of the C++ programming language.

These books are by no means exhaustive but will allow you to start learning C++. One can begin with C++ Primer and then move to the other books to understand and learn about advanced topics.

Having said that, reading in no way substitutes for actual coding practice. Spend as much time as possible writing code. You can start with simple programs that are discussed in the books above and slowly work your way to adding functionality and features to those programs. Or you can also start writing your own programs. Either way, know your tools and write as much code as possible while applying the concepts you learn.

A lot of coding problems have probably been already solved. The technology world is a rich source of learning from one another, provided one learns to ask the right questions in the proper format. Hang out on sites such as stackoverflow.com, codeproject.com, MSDN forums etc and learn from fellow developers. If you know the answer to a problem, help your fellow colleagues from the industry. Participating in community activities helps you build confidence in your tools, your craftsmanship and ultimately your own abilities. Think of this as an investment for your own good and the benefit of all.

In addition here are a few links you should regularly visit to keep yourself updated with all major events and happenings related to C++.

  1. http://www.isocpp.org
    The Home Page of Standard C++.
  2. http://www.stroustrup.com/ The Home Page of Bjarne Stroustrup, the designer of C++.
  3. http://herbsutter.com Herb Sutter on software, hardware, and concurrency. He has also begun an updated series of Guru of the Week Questions based on C++11 and will be updating his famous Exceptional C++ series of books with C++11.
  4. http://scottmeyers.blogspot.com/ Scott Meyers is one of the world’s foremost experts on C++ software development. He offers training and consulting services to clients worldwide.

Again, no means exhaustive but good starting points from where you can learn C++. Once you learn the basics of standard C++, you can then move to learning platform specific extensions like C++ /CX and develop cool Windows Store apps!

-Sridhar

A short interview on Why is C++ still the answer?

Preemptive snarky comment: What is the question? J

A few weeks back the awesome folks from the Microsoft Careers group and I had a short conversation about my book, how I ended up in the technology world and why I strongly believe that C++ is still the answer to some of the big challenges we face in the technology industry. They have published the interview in a Q&A format via multiple social networks. Pick the one you are associated with and enjoy!!

FACEBOOK

In the latest JobsBlog: Microsoft program manager, and recently published author, Sridhar Poduri describes his path to Microsoft and explains why C++ is the answer: http://bit.ly/15fGtwZ

          Feel free to tag the Microsoft Careers facebook page when sharing.

TWITTER

New on the @MicrosoftJobs Blog: Microsoft PM & author @sridharpoduri explains why C++ is the answer – http://bit.ly/15fGtwZ

 
 

LINKEDIN

In the latest JobsBlog: Microsoft program manager, and recently published author, Sridhar Poduri describes his path to Microsoft and explains why C++ is the answer: http://bit.ly/15fGtwZ

 
 

          Feel free to tag the Microsoft page on LinkedIn when sharing.

Modern C++ and Windows Store apps giveaway

BUILD 2013 is 9 weeks away. In anticipation of BUILD 2013, I am listing a giveaway for my book, “Modern C++ and Windows Store apps”. Every week leading to the BUILD 2013 conference, I will give out a copy of my book. Sounds interesting? Want a copy? Getting a copy is easy. All you have to do is put on your thinking caps and send me a mail about “What interests you about Windows 8 and Modern C++ put together to develop Windows Store apps?”

Each week I will select the best quote I receive, and send out a copy of my book to the person who has sent me the quote. I will also share the quote on my blog, twitter and Facebook feed. So, put your thinking caps on and send me mail at win8book at sridharpoduri dot com.

Please do not post your quotes as comments to this blog post as I will not be considering those. J

Rules and Regulations:

  1. Only one entry per person. I cannot verify duplicates but would expect everyone as professionals to be honorable.
  2. I reserve the right to select what I believe is best entry every week.
  3. I will share the best entry every week.
  4. I will not share the details of the person who sent the best entry (email address or any other personally identifiable details).
  5. After I post the selection of the best entry please look out for an email from me asking for your mailing address.
  6. I will bear the cost of shipment of the book to your address. You do not have to pay for shipping or the book cost. J
  7. Feel free to tweet or share this on your social networks!

Bring those interesting thoughts on!!!

Cheers,

Sridhar

[Updated with purchase and discount links] Modern C++ and Windows Store Apps – A book about Modern C++ and native technologies

Over the past few months, I have been writing a book on developing apps using C++ and the various native technologies shipping as part of the Windows 8 wave. Some of these technologies include: C++11, C++ /CX, native XAML, the Windows Runtime, C++AMP, Windows Azure Mobile Services etc. I am happy to announce that the Kindle version of “Modern C++ and Windows Store apps” will be available shortly for purchase via the Amazon website. A paperback edition will also be available soon.

For purchasing the paperback edition of the book, please visit this link. You can use the discount code 5C56GCKN to get 20% discount off MRP.

For folks interested in an electronic edition, you can purchase a Kindle edition on Amazon

A quick update: This is just me self-publishing the book thereby bringing the work to its logical conclusion.

For the benefit of everyone, I am listing some of the topics that await anyone who picks up a copy of this book. For the more adventurous amongst us who would rather prefer to play with code, the book samples can be downloaded from the Downloads section of this blog.

The new native API
What is a Windows 8 App?
Quick walkthrough of a Windows 8 App

Hello Modern C++
Move semantics
Welcome to the Component Extensions
The C++ /CX Type System
Asynchronous Programming in C++ /CX
Building a WinRT component using C++ /CX and ISO-C++
Guidance on the use of C++ /CX

Introduction to XAML
Hello World with XAML and C++ /CX
Basic XAML Syntax
Using Panels to Layout UX
Basic XAML Controls
Windows 8 Signature XAML Controls
Handling Events
Markup Extensions
Data Binding
Binding to a Data Model
Building a XAML custom control
Using Animations in XAML Controls

Introduction to XAML + DirectX
SurfaceImageSource
VirtualSurfaceImageSource
Design considerations when using SiS and VSiS
SwapChainBackgroundPanel
The DrawIt Application – C++, XAML and DirectX

The C++ AMP Library
The ImageEffects application

Playing by the rules of the Windows Runtime
Introduction to the Windows Runtime Library
Hello World XAML App using Windows Runtime Library

Getting Started with Windows Store apps and Windows Azure Mobile Services
Creating a simple Windows Store app with Windows Azure Mobile Services
Supporting Push Notifications using Windows Azure Mobile Services

And more….

I had a lot of fun writing this stuff and learning along the way. I hope there are concepts that you can apply to your work as you build apps for the Windows 8 Store using C++.

Please send feedback on the book contents directly to win8book at sridharpoduri dot com

The power and flexibility of C++ or why you should write C++ apps for Windows 8 and beyond :-)

Last Friday, the C++ team in Redmond released source code for “Project Austin” a skunk works project that is essentially a note taking app. Check out the team’s announcement blog here.

This app is one great example of the power of C++ allowing you to code in a largely ISO-C++ code base and then interface with the Windows Runtime API. The app uses the XAML-DX SwapChainBackgroundPanel type and uses XAML to layer UI elements over the “main Window”. It also uses C++AMP and extensively uses DirectX and all of the native goodness that is present in C++ and Windows 8.

I could go on and on but I would defer you to the blog post above. And yes, all this goodness in graphics, performance, inking etc without the overhead of GC.

Enjoy!

Running the Ocean Simulation AMP sample on the NVidia GeForce GTX 550Ti

As mentioned earlier, I upgraded my home machine GPU to a NVidia GeForce GTX 550Ti recently. I setup a USB hard disk with Win8 and Windows To Go (WTG) and took time today to test AMP performance on this setup.

I built the Ocean simulation AMP sample available here in Release mode and launched the application. The application consistently obtained 60fps and at times more than that too. I recorded the application executing on the NVidia GPU and uploaded it to vimeo.

 

Using JSON and C++ in Windows 8 Apps

The LiveSDK team has a bunch of exciting samples demonstrating using JavaScript along with HTML, C# with XAML to build apps that integrate Live services. Sadly however, a full blown C++ sample is missing from the lot as I write this today. Last week I built a proof of concept sample PhotoViewer app as part of writing samples for my upcoming book on building Windows 8 Apps using C++ and XAML. What follows below is a summary post on how to consume JSON APIs in C++.

Windows Runtime has built in APIs to support JSON APIs: both building objects based on responses from web services like the Live service in our case and then parse the object collection to build desired features. The namespace Windows::Data::Json namespace contains a list of APIs that make JSON consumption a breeze. The various classes and types that form part of this namespace are listed below:

  1. IJsonValue
  2. JsonArray
  3. JsonError
  4. JsonErrorStatus enumeration
  5. JsonObject
  6. JsonValue
  7. JsonValueType enumeration.

In order to make successful requests to Live services, it is recommended to have users sign in using “Microsoft Accounts”. Purchasing and downloading apps from the Windows Store needs users to sign in with Microsoft Account. Having such an account also enables single sign on for all Microsoft services. We will explore the process of triggering a sign in operation in a later blog post. For today, we focus on making requests to the skydrive APIs.

Fetching Album data from Skydrive

The Live Services team built a great “test” portal for testing various Live APIs here.  The first web request to fetch a list of album data is to make a GET request to the base API url at http://apis.live.net/v5.0/me/albums and append an authentication token. This token is initially obtained when the user signs in to the Live OAuth authentication API. For our C++ code to make a successful web request, we rely on XHR (Xml Http Request). The LiveSDK samples on GitHub have a helper class called XHREvent and we will rely on the same. Our code to make a web request looks like this:

 1: std::wstring url = PhotoSkyCpp::BaseApiUrl + L"/me/albums" + SampleDataSource::GetAccessTokenParameter();

 2: SendRequest(

 3:     ref new Uri(ref new String(url.c_str())),

 4:     "GET",

 5:     ref new XHRDataReceivedHandler(this, &SampleDataSource::OnDataAvailable),

 6:     ref new XHRCompletedHandler(this, &SampleDataSource::OnGetAlbumDataCompleted),

 7:     ref new XHRFailedHandler(this, &SampleDataSource::OnSendRequestFailed));

The SendRequest function accepts a URL, an action verb, which in our case is “GET” and three callback functions, one for notifying if data is available, one for the data fetch completion status and finally a callback to handle request send failures.

For the case of our discussion, we will ignore both the OnDataAvailable and OnSendRequestFailed functions. When the Live API sends a response to our “/me/albums” request, it does so in JSON format. This response is passed back to the OnGetAlbumDataCompleted handler and we will build our JSON objects here.

In order to build a Json Object successfully from a web response such as Live service, we first need to initialize the Json object as follows:

 

 1: JsonObject^ tokenResponse = ref new JsonObject();

 

The JsonObject class listed above has a static function named TryParse that accepts a string and a reference to a JsonObject type that can be initialized with the said response string.

 

 1: if (JsonObject::TryParse(responseString, &tokenResponse))

 2: {

 3:     ... use the JsonObject tokenResponse

 4: }

 

Once the JsonObject is initialized successfully, we obtain a read-only collection of the Json Objects. The next step is parsing this collection for the data key. For a valid Json object view, a data key must be present. The Lookup method on the IMapView collection returns a IJsonValue^ associated with the data key. The next step is to obtain a string representation of the series of Jsonvalues by calling the Stringify method on the IJsonValue^ obtained from the previous step.

 

 1: auto map = tokenResponse->GetView();

 2: IJsonValue^ value = map->Lookup("data");

 3: String^ s = value->Stringify();

 

The final step is to iterate over the collection of items present in this string and do whatever is needed for your app.

 

 1: JsonArray^ mapValue = ref new JsonArray();

 2: if (JsonArray::TryParse(s, &mapValue))

 3: {

 4:     auto vec = mapValue->GetView();

 5:     std::for_each(begin(vec), end(vec), [this](IJsonValue^ M)

 6:     {

 7:         Name = M->GetObject()->GetNamedString("name");

 8:         ID = M->GetObject()->GetNamedString("id");

 9:         //Do specific operations using the Name and ID properties

 10:     });

 11: }

 

This is a very simplified introduction to using the JSON APIs in Windows Runtime and consume the same from C++.

 

Enjoy!!!