程序代写 COSC1076 Week 01

Getting Started in C++
COSC1076 Week 01

Copyright By PowCoder代写 加微信 powcoder

Primary reason: Learning Programming Skills & Techniques • Dynamic Memory Management
• More explicit program control
• Supported language feature set
Secondary reason: Learn a foundational & common language family • C++ is used for:
– Optimisation – Efficiency
• GPU Programming
Week 01 | Getting Started with C++ COSC1076

From 2019 Stack Overflow Survey (Professional Developers)
Week 01 | Getting Started with C++ COSC1076

C, C++, C++11, or C++14 ?
C++ is originally an extension to C
• C is a legal subset of C++
• Biggest introduction are Classes, Generics & the STL (standard template library)
• This course works with C++, but many concepts are perfectly fine in C
C++ has seen many standards, that require standard compliant compilers to consistently handle
• C++11 (2011), was a major overhaul to the language
• C++14 (2014), additional language feature, consistency updates, bug fixes,
– This is the version we are using
• C++17 (2017), latest standard, we won’t use this
Week 01 | Getting Started with C++ COSC1076

Learning a new Language Is a Skill

Java/C++ diff

Development Cycle
Week 01 | Getting Started with C++ COSC1076

C++ Program Structure
Header Includes Defines
Namespace uses Function Declarations Main Function Function Definitions
#include
#include
#define EXIT_SUCCESS 0
using std::cout; using std::cin;
double foo(int x, float y, char z); void bar(int x, float y, char z);
int main (void) { int i;
float f; char c; double d;
d = foo(i, f, c);
cout << "foo:\t" << d << "+" << f << "*" << c << "=" << d << std::endl; bar(i, f, c); cout << "bar:\t" << d << "+" << f << "*" << c << "=" << d << std::endl; return EXIT_SUCCESS; double foo(int x, float y, char z) { return x + y * z; } void bar(int x, float y, char z) { Week 01 | Getting Started with C++ COSC1076 Compiling and Running C++ Programs Before being executed, C++ programs must be compiled into Machine Code • Similar, but different from Java • Machine code is CPU (processor) specific Use GCC (g++) compiler g++ -Wall -Werror std=c++14 —O -o
Compiler options • -Wall
• -std=c++14 • -O
• -o
enable all error checking
convert warnings into errors, stopping compilation enable all c++14 features
turn on optimiser
output filename of executable
Week 01 | Getting Started with C++ COSC1076

The Basics – What is the same
Some Types
• float/double • char
• Arithmetic
• Comparison
• if / elseif / else
Iteration • While • For
Week 01 | Getting Started with C++ COSC1076

Differences
Standard I/O • cout / cin
• Extended types • Implicit casting
Declarations
• Parameter Passing
Global Variables
Namespaces Declare & Initialise?
Week 01 | Getting Started with C++ COSC1076

STL Strings
Like Java, the STL provides a string object • Contained in header
• Within the std namespace
Supported operations include:
• Assignment with “ ” – style syntax
• Concatenation with ‘+’ operator (of string objects!)
Has methods/functions that can be called
• c_str() – talk more about this next week • substr() – substring
• find() – find substring
Week 01 | Getting Started with C++ COSC1076

Standard I/O – C++ STL (cout)
For output, use the cout object
• Contained in the header • Within the std namespace
Uses the output operator (<<) << • Uses default formatting for output
• Returns a value – the output location • Allows operators to be chained
std::cout << 7 << ‘a’ << 4.567 << std::endl Week 01 | Getting Started with C++ COSC1076 Standard I/O - C++ STL (endl) Operating System independent newline character: • std::endl • Equivalent to using ‘\n’ character. These are the same: std::cout << 7 << std::endl std::cout << 7 << "\n" Week 01 | Getting Started with C++ COSC1076 Standard I/O - C++ STL (cin) For input, use the cin object • Contained in the header • Within the std namespace
Uses the input operator (>>)
>> • This is context sensitive!
• Uses the type of the input variable to determine what to read from input Example
std::cin >> x
std::cin >> y
Week 01 | Getting Started with C++ COSC1076

Standard I/O – C++ STL (cin)
What about:
• End of input?
• Input error or failure?
cin is an object – you should be familiar with these from Java • Has functions to check for these things
– eof() – check for end of file
– fail() – check for read error
• (More on classes and objects next week)
Week 01 | Getting Started with C++ COSC1076

Standard I/O – C++ STL
Other functions for reading that could be used: • std::getline()
• std::read()
• More on these later in the course, since we haven’t seen how to use their argument yet (need c-style strings)
Week 01 | Getting Started with C++ COSC1076

#define statements allow constants to be defined in the program • Syntax
#define DEFINE_NAME
• By convention, always use uppercase
• Placed at the top of the file (below headers)
They act as a literal “find-and-replace”, so be careful about: • Brackets
• ‘;’ for end-of-statement
Week 01 | Getting Started with C++ COSC1076

C/C++ Compilation Process
C++ Source File (.cpp)
Pre-processor Compiler Assembler File (.s) Object File (.o) Executable
Week 01 | Getting Started with C++

C/C++ Preprocessor
Prepare source code files for actual compilation Process ‘#’ pre-preprocessor directives
• Process#includestatements
– locates and includes header files
• Process#definestatements – find-and-replace
• Process#ifdefstatements – will see later
• Process#pragmastatements
– compiler specific directive, not used in this course
Week 01 | Getting Started with C++ COSC1076

Types may not be what they seem
Numbers represented true and false • 0 is false
• Any non-zero value is true
A bool is implemented as a number • false is always 0.
• But true is not necessarily 1.
A char is a signed 8-bit number.
• You can ‘add’ and ‘subtract’ characters, which does have uses
Week 01 | Getting Started with C++ COSC1076

The values a type can hold are dependent on the ‘size’ of the type: C++ has extended the following data types:
•{signed | unsigned} {long | short} int •{signed | unsigned} char
•{long} double
By convention, the sizes are:
long double
Week 01 | Getting Started with C++ COSC1076

Type Casting
C++ use implicit type casting to convert between compatible types • Typically this applies to numeric types
• Be careful!
– Implicit type conversion only happens when absolutely necessary Explicit type casting is done using bracket notation
(new type) value (int) 7.4f
Week 01 | Getting Started with C++ COSC1076

Declaration vs Definition vs Initialisation
Declaration
• Introduce a name (variable, class, function) into a scope • Fully specify all associate type information
Definition
• Fully specify (or describe) the name/entity
• All definitions are declarations, but not vice versa
Initialisation
• Assign a value to a variable for the first time
What happens if you define a variable without initialising it?
Week 01 | Getting Started with C++ COSC1076

Similar in concept to Java Methods
Functions are not associated with a class, and sit in the “Global” scope
• Functions must be declared before they can be used (called)
– A function declaration is also called a function prototype • Functions must only be defined once
– This can be after it is called
– It doesn’t not even have to be in the same cpp file! (more on this later)
Pass-by-value
• Pass-by-reference later (next week)
• Array passing (next week, more detail)
Week 01 | Getting Started with C++ COSC1076

Declaration (Prototype)
int foo(int x, double y);
Return type Name
Parameters
Definition
int foo(int x,
// Do stuff
Return Value
Week 01 | Getting Started with C++ COSC1076

Function calls operate through an approach called pass-by-value
• The value of the parameter is copied when it is given to the function
• Changing the parameter within a function does not modify the value from the calling entity
• This is similar to primitive types in Java
Calling Function
Called Function
pass-by-value
pass-by-value
Week 01 | Getting Started with C++ COSC1076

Similar to Java Arrays
• Largely syntactic difference when declaring • No need to “new” the array
int a[LENGTH]; • Can be initialised when declared
int a[LENGTH] = {1}; BUT, not automatic bounds checking!
• Cells “before” and “after” and start/end of the array can be accessed!
• It is the programmer’s responsibility to ensure that a program does not access outside an array’s limits.
Week 01 | Getting Started with C++ COSC1076

Multi-dimensional arrays • Again, similar to Java
int a[DIM1][DIM2]; • Inline initialisation is trickier
int a[DIM1][DIM2] = { {1,2,3}, {4,5,6}, …};
Week 01 | Getting Started with C++ COSC1076

Arrays are different** (sort-of)
• Arrays (as parameters) operate through pass-by-reference • The actual array is passed.
– Changing a value in the array within the called function modifies the value from the calling function
Calling Function
Called Function
pass-by-reference
** As we will see next week:
Under-the-hood an array is implemented using a pointer
The pointer is copied (pass-by-value)
The high-level effect to the programmer is pass-by-reference
Week 01 | Getting Started with C++ COSC1076

Namespaces
Define a new scope
• Similar to packages in Java
• Useful for organising large codebases
namespace myNamespace { … }
Function, Class, Variables, etc labels can be enclosed within a namespace • The namespace must be referenced to access the entity, using ::
::

entities can be exported
using std::cout
Everything in a namespace can be exported
using namespace std • This is banned within this course
The std namespace
• Most STL entities we will use exist within the std namespace
Week 01 | Getting Started with C++ COSC1076

Global Variables
So far, all variables have been defined within the scope of a function. • The variable only exists within that function
• The variable cannot be referenced from elsewhere
A variable defined outside of any function is global
• Can be used within any function, so long as the definition appears before the
variable is used
• These are incredibly bad design and style
Global variables are banned in this course
Week 01 | Getting Started with C++ COSC1076

C++ Style Guide

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com