0% found this document useful (0 votes)
23 views6 pages

S1 2 Handout

The document provides an overview of basic C++ language features and concepts, including: 1) C++ is an object-oriented programming language that builds on C and provides features like polymorphism, templates, and inheritance. 2) Key concepts discussed include expressions, statements, blocks, scope, operators, and control structures like conditionals and loops. 3) Variables represent labeled boxes in memory that contain values and have a specific data type determining their size.

Uploaded by

J Reach (JR3ach)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

S1 2 Handout

The document provides an overview of basic C++ language features and concepts, including: 1) C++ is an object-oriented programming language that builds on C and provides features like polymorphism, templates, and inheritance. 2) Key concepts discussed include expressions, statements, blocks, scope, operators, and control structures like conditionals and loops. 3) Variables represent labeled boxes in memory that contain values and have a specific data type determining their size.

Uploaded by

J Reach (JR3ach)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Overview (cont.

)
Section 1.2
Basic Language Features ● Characteristics

➢ provides high-level OO functionality


1. Overview  polymorphism

2. Terminology  generic programming using templates


 operator overloading
3. Operators  multiple inheritance

4. Control and data structures  exception handling

5. Variables and data types ➢ allows calls to low-level C functions

6. Standard I/O streams


7. Functions
8. References
1 ©2018 Christine Laurendeau 3

1.2.1 Overview 1.2.2 Terminology


● History of C++ ● Expression
➢ sequence of operations that resolve to a single value
➢ general purpose object-oriented programming language ➢ examples:
 a + b ­ 3 * c
➢ developed by Bjarne Stroustrup, starting in 1979  d * pow(g, a)

➢ “C with classes”, i.e. object-oriented C


● Statement
➢ expression terminated by a semi-colon
➢ examples:
 tmpValue = a + b ­ 3 * c;
 what happens here: d * pow(g, a);
➢ statements also resolve to a single value
 value may be void

©2018 Christine Laurendeau 2 ©2018 Christine Laurendeau 4


Terminology (cont.) 1.2.3 Operators
● Block ● Types of operators:
➢ sequence of statements between a pair of matching braces
➢ arithmetic: +  ­  *  /  %  ++ ­­
➢ examples:
 functions ➢ relational: ==  !=  <  >  <=  >=
 loops, if-statements
 free-floating blocks! ➢ logical: &&  ||  !

➢ spot the difference: ➢ bitwise: ~  &  |  ^  >>  <<


int x = 8, size = 5, i = 0; int x = 8, size = 5, i = 0;
while (i <= size) { while (i <= size) {
  x = i;      int x = i;
➢ assignment: =  +=  ­=  *=  /=  %=
  ++i;   ++i;
} }
cout << x << endl; cout << x << endl; ➢ conditional: ?:

©2018 Christine Laurendeau 5 ©2018 Christine Laurendeau 7

Terminology (cont.) Operators (cont.)


● Scope ● Characteristics of operators:
➢ the part of the program where a variable can be used ➢ arity
 number of operands
● Block scope:  operators can be unary, binary, or ternary

➢ variables are declared inside a block


➢ precedence
 these are called local variables
 order in which operators execute
➢ any kind of block can have local variables  remember BEDMAS?

● Global scope (also called file scope) ➢ associativity


➢ variables are declared outside of any block  order in which operators of the same precedence execute
 these are called global variables  associativity can be left-to-right, or right-to-left
➢ industrial quality software never uses global variables

©2018 Christine Laurendeau 6 ©2018 Christine Laurendeau 8


1.2.4 Control and Data Structures 1.2.5 Variables and Data Types
● Control structures ● Program memory is like a bookshelf full of boxes
➢ the boxes have different sizes
➢ conditional ➢ each box has a label or name
 example: if­else
➢ each box contains something

➢ selective
➢ each box is in a specific location on the shelf
 example: switch
● Variables are like those boxes
➢ iterative ➢ they come in different sizes, based on data type
 example: for, while, do­while  examples: int, double, char
➢ they have a name
➢ jump ➢ they contain something, called a value
 example: break, continue
➢ they have a location in memory
 represented as a memory address where the value is stored

©2018 Christine Laurendeau 9 ©2018 Christine Laurendeau 11

Control and Data Structures (cont.) Variables and Data Types (cont.)
● Data structures ● Characteristics of variables:

➢ C-style structs ➢ name


 in C++, they have variables and functions, just like classes  identifier that the program uses to access or modify the content
 by default, their members are public
 bad software engineering ➢ value
 content of the variable
➢ classes
 like Java, they have variables and functions ➢ data type
 by default, their members are private  determines the number of bytes required in memory
 using classes follows the principle of least privilege
 good software engineering ➢ location in memory
 address in memory where the value is stored

©2018 Christine Laurendeau 10 ©2018 Christine Laurendeau 12


Variables and Data Types (cont.) 1.2.7 Functions
● Data types ● In C++, there are two types of functions:

➢ primitive (built-in) ➢ global functions


 examples: int, float, double, char, bool  defined outside of any block
 available to all functions and classes in the program
➢ user defined  example: main()
 defined by programmer (you!)
 these are your classes ➢ member functions
 defined within a class
➢ memory address  may be accessed on an object of that class
 example: pointers  static member functions can also be accessed on the class itself

©2018 Christine Laurendeau 13 ©2018 Christine Laurendeau 15

1.2.6 Standard I/O Streams Functions (cont.)


● C++ library stream objects ● Important terminology:
➢ declared in iostream library
➢ function declaration
➢ encapsulated in std namespace
 function prototype/signature

● cout ➢ function implementation


➢ ostream object used for standard output  the code between the braces
 the “body” of the function

● cin
➢ istream object used for standard input
● Member functions:

➢ declaration and implementation are stored in different files


● cerr
➢ ostream object used for standard error

©2018 Christine Laurendeau 14 ©2018 Christine Laurendeau 16


Function Design Function Design (cont.)
● Characteristics of correctly designed functions: ● Parameters

➢ take data in, do something, return result ➢ they are treated as local variables inside a function
 two ways to return results: return value or using parameters
● Parameter modifiability:
➢ are single-purpose
 have a single goal, do one thing only ➢ pass-by-value
 parameter value is copied from the calling function
➢ encapsulate (hide) their functionality  function works on local copy
 other programmers know what function does, not how it does it
➢ pass-by-reference
➢ should be reusable  parameter is memory address of variable declared in calling function
 in the same program, or in other programs  allow variables in calling function to be modified
 in C++, this can be done using pointers or references

©2018 Christine Laurendeau 17 ©2018 Christine Laurendeau 19

Function Design (cont.) Function Design (cont.)


● Return values: ● Every parameter has a role to play in the function:

➢ often used to indicate success or failure ➢ input parameter


 value required by function to do its work
➢ used to return simple results from very simple functions  data sent by calling function
 mostly “getter” functions
➢ output parameter
➢ normally, results are returned using an output parameter  stores a result of the function doing its work
 this allows multiple values to be returned to calling function  this is how results are correctly “returned” to the calling function
 ... more on this coming up...
➢ input-output parameter
 plays both an input parameter role, and an output parameter role
 its value is needed by the function
 it gets modified by the function

©2018 Christine Laurendeau 18 ©2018 Christine Laurendeau 20


1.2.8 References
● To pass parameters by reference:

➢ we need to use a memory address as a variable

➢ best way to do this is with pointers


 pointers are very powerful, but can be difficult to learn
 ... more on pointers later ...

➢ alternative to pointers: references


 used for pass-by-reference
 not very versatile

©2018 Christine Laurendeau 21

References (cont.)
● What is a reference not?
➢ a reference is not a variable! it does not occupy memory
➢ it must be assigned a value on declaration
➢ its value can never change

● So what is it?
➢ a binding, or an alias, for an existing variable
➢ declaring and initializing a reference creates a bond between:
 the reference name
 an existing variable
➢ the bond is unbreakable
➢ most common use is passing parameters by reference

©2018 Christine Laurendeau 22

You might also like