C++ Programming

Last updated on 26th March 2014

I taught this course to Ist M.Sc. (Maths) students twice or thrice. The last time I taught the course would have been in 2008. The class size was typically around 20 to 25 students.

The course content consists of:

  • Powerpoint presentations
    • [A few presentation slides have only the topic names and corresponding sections in the book. If time permits and there is a demand I am willing to consider expanding these few slides to be more like the other explanatory slides.]
  • Sample programs (some created by me and some from the course book)
    • [For sample/example programs created by me, as WordPress does not permit .cpp/.cc files to be uploaded I have converted .cpp/.cc files to pdf and then uploaded them. I have also provided an external link to zip files containing these sample/example programs in original file format.]
  • Assignments

The course is based on the book, “C++ Primer, Fourth Edition” by Stanley B. Lippman, Josee Lajoie & Barbara E. Moo. It is published by Pearson Education, http://www.pearsonhighered.com/educator/product/C-Primer/9780201721485.page. Please note that the fourth edition is reported as out of print in the above link and that the newer fifth edition of the book is available. However, pdf versions of the fourth edition book, as of now, can be easily obtained using Google search. The fourth edition book example source code can be downloaded from:

Windows format files: http://www.informit.com/content/images/0201721481/sourcecode/MS_files.zip

Linux format files: http://www.informit.com/content/images/0201721481/sourcecode/gnu_files.tar.gz

If there is demand and time permits, I am willing to consider upgrading this course content to the fifth edition sometime. I have not yet read the fifth edition book in detail and so I do not know whether most of this course content applies to that edition too. The material of this course does not cover the new C++ 11 standard. The fifth edition covers the new C++ 11 standard.

The course has a pre-requisite of the student having learnt ‘C’ programming. It focuses only on those aspects of C++ which are not present in ‘C’. [In many Indian colleges and universities, I believe, students are first taught ‘C’ programming over one or two semesters followed by a semester of C++. This is the environment in which I taught the course.] Further, it explains C++ concepts (over and above ‘C’ concepts) using relatively simple examples and assignments. So a Sales_item class or a string class is used as the example and/or assignment class instead of a somewhat more complicated graphics class. Such an approach I thought was suitable for the I M.Sc. (Maths) class that I taught as the institution did have some significant constraints in lab. time availability for programming work for those students.

For students who would like to do/follow/go through a more rigorous and perhaps time-consuming C++ course, taught at the freshman (first) year in a USA engineering college/university which does not seem to presume prior knowledge of ‘C’ language part of C++, information including links are provided about one such course designed by Prof. Bjarne Stroustrup, creator of C++, towards the end of this post.

Further, please note that, as a first step, I have focused on putting up the course content used by me to teach this course in the deemed university in Andhra Pradesh, India, suitably modified, on this blog. As part of the minor modifications for putting it up on this publicly accessible blog, some errors may have crept in. I have not checked all the modifications for accuracy (due to other demands on my time). If this course (on this blog) does get utilized by students then the errors in the modified part will come to light and will get fixed by me (or others). I think that is a better way of investing my (and others) time for fixing any errors in this course (on this blog).

The course covers the following topics.

1) Introduction to C++

  1. Importance of C++
  2. First C++ program

Powerpoint slides and example source file(s) in pdf format: Introduction.pptIntroduction-2.pptIntroduction-3.ppt and enumtest.cpp.pdf. Example source file(s) (my own, if I recall correctly, and so different from book example code) in original file format – zip file.

2) Introduction to C++ Classes

  1. Notion of Class
  2. How to use a class (as against how to create a class)
  3. Using member functions of a class
  4. Using operators of a class

Powerpoint slides: Classes-Introduction.ppt

3) C++ Standard Library

  1. Std namespace
  2. Using string class
  3. Using vector class (template)
  4. Using ifstream & ofstream classes

Powerpoint slides and example source file(s) in pdf format: stdLibrary1.ppt, stdLibrary2.ppt, fileio.cpp.pdf, filecin.cpp.pdf and countWordNLine.cpp.pdf. Example source file(s) (my own, if I recall correctly, and so different from book example code) in original file format – zip file.

4) Dynamic Memory

  1. Heap & Stack
  2. new expression
  3. delete expression
  4. Dynamic array allocation & deletion

Powerpoint slides and example source file(s) in pdf format: DynamicMemory.ppt, DynamicMemory2.ppt, newdelete.cpp.pdf, ccharstr.cpp.pdf and newsimple.cpp.pdf. Example source file(s) (my own, if I recall correctly, and so different from book example code) in original file format – zip file.

5) Functions

  1. Quick mention of C ‘functions’
  2. Reference parameters
  3. Default arguments
  4. Inline functions
  5. Class Member functions
  6. Scope of functions (Global & Class)
  7. Overloaded functions
  8. Function matching & Function Overload resolution
  9. Argument conversions

Powerpoint slides and example source file(s) in pdf format: Functions.ppt, Functions2TopicsOnly.ppt, Functions3.ppt, swap1.cpp.pdf, strparm1.cpp.pdf, swap2.cpp.pdf, strparm2.cpp.pdf, fnscope.cpp.pdf, fnscopeErr.cpp.pdf, ovlfns.cpp.pdf, ovlfns2.cpp.pdf and ovlfnsconv.cpp.pdf. Example source file(s) (my own, if I recall correctly, and so different from book example code) in original file format – zip file.

6) Classes (Creating own classes as against using supplied classes)

  1. Data Abstraction
  2. Data Encapsulation (Data Hiding)
  3. Class Definition; Difference between class definition and class declaration
  4. Constructors
  5. Friends
  6. static class members

Powerpoint slides and example source file(s) in pdf format: Classes2TopicsOnly.ppt, g.cpp.pdf and static.cpp.pdf. Example source file(s) (my own, if I recall correctly, and so different from book example code) in original file format – zip file.

7) Copy Control, Assignment Operator and Destructor

  1. Copy constructor
  2. Copy initialization
  3. Synthesized copy constructor
  4. Argument copy of class object
  5. Copy of object returned by a function
  6. Own copy constructor; when is it necessary?
  7. Assignment operator overload
  8. Synthesized assignment operator
  9. Own assignment operator overload; when is it necessary?
  10. Shallow Copy vs. Deep Copy
  11. Quick mention of other operator overloads (+, -, == etc.)
  12. What is a destructor?
  13. When does it get called?
  14. Destructor syntax
  15. Synthesized destructor
  16. Own destructor; when is it necessary?
  17. Rule of Three

Powerpoint slides and example source file(s) in pdf format:

ClassesCopyControl.pptClassesCopyControl2.pptClassesCopyControl3.ppt,

return.cpp.pdfourstring.cpp.pdfourstring2.cpp.pdfourstring3.cpp.pdf,

ourstringNoCC.cpp.pdfourstringAssignOp.cpp.pdfourstringAssignOp2.cpp.pdf,

ourstring4.cpp.pdf and ourstring5.cpp.pdf. Example source file(s) (my own, if I recall correctly, and so different from book example code) in original file format – zip file.

8) Inheritance

  1. Simple inheritance example
  2. Derived class & Base class
  3. Invoking methods through pointers
  4. Invoking method through base class pointer or reference
  5. Virtual functions
  6. Inheritance hierarchy
  7. Dynamic binding/Polymorphism
  8. Pure virtual functions

Powerpoint slides and example source file(s) in pdf format: Inheritance.ppt, Inheritance2.ppt, Inheritance3.ppt, inherit.cpp.pdf, inherit1.cpp.pdf, inherit2.cpp.pdf, inherit3.cpp.pdf, shapes1.cpp.pdf, shapes2.cpp.pdf, shapes3.cpp.pdf and vecshapes1.cpp.pdf. Example source file(s) (my own, if I recall correctly, and so different from book example code) in original file format – zip file.

9) Exception Handling

  1. Exceptions are run-time anomalies
  2. ‘C’ program error handling; Painful
  3. Exceptions to the rescue in C++
  4. throw
  5. try-catch
  6. What if there is no try-catch?
  7. Java, C# exception usage
  8. Exception class hierarchy
  9. Multiple catch handlers
  10. Functions exited during search for an exception handler
  11. Multiple try-catch

Powerpoint slides and example source file(s) in pdf format: Exceptions.ppt, Modexception_example.cpp.pdf, except2.cpp.pdf, except3.cpp.pdf and except4.cpp.pdf. Example source file(s) (my own, if I recall correctly, and so different from book example code) in original file format – zip file.

10) More on Std. Library

  1. Container classes & Generic algorithms
  2. Sequential Containers & Associative Containers
  3. Std. Library design
  4. pair
  5. map; how to use it
  6. iterators; vector & map iterators
  7. set; how to use it

Powerpoint slides and example source file(s) in pdf format: StdLibmap.ppt, word_count.cc.pdf, word_count1.cc.pdf and word_count2.cc.pdf. Example source file(s) (my own, if I recall correctly, and so different from book example code) in original file format – zip file.

Former Student Feedback Comments

A former student who was taught this course by me in the deemed university in Andhra Pradesh, India, wrote the following over email on March 18th 2014 (modified slightly to fix a couple of minor grammatical errors):
It is great that you have shared the C++ programming teaching material prepared by you on your blog. I really hope that people make use of it for I know how useful it could be. I can say this as a direct beneficiary of this, and today that is what fills my bank account at the end of every month.

I just hope and pray that Swami gives you the strength and determination to keep up the good work. I also think that I too can take a tiny part of the credit in this endeavour of you putting up these slides, as I was perhaps one of the many who would have suggested that you put these slides up in some forum accessible to the students’ community. Thank you for considering the request.

Prof. Bjarne Stroustrup’s book and course on C++

Prof. Bjarne Stroustrup, creator of C++, teaches C++ at Texas A&M university (TAMU). The course is based on his book, Programming — Principles and Practice Using C++. Here’s the book support page: http://www.stroustrup.com/Programming/. An extract from the book support page, “This book is based on a course I designed for engineering freshmen at Texas A&M University. It has now been taught by me and others, at TAMU, and elsewhere, in academia and industry, for almost five years and to thousands of students. It is currently available in (physical and web) book stores.” This page also has a link to lecture slides (for teaching C++).

Note about Course Book Source Code Copyright

The course book (C++ Primer, 4th edition) source code files have permission for usage statements as comments in the beginning of the files. As an example a source code file of chapter 1 (folder named 1) – mysum.cpp has the following comments towards the beginning of the file:

* Permission is granted for this code to be used for educational purposes in
* association with the book, given proper citation if and when posted or
* reproduced.Any commercial use of this code requires the explicit written
* permission of the publisher, Addison-Wesley Professional, a division of
* Pearson Education, Inc. …

—- end extract from header comments of source file —

As this blog/website of mine is a free teaching service to students on the Internet, and not-for-profit, I can consider that permission has been granted for me to use code from the above file on this blog. Further, I can use code from all other example code files of the book which carry a similar permission grant statement on this blog/website. Of course, I have given proper citation on each blog post/web page and Powerpoint slides file that uses the course book code.

The status history regarding the permission issue and putting up the C++ course contents on this blog is available here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s