You are on page 1of 22

COMS12800

Department of Computer Science

University of Bristol, UK

Introduction to C++
Lecturer:

Dr Tilo Burghardt

( mailto: tilo@cs.bris.ac.uk )

Web Materials:

http://www.cs.bris.ac.uk/Teaching/Resources/COMS12800

LECTURE 1
On Objects and Objectives:
A JOURNEY
FROM PROGRAMS
TO SOFTWARE

Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 1

Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 2

So far in your C course


you learned principles and practical skills of programming
in the imperative language C
you essentially implemented small procedural programs
that were able to solve tasks
by declaring typed variables and by defining procedures
by calling/interlinking procedures with arguments in a specific
way to implement your algorithm
by controlling memory (code & data) using pointers
by structuring your code into different files or modules

IN THIS COURSE
How can we change our programming approach better to
manage the requirements & challenges of more complex
software?
Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 3

To put it quite bluntly: as long as


there were no machines, programming
was no problem at all;
when we had a few weak computers,
programming became a mild problem,
and now we have gigantic computers,
programming has become an equally
gigantic problem.
--- E. Dijkstra
(1972 at Turing Award Lecture)

Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 4

Programming Languages grow alongside


Hardware Capabilities and Software Needs

Colossus Mark 2 (1940s)

Apple II (1977)
Tilo Burghardt

Leo I mainframe computer (1960s

Bristol Blue Crystal Supercomputer

Mobiles Devices

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 5

Introduction

Programming = Domain Modelling?


Software represents and interacts with
aspects of real or virtual worlds, their
elements, properties and behaviours.
CHALLENGES:

domains are large, complex and dynamic


often many people need to cooperate in
writing/maintaining software systems
target computers are heterogeneous
software should be reliable, robust, secure,
inexpensive and delivered on time,
yet also flexible, scalable and easy to
maintain, reuse, port and adapt

need for adequate implementation

language(s) and development support


Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 6

Introduction

Early Ideas for Structuring Imperative Programs


global
variables
memory/
variables

stateless
functions

instructions

main
program

procedure

module

unstructured
(e.g. ASSEMBLY 1950s)
Tilo Burghardt

procedural & modular


(e.g. C, PASCAL 1970s)

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 7

Historical Note

Family Tree of C++

(Simplified and Selective)

ASSEMBLY
FORTRAN 1956
ALGOL 1960
CPL 1963
BCPL 1966

LISP 1960

Smalltalk1980

Objective C 1986

Algol W 1966

Pascal 1970

Object Pascal 1986

Delphi 1995

B 1969
C 1972

C++ 1983
C++11 2011

John Backus
(Fortran-1956)
Tilo Burghardt

XC 2009
.

Java 1995

Martin Richards
(BCPL-1966)

C# 2001

Ken Thompson
(B-1969 ,C-1972)

Dennis Richie
(C-1972)

Bjarne Stroustrup
(C++ -1983)

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 8

Historical Note

A Jungle of Programming Languages

Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 9

Why learn C++?

long-standing ubiquity
object-oriented paradigm
high performance
timely evolutions/extensions
- (e.g. C++11, C++11AMP etc)

strong tool/community support


- (e.g. Visual Studio etc)

General Usage of Programming Languages


(source: TIOBE programming community index 2012)

conceptual basis for Java & C#


contains interface to C
- only use C within C++ for special purposes

effective and (often) efficient for


solving (big) engineering problems

General Usage of Programming Paradigms


(source: TIOBE programming community index 2012)

- flexible balance between high performance and


structural organization
Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 10

When to use C++? (general guideline only)

collaborative and complex projects


large performance-critical or real-time systems
reusable and flexible component software
close-to-operating-system or graphics-heavy applications
highly structured and C++ legacy-dependent systems

fast prototyping (e.g. use MatLab or special purpose software)


very small system programs (e.g. use C or even scripting)
web-centric applications (e.g. use Java, Pearl, Python etc)
Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 11

Key Concepts

Structuring Imperative Software


global
variables
memory/
variables
instructions

stateless
functions
main

main

stateful
objects
with
identity

unstructured
procedural & modular object oriented (OO)
(e.g. ASSEMBLY)
(e.g. C, PASCAL,...)
(e.g. C++, Java, Delphi...)
Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 12

Key Concept

Creating & Using Objects


// program fragment for using a card
#include <iostream>
// include the header file of class you wish to use
#include Card.h
// like in C, programs have a main as entry point
int main(void)
{

{
// create an object of type Card
// and initialise its properties

Card myCard;

INSTANTIATION:
class Card is a type and the programmer
can declare variables of that type, i.e. actual
objects or instances (such as myCard)
during declaration all the memory needed
for the properties of the object is allocated
(like malloc in C) and then a special method,
the constructor, is called for initialising the
properties of the object created (such as
setting the initial balance of the card etc)

// use the object by calling its methods

myCard.showId();
myCard.payLunch();
myCard.showBalance();
myCard.payLunch();

METHOD CALLS:
ask the object myCard to do something,
i.e. call the procedure printId from the
context of myCard

DESTRUCTION:
once myCard goes out of scope, a special
}
method, the destructor, is called to cleanup
the memory allocated throughout myCards
lifetime,
the object
is lost| forever
Tilo Burghardt | 2011-12 | Introduction to C++ COMS12800 | Computer
Science then
| University
of Bristol
Lecture 1 | 13
return EXIT_SUCCESS;

Key Concept

Many Objects, one Blueprint - the Class


// program fragment for using a card
#include <iostream>
// include the header file of class you wish to use
#include Card.h
// like in C, programs have a main as entry point
int main(void)
{
// create many objects of type Card
// and initialise their properties

Card myCard001;
Card myCard002;
Card myCard003;
return EXIT_SUCCESS;
}

Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 14

Key Concept

Defining a Class: BluePrint for a type of Object


// code fragment for
// REPRESENTING A CARD
//defining a blueprint for all Cards

typedef class Card {

defining a class
and giving it the
name Card

public: //contents anyone can access


//METHODS things a card CAN DO
Card();
~Card();
void showId();
void showBalance();
void payLunch();

//constructor
//destructor
//print id
//print balance
//reduce balance

//PROPERTIES things a card HAS


int id;
//id number
private: //contents hidden from others
int balance;

} Card;
Tilo Burghardt

//balance on card

the methods of
an object,
essentially a
collection of
procedures
the properties of
an object,
essentially a
collection of
fields similar to
a struct in C

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 15

Key Concept

Implementing a Class: BluePrint for Behaviour


#include <iostream>
#include Card.h"

defining a constructor
to initialise the properties

//constructor

Card::Card()
{
id = 0;
balance = 100;
};
//destructor

Card::~Card()

{};

//print card id to screen


void Card::showId()
{
std::cout << "Card ID: " << id << std::endl;
}
//print card balance to screen
void Card::showBalance()
{
std::cout << "Balance is: " << balance << std::endl;
}

defining an empty
destructornothing to
cleanup since nothing
is created in constructor
Defining the behaviour
of a card by providing
procedures or functions
that operate on the
properties (e.g. print
values to the screen,
change values etc)

//reduce balance to get some lunch!


void Card::payLunch()
{
balance--;
}

Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 16

Putting it all together

A First C++ Program is born


typedef class Card {
public:
Card();
~Card();
void showId();
void showBalance();
void payLunch();
int id;
private:
int balance;

#include <iostream>
#include Card.h"
Card::Card()
{
id = 0;
balance = 100;
};

#include <iostream>
#include Card.h
int main(void)
{
{
Card myCard;
myCard.showId();
myCard.payLunch();

Card::~Card() {};
void Card::showId()
{
std::cout << "Card ID: " <<
id << std::endl;
}

myCard.showBalance();
myCard.payLunch();
}
return EXIT_SUCCESS;

} Card;

void Card::showBalance()
{
std::cout << "Balance is: " <<
balance << std::endl;
}

void Card::payLunch()
{
balance--;
}

Structure of the Class


Tilo Burghardt

Behaviour of the Class

Usage of the Class

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 17

Development Environment:

Visual Studio

Download your own student copy of Visual Studio


at http://dreamspark.com
Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 18

Key Concept

So, what exactly is a Class?


A class can be understood as a
module / unit
a header file (e.g. myCard.h) and a source
file (e.g. myCard.cpp) which is compiled
separately

type / structure
with fields allowing you to define variables
(properties) and procedures (methods)

context /namespace
providing implicit locality to its methods
(e.g. dot-notation myCard.payLunch())
no name clashes between different classes

// code fragment for


// REPRESENTING A CARD
//defining a blueprint for all Cards
typedef class Card {
public: //contents anyone can access
//METHODS things a card CAN DO
Card();
~Card();
void showId();
void showBalance();
void payLunch();

//constructor
//destructor
//print id
//print balance
//reduce balance

//PROPERTIES things a card HAS


int id;
//id number
private: //contents hidden from others
int balance;

//balance on card

} Card;

essentially: building block


for designing small, reliable, compact components that
can be reused, refined but mainly combined to form complex programs
Tilo Burghardt | 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1

| 19

Some Principles of
Good Object Oriented Design (OOD)
Flexibility
you can easily adapt code to address new problems
Reusability
you avoid coding the same behaviour over and over again
Maintainability
you can easily add features
Low Coupling
objects are fairly independent so you can work in teams,
sharing
Scalability
small classes can be combined into complex programs
without loosing oversight and control
Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 20

Designing Objects Who does What?


When designing Objects
deciding who (i.e. which object/class)
is responsible for what (i.e. which tasks
and fields) is essential and often difficult
Little Task Aside:
Find out about the CRC technique where
you create a card for each Class, and on it
you write its Responsibilities and Collaborators
you play role-playing games to decide "who"
is responsible for "what
Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 21

Outlook to next Lecture

Basics of Inheritance
(Evolution & Specialisation for Classes)
Tilo Burghardt

| 2011-12 | Introduction to C++ COMS12800 | Computer Science | University of Bristol | Lecture 1 | 22

You might also like