Thursday, May 7, 2009

C++ FAQ

[1.1] What is a class?
The fundamental building block of OO software. 
A class defines a data type, much like a struct would be in C. In a computer science sense, a type consists of both a set of states and a set of operations which transition between those states. Thus int is a type because it has both a set of states and it has operations like i + j or i++, etc. In exactly the same way, a class provides a set of (usually public) operations, and a set of (usually non-public) data bits representing the abstract values that instances of the type can have. 
You can imagine that int is a class that has member functions called operator++, etc. (int isn't really a class, but the basic analogy is this: a class is a type, much like int is a type.) 
Note: a C programmer can think of a class as a C struct whose members default to private. But if that's all you think of a class, then you probably need to experience a personal paradigm shift. 
________________________________________
[1.2] What is an object?
A region of storage with associated semantics. 
After the declaration int i; we say that "i is an object of type int." In OO/C++, "object" usually means "an instance of a class." Thus a class defines the behavior of possibly many objects (instances). 
________________________________________
[1.3] When is an interface "good"?
When it provides a simplified view of a chunk of software, and it is expressed in the vocabulary of a user (where a "chunk" is normally a class or a tight group of classes, and a "user" is another developer rather than the ultimate customer). 
The "simplified view" means unnecessary details are intentionally hidden. This reduces the user's defect-rate. 
The "vocabulary of users" means users don't need to learn a new set of words and concepts. This reduces the user's learning curve. 
________________________________________
[1.4] What is encapsulation?
Preventing unauthorized access to some piece of information or functionality. 
The key money-saving insight is to separate the volatile part of some chunk of software from the stable part. Encapsulation puts a firewall around the chunk, which prevents other chunks from accessing the volatile parts; other chunks can only access the stable parts. This prevents the other chunks from breaking if (when!) the volatile parts are changed. In context of OO software, a "chunk" is normally a class or a tight group of classes. 
The "volatile parts" are the implementation details. If the chunk is a single class, the volatile part is normally encapsulated using the private and/or protected keywords. If the chunk is a tight group of classes, encapsulation can be used to deny access to entire classes in that group. Inheritance can also be used as a form of encapsulation. 
The "stable parts" are the interfaces. A good interface provides a simplified view in the vocabulary of a user, and is designed from the outside-in (here a "user" means another developer, not the end-user who buys the completed application). If the chunk is a single class, the interface is simply the class's public member functions and friend functions. If the chunk is a tight group of classes, the interface can include several of the classes in the chunk. 
Designing a clean interface and separating that interface from its implementation merely allows users to use the interface. But encapsulating (putting "in a capsule") the implementation forces users to use the interface. 
________________________________________
[1.5] How does C++ help with the tradeoff of safety vs. usability?
In C, encapsulation was accomplished by making things static in a compilation unit or module. This prevented another module from accessing the static stuff. (By the way, static data at file-scope is now deprecated in C++: don't do that.) 
Unfortunately this approach doesn't support multiple instances of the data, since there is no direct support for making multiple instances of a module's static data. If multiple instances were needed in C, programmers typically used a struct. But unfortunately C structs don't support encapsulation. This exacerbates the tradeoff between safety (information hiding) and usability (multiple instances). 
In C++, you can have both multiple instances and encapsulation via a class. The public part of a class contains the class's interface, which normally consists of the class's public member functions and its friend functions. The private and/or protected parts of a class contain the class's implementation, which is typically where the data lives. 
The end result is like an "encapsulated struct." This reduces the tradeoff between safety (information hiding) and usability (multiple instances). 
________________________________________
[1.6] How can I prevent other programmers from violating encapsulation by seeing the private parts of my class?
Not worth the effort — encapsulation is for code, not people. 
It doesn't violate encapsulation for a programmer to see the private and/or protected parts of your class, so long as they don't write code that somehow depends on what they saw. In other words, encapsulation doesn't prevent people from knowing about the inside of a class; it prevents the code they write from becoming dependent on the insides of the class. Your company doesn't have to pay a "maintenance cost" to maintain the gray matter between your ears; but it does have to pay a maintenance cost to maintain the code that comes out of your finger tips. What you know as a person doesn't increase maintenance cost, provided the code you write depends on the interface rather than the implementation. 
Besides, this is rarely if ever a problem. I don't know any programmers who have intentionally tried to access the private parts of a class. "My recommendation in such cases would be to change the programmer, not the code" [James Kanze; used with permission]. 
________________________________________
[1.7] Is Encapsulation a Security device?
No. 
Encapsulation != security. 
Encapsulation prevents mistakes, not espionage. 
________________________________________
[1.8] What's the difference between the keywords struct and class?
The members and base classes of a struct are public by default, while in class, they default to private. Note: you should make your base classes explicitly public, private, or protected, rather than relying on the defaults. 
struct and class are otherwise functionally equivalent. 
OK, enough of that squeaky clean techno talk. Emotionally, most developers make a strong distinction between a class and a struct. A struct simply feels like an open pile of bits with very little in the way of encapsulation or functionality. A class feels like a living and responsible member of society with intelligent services, a strong encapsulation barrier, and a well defined interface. Since that's the connotation most people already have, you should probably use the struct keyword if you have a class that has very few methods and has public data (such things do exist in well designed systems!), but otherwise you should probably use the class keyword. 

Friday, April 10, 2009

Prog2impress.Com

Prog2impress.Com

A website designed to help learning C++. Understandable C++ programming tutorials, source code, tips and tricks. Prog2impress.com

Thursday, March 12, 2009

C++ Web Sites

http://www.utc.edu/~cslab/cpsc350/cs350/Notes_on_C++.ps
62 page book on learning how to write C++ code

http://www.blindprogramming.com/ftp/c/
C++ Tutorials and Learning Downloads

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang98/HTML/_pluslang_organization.asp
C++ Language Reference, according to Microsoft

http://www.intap.net/~drw/cpp/index.htm
Online C++ Tutorial

http://www.cee.hw.ac.uk/~rjp/Coursewww/
Online C and C++ Courses - The material consists of two related courses, each with an integrated set of notes, examples and exercises. The two courses will cover ANSI C and C++.

http://www.functionx.com/bcb/index.htm
Borland C++ Builder Tutorial

http://www.functionx.com/cpp/index.htm
C++ Tutorial

http://swt-www.informatik.uni-hamburg.de/~strunk/cpp/
C++ Tutorial Textbook

http://www.4p8.com/eric.brasseur/cppcen.html
C++ Tutorial for C Users - This text enunciates and illustrates features and basic principles of C++. It is aimed at experienced C users who wish to learn C++.

http://www.devx.com/free/tips/tipresults.asp?whereby=title&CPP=1
C++ Tips

http://www.devx.com/gethelp/gethelp.asp?Area=C%2FC%2B%2B
Get help with C++ - ask your questions and get answers
http://www.gaddisbooks.com/standard/Downloads/introvc.zip
Introduction to Visual C++ (112 K) - This MS-Word document contains a guide for getting started with Microsoft Visual C++ 6.

http://www.ddj.com/articles/2001/0185/languages/c/visual_cpp_tutorial.htm
Visual C++ Tutorial

http://www.gaddisbooks.com/alternate/Downloads/QuickRef.zip
C++ Quick Reference Sheet - This MS-Word document provides many helpful quick references for the C++ languages. It includes references for data types, operators, if statements, loops, cin, cout, and commonly used library functions.

http://www.gaddisbooks.com/standard/Downloads/introcb.zip
Introduction to C++ Builder (131 K) - This MS-Word document contains a guide for getting started with Borland (Inprise) C++ Builder 4.

http://www.programmersheaven.com/filez/cpp/text/DARKCPP.PDF
The Darker Side of C++ Revisited

http://www.linuxdoc.org/HOWTO/C++Programming-HOWTO.html
C++ Programming How To - This document provides a comprehensive list of C++ URL pointers, links to C++ online textbooks, and programming tips on C++.

http://www.cplusplus.com/
cplusplus.com - The C++ resources network - Information, Documentation, Reference, Sourcecodes and Forums about C++ programming language

http://www.cplusplus.com/doc/
The Cplusplus.com Language Tutorial

http://www.oreilly.com/catalog/cplus/chapter/ch07.html
The Programming Process chapter from Practical C++ Programming

http://www.meetit.com/skilltest/CplusTest.html
Free Object Oriented C++ Skills Test

http://www2.links2go.com/topic/C++_Tutorials
C++ Tutorial links

http://www.troubleshooters.com/codecorn/crashprf.htm
Crashproof C++: The bad-old-days of C are over.

http://www.programmersheaven.com/zone3/index.htm
C/C++ Zone

http://web.archive.org/web/20030208004025/http://www.vb-bookmark.com/CppTutorial.html
Links to C++ Tutorial and Beginner's Guides

http://msint12.tripod.com/
C++ Tutorial is available here

http://www.desy.de/user/projects/C++.html
C++ Tutorial

http://www.cs.brown.edu/courses/cs123/javatoc.html
Java to C++ Transition Tutorial

http://www.cygnus.com/misc/wp/
The ISO/ANSI C++ Draft Standard

http://www.dinkumware.com/htm_cpl/index.html
C++ Library Reference

http://library.thinkquest.org/21963/C__~1/C__~1.HTM
C++ Tutorial

http://www.cs.wustl.edu/~schmidt/C++/index.html
C++ Programming Language Tutorials
This material was developed as part of a series of courses on C++ I taught at the University of California, Irvine and at Washington University, St. Louis. I'm making it available on the WWW for anyone who is interested in teaching or learning about C++. These handouts are stored in postscript 4-up on a page.

http://www.gustavo.net/programming/c__tutorials.shtml
C/C++ Tutorial Links

http://www.infosys.tuwien.ac.at/Research/Component/tutorial/prwmain.htm
Standard Template Library for C++

http://www.cs.uregina.ca/dept/manuals/C++/tutorial.html
http://www8.silversand.net/techdoc/cpp/cpplist.htm
C++ Tutorial (Coronado), downloads here: Cptutt22.zip 112kb and Cptuts22.zip 83K (source code)
http://www.redrival.com/bigt/basicc.htm
C/C++ Tutorials

http://www.teststeststests.com/c++test.htm
C++ Syntax Test

http://linknetwork.tripod.com/cplusplus.html
C/C++ Links

http://www.sasked.gov.sk.ca/~ehs/HeiseIntra/C.html
Free C++ Course

http://www.mysteries-megasite.com/linux/C-tutorial.html
C++ Tutorial Links

http://www.dinkumware.com/refcpp.html
The Dinkum C++ Library Reference

http://users.utu.fi/sisasa/oasis/oasis-cc++.html
C/C++ Links

http://www.cee.hw.ac.uk/~pjbk/pathways/cpp1/cpp1.html
Introduction to C++ Programming I

http://www.skylit.com/pascpp/index.html
Pascal and C++ Side by Side

http://www.skylit.com/style/index.html
The 32 Bits of Style by Gary Litvin (C++)

http://webnz.com/robert/cpp_site.html#Learn
Learning C++ - Links

http://doc.trolltech.com/
Qt Reference Documentation and Tutorials

http://www.cs.umd.edu/users/cml/cstyle/
http://www.cs.umd.edu/users/cml/resources/cstyle/
C and C++ Style Guides - This page offers access to my collection of style guides for code written in C and C++. A few documents offer some discussion about the value and utility of such style guides.

http://members.tripod.com/jcarbrey/cpp_programming_sourcecode/tutorial.htm
C++ Tutorial links

http://www.findtutorials.com/list.asp?category=56
C++ Tutorial Links

http://www.findtutorials.com/list.asp?category=60
Visual C++ Tutorial Links

http://www.academicoop.com/Curriculum/visualc++.asp
Visual C++ Resources and Tutorials

http://www.unixreview.com/development/c_plus.shtml
C++ section of Unix review

http://www.inficad.com/~thurmunit/c/
C/C++ Resources

http://www.csci.csusb.edu/dick/samples/c++.html
Pointers to C++ Documentation and Help

http://www.cs.ucf.edu/courses/cop3530/Stl-tut.doc
C++ STL Tutorial
http://www.cs.ucf.edu/courses/cop3530/Stl-sol.doc
Exercise Solutions
R>
http://www.phys.ualberta.ca/~binliu/cc.htm
C/C++ Tutorial Links

http://examware.com/tutor6.html

Programming in C and C++ A web site designed to help people learn to program C/C++ easily, as well as give them many other useful resources
http://www.cprogramming.com

http://www.phys.ualberta.ca/~binliu/cc.htm
C/C++ Tutorials

http://www.planet-source-code.com/vb/Tutorial/default.asp?lngWId=3
C/C++ Tutorials and Articles

http://www.cplus-zone.com/

http://www.vb-bookmark.com/vbCpp.html

http://cplus.about.com/compute/cplus/mbody.htm

http://www2.andrews.edu/~maier/tutor.html
Tutorials in C/C++

http://www.scriptsearch.com/C_and_Cpp/
http://www.scriptsearch.com/C_and_Cpp/Tips_and_Tutorials/
C and C++ Tips and Tutorials from Scriptsearch.com - Online tutorials, tips, and help on programming in Python.

http://www.thefreecountry.com/developercity/freelib.html
Free C and C++ Libraries

http://freebooks.by.ru/view/ObjectWindowsProgGuide/ewtoc.html
Object Windows Programmers Guide, Volume 3 - free book

http://www.1001tutorials.com/cpp/index.shtml
Many C++ Tutorials linked on this page