Programming in C and C++
Programming in C and C++
2 / 21
C++ fundamental types
3 / 21
C++ enumeration
4 / 21
References
5 / 21
References in function arguments
I When used as a function parameter, a referenced value is not copied;
for example:
void inc(int& i) { i++;} //bad style?
I Declare a reference as const when no modification takes place
I It can be noticeably more efficient to pass a large struct by reference
I Implicit type conversion into a temporary takes place for a const
reference but results in an error otherwise; for example:
1 float fun1(float&);
2 float fun2(const float&);
3 void test() {
4 double v=3.141592654;
5 fun1(v); //Wrong
6 fun2(v);
7 }
1 void f(double);
2 void f(long);
3 void test() {
4 f(1L); //f(long)
5 f(1.0); //f(double)
6 f(1); //Wrong: f(long(1)) or f(double(1)) ?
7 / 21
Scoping and overloading
1 void f(int);
2
3 void example() {
4 void f(double);
5 f(1); //calls f(double);
6 }
8 / 21
Default function arguments
9 / 21
Namespaces
Related data can be grouped together in a namespace:
10 / 21
Using namespaces
I A namespace is a scope and expresses logical program structure
I It provides a way of collecting together related pieces of code
I A namespace without a name limits the scope of variables, functions
and classes within it to the local execution unit
I The same namespace can be declared in several source files
I The global function main() cannot be inside a namespace
I The use of a variable or function name from a different namespace
must be qualified with the appropriate namespace(s)
I The keyword using allows this qualification to be stated once, thereby
shortening names
I Can also be used to generate a hybrid namespace
I typedef can be used: typedef Some::Thing thing;
I A namespace can be defined more than once
I Allows, for example, internal and external library definitions
11 / 21
Example
3 namespace Module2 {
4 inline int sqr(const int& i) {return i*i;}
5 inline int halve(const int& i) {return i/2;}
6 }
7
12 / 21
Linking C and C++ code
1 extern "C" {
2 int globalvar; //definition
3 int f();
4 void g(int);
5 }
13 / 21
User-defined types
I C++ provides a means of defining classes and instantiating objects
I Classes contain both data storage and functions which operate on
storage
I Classes have access control: private, protected and public
I Classes are created with class or struct keywords
I struct members default to public access; class to private
I The constructor syntax is a member function with the same name as
the class
I The destructor syntax is a member function with the same name as
the class, prefixed with a tilde (~)
I A constructor can be overloaded to provide multiple instantiation
methods
I Can create static (i.e. per class) member variables
14 / 21
Example
1 class Complex {
2 double re,im;
3 public:
4 Complex(double r=0.0L, double i=0.0L);
5 };
6
7 Complex::Complex(double r,double i) {
8 re=r,im=i; // deprecated initialisation-by-assignment
9 }
10
11 int main() {
12 Complex c(2.0), d(), e(1,5.0L);
13 return 0;
14 }
15 / 21
Constructors and destructors
16 / 21
Copy constructor
17 / 21
Assignment operator
18 / 21
Constant member functions
19 / 21
Arrays and the free store
I An array of class objects can be defined if a class has a default
constructor
I C++ has a new operator to place items on the heap:
Complex* c = new Complex(3.4);
I Items on the heap exist until they are explicitly deleted:
delete c;
I Since C++ (like C) doesn’t distinguish between a pointer to a single
object and a pointer to an the first element of an array of objects,
array deletion needs different syntax:
1 Complex* c = new Complex[5];
2 ...
3 delete[] c; // Cannot use "delete" here, only "delete[]"
I When an object is deleted, the object destructor is invoked. When an
array is deleted, the object destructor is invoked on each element
I The C++ standard library provides std::vector or std::array, so
raw arrays are rarely used
20 / 21
Exercises
1. Write an implementation of a class LinkList which stores zero or
more positive integers internally as a linked list on the heap. The
class should provide appropriate constructors and destructors and a
method pop() to remove items from the head of the list. The method
pop() should return -1 if there are no remaining items. Your
implementation should override the copy constructor and assignment
operator to copy the linked-list structure between class instances. You
might like to test your implementation with the following:
1 int main() {
2 int test[] = {1,2,3,4,5};
3 LinkList l1(test+1,4), l2(test,5);
4 LinkList l3=l2, l4;
5 l4=l1;
6 printf("%d %d %d\n",l1.pop(),l3.pop(),l4.pop());
7 return 0;
8 }
Hint: heap allocation & deallocation should occur exactly once!
21 / 21