SlideShare a Scribd company logo
The Structure of a
Program
Michael Heron
Introduction
• Computer programs in C++ are broken up into several parts.
• For various reasons, which we will discuss as we go through the
module.
• In this lecture, we’re going to look at the big picture of what
parts make up a program.
• We’ll get to the detail in later lectures.
• Some of this will be quite abstract at the moment.
A C++ Program
• A properly designed C++ program consists of:
• One or more objects.
• These objects are defined by classes.
• Each object consists of:
• Attributes
• Behaviours
• Each behaviour consists of:
• Variables
• Selection structures
• Repetition structures
• Data structures
Objects and Classes
• Conceptually the most difficult thing to visualise.
• So don’t worry about it too much for now.
• C++ is an object oriented programming language.
• Except, not really.
• We will be looking at writing object oriented programs.
• This is a framework common to many programming languages.
• Java and C# being two very popular examples.
The Structure of an Object
• We’ll begin closer to home.
• With the idea of an object.
• An object is an instance of a class.
• A class is like a blueprint telling the object what structure it has.
• A class defines:
• Attributes
• Behaviours
• The object defines
• State
Eh?
• There exists, somewhere, a blueprint for a chair.
• Blueprints for the chairs on which you are sitting.
• The blueprint defines what the chair looks like.
• It defines the structure of the chair
• It defines the relationship of the legs to the seat
• This blueprint would be the class.
• The specific chairs on which you are sitting would be objects.
Uh…
• The blueprint tells us how the chair is supposed to behave and
what information about that chair may be mutable.
• The colour of the chair
• The material of the chair
• The size of the chair
• The class says:
• A chair has a colour, material, and size
• The object says:
• I am blue, made of leather, and is medium sized.
Okay!
• We’ll come back to this subject later.
• Because regardless of what people may tell you, object
orientation does not come naturally to most people.
• Suffice to say that an object is one of the building blocks of an
object oriented program.
• Java and C# require you to use objects.
• C++ lets you use them if you like.
Attributes
• Attributes are things that an object will have.
• Usually things that are mutable (they can change).
• Consider a human face.
• It has eyes
• It has a nose
• It has a mouth
• These are modeled in a computer program as variables.
• A class is thus a collection of variables.
Behaviours
• As well as these variables, a class contains behaviours for
acting upon those variables.
• If the class is a human face:
• Attributes: Eyes, Mouth, Nose
• Behaviours: blink, smile, sniff
• These are modeled in a computer program as functions.
• Also called methods.
• Two names for the same thing, we’ll use these interchangeably.
Behaviours
• Behaviours can be broken down into further parts.
• Behaviours may have variables of their own.
• Temporary variables that only exist as long as the method is
executing.
• Behaviours will usually incorporate programming structures.
• Some structures allow the programmer to choose between courses
of actions.
• Some structures allow the programmer to repeat blocks of code.
My First Program
• The simplest program you can write in C++ is the following:
#include <iostream>
int main() {
cout << "Hello Dundee University!" << endl;
return 1;
}
The Program
• The first part is known as a preprocessor directive.
• More on this later.
• Main is the starting point of the program.
• It’s a function.
• C++ will look for this when you tell it to compile and execute a
program.
• cout is a special keyword in C++
• It refers to the standard output device.
The Program
• The << symbols are known as operators.
• These are a big part of C++, so we’ll come back to these.
• In this case, they mean ‘send the follow text to the standard
output device’
• endl is a special keyword.
• It means ‘a line end symbol’
• Return means very little in this context.
• We’ll come back to it in future lectures.
Experimentation
• The key to successful programming is experimentation.
• You have to be willing to play around with code to see what happens.
• One of the questions I am asked most often is ‘what happens when I
do this?’
• You don’t need to ask me.
• Make a backup of your code, and try it yourself.
• Your code is just plain text
• You can save it in notepad
A Second C++ Program
#include <iostream>
int main() {
int age;
cout << "How old are you?" << endl;
cin >> age;
cout << "You are " << age << " years old" << endl;
}
New Features
• A variable!
• int age
• User input
• cin is keyboard input
• Notice that the >> goes the other way
• Display of the contents of a variable to the user
• Sending age to the standard output
Variables
• Variables are little boxes we use to hold data when we don’t
know in advance what’s going to go in them.
• Common when dealing with user input.
• Variables have two parts to them.
• A name
• A type
• The type we have used here is int
• Short for integer, which is a whole number.
Variables
• A variable has three parts to its lifecycle.
• It is created.
• Or declared.
• It is manipulated
• It has values assigned to it.
• We can set the variable to have certain values.
• age = 100;
• It has the value queried.
• It is destroyed.
• This is done automatically for this variable.
• Not so automatically for others.
Variables
• C++ offers support for different kinds of variable.
• float
• Floating point numbers
• char
• Single alphanumeric characters
• bool
• True or false
Variables
• C++ also offers support for strings.
• But not natively.
• String support can be enabled by adding the following to your
program after the #include:
• using namespace std;
• From that point on, the string data type is available to you.
A Third Program
#include <iostream>
using namespace std;
int main() {
string name;
cout << "What is your name?" << endl;
cin >> name;
cout << "Your name is " << name << endl;
}
Arithmetic Operators
• For the numeric data types, we can perform simple arithmetic on them
using the arithmetic operators.
• +
• Addition
• -
• Subtraction
• *
• Multiplication
• /
• Division
• %
• Modulo division
A Fourth Program
#include <iostream>
using namespace std;
int main() {
int num1, num2;
int answer;
cout << "What is your first number?" << endl;
cin >> num1;
cout << "What is your second number?" << endl;
cin >> num2;
answer = num1 + num2;
cout << "Your answer is " << answer << endl;
}
Summary
• C++ Programs are made up of many parts
• Objects, classes
• Variables and Structures
• We’ve looked at one of these today in any depth.
• The variable.
• Variables are the building blocks of a program.
• They let us deal with ambiguity, the unknown, and complexity.

More Related Content

PPTX
Object oriented programming
PPTX
2CPP19 - Summation
PPTX
Object Oriented Programming Principles
PPTX
How Functional Programming Made Me A Better Developer
PDF
Preparing for Scala 3
PPT
OOP programming
PDF
12. Objects I
Object oriented programming
2CPP19 - Summation
Object Oriented Programming Principles
How Functional Programming Made Me A Better Developer
Preparing for Scala 3
OOP programming
12. Objects I

What's hot (12)

PPTX
PPT
Lambdas
PDF
Domain-Driven Design
PPTX
Js tips & tricks
PDF
itft-Fundamentals of object–oriented programming in java
PPTX
Evolve Your Code
PPTX
SKILLWISE - OOPS CONCEPT
PPT
Object-Oriented Concepts
PPTX
Programming Paradigm & Languages
PPTX
Expression language
PDF
Python's dynamic nature (rough slides, November 2004)
PPT
C sharp
Lambdas
Domain-Driven Design
Js tips & tricks
itft-Fundamentals of object–oriented programming in java
Evolve Your Code
SKILLWISE - OOPS CONCEPT
Object-Oriented Concepts
Programming Paradigm & Languages
Expression language
Python's dynamic nature (rough slides, November 2004)
C sharp
Ad

Viewers also liked (20)

PPTX
Hr unit 2012 part 1
PPTX
Time unit 2012 nov part 2
PPTX
Cost unit 2013 part 2. 24 jan
PPTX
Quality unit 2012 part 1. day 1 - 31 jan
PPTX
Cost unit 2013 part 1. 17 jan
PPTX
Scope part 2 day 1
PPTX
Record keeping..
PPTX
Start a Business with Business Model Canvas
PDF
Driversmanual
PPT
Let's Start a Business
PPT
Session 2 positive thinking and attitude ( SMS )
PPTX
Startup Toolkit “Stop Dreaming Start Business”
PPTX
Start Your Own Business
PPTX
A to Z personality theories - A complete guide to human behavior
PDF
P3M - Project, Program, Portfolio Management Framework
PPTX
How to Start a Small Business
PPTX
Road safety defensive driver`s driving training mannual
PPTX
RECORDS & REPORTS
PPTX
Positive Attitude Presentation
Hr unit 2012 part 1
Time unit 2012 nov part 2
Cost unit 2013 part 2. 24 jan
Quality unit 2012 part 1. day 1 - 31 jan
Cost unit 2013 part 1. 17 jan
Scope part 2 day 1
Record keeping..
Start a Business with Business Model Canvas
Driversmanual
Let's Start a Business
Session 2 positive thinking and attitude ( SMS )
Startup Toolkit “Stop Dreaming Start Business”
Start Your Own Business
A to Z personality theories - A complete guide to human behavior
P3M - Project, Program, Portfolio Management Framework
How to Start a Small Business
Road safety defensive driver`s driving training mannual
RECORDS & REPORTS
Positive Attitude Presentation
Ad

Similar to CPP02 - The Structure of a Program (20)

PPTX
CPP19 - Revision
PPTX
CPP13 - Object Orientation
PPTX
Object Oriented Programming Using C++.pptx
PPTX
CPP16 - Object Design
PPTX
CPP03 - Repetition
PPTX
PDF
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
PPTX
[OOP - Lec 01] Introduction to OOP
PDF
INTRODUCTION TO C PROGRAMMING in basic c language
PPT
01 introduction to cpp
PPTX
Introduction to C ++.pptx
PPTX
2CPP02 - C++ Primer
PPTX
Presentation on topic of c and c++ programming language.(.pptx
PPTX
2CPP04 - Objects and Classes
PPTX
C++ l 1
PPTX
2CPP03 - Object Orientation Fundamentals
PPTX
CPP01 - Introduction to C++
PPT
Introduction to the intermediate Python - v1.1
PPTX
Programming using C++ - slides.pptx
PPTX
CPP06 - Functions
CPP19 - Revision
CPP13 - Object Orientation
Object Oriented Programming Using C++.pptx
CPP16 - Object Design
CPP03 - Repetition
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
[OOP - Lec 01] Introduction to OOP
INTRODUCTION TO C PROGRAMMING in basic c language
01 introduction to cpp
Introduction to C ++.pptx
2CPP02 - C++ Primer
Presentation on topic of c and c++ programming language.(.pptx
2CPP04 - Objects and Classes
C++ l 1
2CPP03 - Object Orientation Fundamentals
CPP01 - Introduction to C++
Introduction to the intermediate Python - v1.1
Programming using C++ - slides.pptx
CPP06 - Functions

More from Michael Heron (20)

PPTX
Meeple centred design - Board Game Accessibility
PPTX
Musings on misconduct
PDF
Accessibility Support with the ACCESS Framework
PDF
ACCESS: A Technical Framework for Adaptive Accessibility Support
PPTX
Authorship and Autership
PDF
Text parser based interaction
PPTX
SAD04 - Inheritance
PPT
GRPHICS08 - Raytracing and Radiosity
PPT
GRPHICS07 - Textures
PPT
GRPHICS06 - Shading
PPT
GRPHICS05 - Rendering (2)
PPT
GRPHICS04 - Rendering (1)
PPTX
GRPHICS03 - Graphical Representation
PPTX
GRPHICS02 - Creating 3D Graphics
PPTX
GRPHICS01 - Introduction to 3D Graphics
PPT
GRPHICS09 - Art Appreciation
PPTX
2CPP18 - Modifiers
PPTX
2CPP17 - File IO
PPT
2CPP16 - STL
PPT
2CPP15 - Templates
Meeple centred design - Board Game Accessibility
Musings on misconduct
Accessibility Support with the ACCESS Framework
ACCESS: A Technical Framework for Adaptive Accessibility Support
Authorship and Autership
Text parser based interaction
SAD04 - Inheritance
GRPHICS08 - Raytracing and Radiosity
GRPHICS07 - Textures
GRPHICS06 - Shading
GRPHICS05 - Rendering (2)
GRPHICS04 - Rendering (1)
GRPHICS03 - Graphical Representation
GRPHICS02 - Creating 3D Graphics
GRPHICS01 - Introduction to 3D Graphics
GRPHICS09 - Art Appreciation
2CPP18 - Modifiers
2CPP17 - File IO
2CPP16 - STL
2CPP15 - Templates

Recently uploaded (20)

PDF
Understanding Forklifts - TECH EHS Solution
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
assetexplorer- product-overview - presentation
PDF
Digital Strategies for Manufacturing Companies
PPTX
history of c programming in notes for students .pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
L1 - Introduction to python Backend.pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
ai tools demonstartion for schools and inter college
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Transform Your Business with a Software ERP System
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Introduction to Artificial Intelligence
Understanding Forklifts - TECH EHS Solution
wealthsignaloriginal-com-DS-text-... (1).pdf
assetexplorer- product-overview - presentation
Digital Strategies for Manufacturing Companies
history of c programming in notes for students .pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
L1 - Introduction to python Backend.pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Nekopoi APK 2025 free lastest update
Navsoft: AI-Powered Business Solutions & Custom Software Development
Softaken Excel to vCard Converter Software.pdf
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
ai tools demonstartion for schools and inter college
How to Migrate SBCGlobal Email to Yahoo Easily
Digital Systems & Binary Numbers (comprehensive )
Transform Your Business with a Software ERP System
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Introduction to Artificial Intelligence

CPP02 - The Structure of a Program

  • 1. The Structure of a Program Michael Heron
  • 2. Introduction • Computer programs in C++ are broken up into several parts. • For various reasons, which we will discuss as we go through the module. • In this lecture, we’re going to look at the big picture of what parts make up a program. • We’ll get to the detail in later lectures. • Some of this will be quite abstract at the moment.
  • 3. A C++ Program • A properly designed C++ program consists of: • One or more objects. • These objects are defined by classes. • Each object consists of: • Attributes • Behaviours • Each behaviour consists of: • Variables • Selection structures • Repetition structures • Data structures
  • 4. Objects and Classes • Conceptually the most difficult thing to visualise. • So don’t worry about it too much for now. • C++ is an object oriented programming language. • Except, not really. • We will be looking at writing object oriented programs. • This is a framework common to many programming languages. • Java and C# being two very popular examples.
  • 5. The Structure of an Object • We’ll begin closer to home. • With the idea of an object. • An object is an instance of a class. • A class is like a blueprint telling the object what structure it has. • A class defines: • Attributes • Behaviours • The object defines • State
  • 6. Eh? • There exists, somewhere, a blueprint for a chair. • Blueprints for the chairs on which you are sitting. • The blueprint defines what the chair looks like. • It defines the structure of the chair • It defines the relationship of the legs to the seat • This blueprint would be the class. • The specific chairs on which you are sitting would be objects.
  • 7. Uh… • The blueprint tells us how the chair is supposed to behave and what information about that chair may be mutable. • The colour of the chair • The material of the chair • The size of the chair • The class says: • A chair has a colour, material, and size • The object says: • I am blue, made of leather, and is medium sized.
  • 8. Okay! • We’ll come back to this subject later. • Because regardless of what people may tell you, object orientation does not come naturally to most people. • Suffice to say that an object is one of the building blocks of an object oriented program. • Java and C# require you to use objects. • C++ lets you use them if you like.
  • 9. Attributes • Attributes are things that an object will have. • Usually things that are mutable (they can change). • Consider a human face. • It has eyes • It has a nose • It has a mouth • These are modeled in a computer program as variables. • A class is thus a collection of variables.
  • 10. Behaviours • As well as these variables, a class contains behaviours for acting upon those variables. • If the class is a human face: • Attributes: Eyes, Mouth, Nose • Behaviours: blink, smile, sniff • These are modeled in a computer program as functions. • Also called methods. • Two names for the same thing, we’ll use these interchangeably.
  • 11. Behaviours • Behaviours can be broken down into further parts. • Behaviours may have variables of their own. • Temporary variables that only exist as long as the method is executing. • Behaviours will usually incorporate programming structures. • Some structures allow the programmer to choose between courses of actions. • Some structures allow the programmer to repeat blocks of code.
  • 12. My First Program • The simplest program you can write in C++ is the following: #include <iostream> int main() { cout << "Hello Dundee University!" << endl; return 1; }
  • 13. The Program • The first part is known as a preprocessor directive. • More on this later. • Main is the starting point of the program. • It’s a function. • C++ will look for this when you tell it to compile and execute a program. • cout is a special keyword in C++ • It refers to the standard output device.
  • 14. The Program • The << symbols are known as operators. • These are a big part of C++, so we’ll come back to these. • In this case, they mean ‘send the follow text to the standard output device’ • endl is a special keyword. • It means ‘a line end symbol’ • Return means very little in this context. • We’ll come back to it in future lectures.
  • 15. Experimentation • The key to successful programming is experimentation. • You have to be willing to play around with code to see what happens. • One of the questions I am asked most often is ‘what happens when I do this?’ • You don’t need to ask me. • Make a backup of your code, and try it yourself. • Your code is just plain text • You can save it in notepad
  • 16. A Second C++ Program #include <iostream> int main() { int age; cout << "How old are you?" << endl; cin >> age; cout << "You are " << age << " years old" << endl; }
  • 17. New Features • A variable! • int age • User input • cin is keyboard input • Notice that the >> goes the other way • Display of the contents of a variable to the user • Sending age to the standard output
  • 18. Variables • Variables are little boxes we use to hold data when we don’t know in advance what’s going to go in them. • Common when dealing with user input. • Variables have two parts to them. • A name • A type • The type we have used here is int • Short for integer, which is a whole number.
  • 19. Variables • A variable has three parts to its lifecycle. • It is created. • Or declared. • It is manipulated • It has values assigned to it. • We can set the variable to have certain values. • age = 100; • It has the value queried. • It is destroyed. • This is done automatically for this variable. • Not so automatically for others.
  • 20. Variables • C++ offers support for different kinds of variable. • float • Floating point numbers • char • Single alphanumeric characters • bool • True or false
  • 21. Variables • C++ also offers support for strings. • But not natively. • String support can be enabled by adding the following to your program after the #include: • using namespace std; • From that point on, the string data type is available to you.
  • 22. A Third Program #include <iostream> using namespace std; int main() { string name; cout << "What is your name?" << endl; cin >> name; cout << "Your name is " << name << endl; }
  • 23. Arithmetic Operators • For the numeric data types, we can perform simple arithmetic on them using the arithmetic operators. • + • Addition • - • Subtraction • * • Multiplication • / • Division • % • Modulo division
  • 24. A Fourth Program #include <iostream> using namespace std; int main() { int num1, num2; int answer; cout << "What is your first number?" << endl; cin >> num1; cout << "What is your second number?" << endl; cin >> num2; answer = num1 + num2; cout << "Your answer is " << answer << endl; }
  • 25. Summary • C++ Programs are made up of many parts • Objects, classes • Variables and Structures • We’ve looked at one of these today in any depth. • The variable. • Variables are the building blocks of a program. • They let us deal with ambiguity, the unknown, and complexity.