Code Examples
CSc 220: C and C++ Programming
This page links a large number of small code examples for use in lecture.
Introduction
Expressions, Statements and Functions
Basic Constructs
Hello, World!
Hello in C++
Newlines
Identifiers
Constants
An Average Example
Problems With C I/O
Average in C++
Increment Operators
C++ Strings
Control Constructs
Boolean Operations
Another Average Example
Reading In the While Test
Using Update Operators
Watch Out For This
Comparison Pitfall
For Loops
Average For Loop
Tom Five Times I
Tom Five Times II
Tom Five Times III
Tom Five Times IV
Tom Five Times V
Switch
Switch Using Break
Break and Continue
Calculator Example
Functions In Plain C
Functional Average
Functional Average II
Functional Average III
Functions In C++
C++ Functions
Default Parameters
Function Overloading
Reference Parameters
Constant References
Data
Character Processing
Character Operations
Print the ASCII Set
Convert To Upper Case
Convert To Upper Case in C++
Remove Indentation
Input Statistics
Character Warnings
Basic Types
Builtin Types
Sizeof Operator
Type Conversion
Unsigned Pitfalls
Enum Declaration
Typedef Declaration
Arrays and Pointers
Array Echo
Basic Pointers
Swap Using Pointers
Arrays and Pointers
Array Passing
Character Histogram Generator
Integer Select Sort
Integer Quick Sort
Integer Library Sort
Array Error I
Array Error II
Array Error III
How Big Do You Want That Array, Sir?
Dynamic Array Allocation
Variable-Sized Arrays
Plain C Strings
C Strings
C String Facility
String Copy Implementation I
String Copy Implementation II
String Copy Implementation III
Simple String Sort
String Sort With Dynamic Allocation
List Command-Line Arguments.
Simple String Formatter
String Error
Structs and Classes
C Structs
Point With Struct
C++ Point Class I
C++ Point Class II
Linked List (Structs)
Linked List (Classes)
Multi-File Programs
Introduction
C Point ADT
Point Header File
Point Implementation File
Point Driver File
C++ Point ADT I
Point Class Header File
Point Class User File
C Point ADT II
Point Class Header File II
Point Class Implementation File II
Point Class User File II
C Point ADT III
Point Class Header File III
Point Class Implementation File III
Point Class User File III
C Integer Stack ADT
C Integer Stack ADT
C Integer Stack Implementation
C Integer Stack Driver
C++ Integer Stack ADT
C++ Integer Stack ADT
C++ Integer Stack Implementation
C++ Integer Stack Driver
Inheritance
Basics
PrintMe Class
PrintMe2 Class
PrintMeLots Class
PrintMeLots2 Class
PrintMe Test Driver
Stack
Generalized Stack Class
Generalized Stack Implementation
Generalized Stack Driver
Ugly Shapes
Ugly Shapes
Text Canvas Class
Text Canvas Implementation
Shape Classes
Shape Implementations
Compound Shape Class
Compound Shape Implementation
Shape Driver I
Shape Driver II
Shape Driver III
Shape Driver IV
Multiple Inheritance
Expression Calculator
Linked Lists
Linked List Node Classes
Linked List Classes
Linked List Implementation
Linked List Driver
Templates
Template Functions
Template Stack
Template Generalized Stack
Template Generalized Stack Driver
Array Template Stack
Array Template Stack
Array Template Test Driver
Vector Demo
Vector Demo With Iterators
Fast Primes Generator
Prime Generator Algorithm
Composite Number Eliminator Class
Composite Number Eliminator Implementation
Fast Primes Generator