Intro to C++ for Robotics Made for ALARM. Computer System Hardware – cpu, memory, i/o Software – OS, drivers.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Air Force Institute of Technology Electrical and Computer Engineering
Writing a Good Program 6. Pointers and Arrays
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
JavaScript, Third Edition
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Chapter 2: Using Data.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dennis Ritchie 1972 AT & T Bell laboratories (American Telephone and Telegraph) USA 1www.gowreeswar.com.
CSC 107 – Programming For Science. The Week’s Goal.
CSIS 113A Lecture 3 Conditional & Switch Glenn Stevenson CSIS 113A MSJC.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Copyright Curt Hill Variables What are they? Why do we need them?
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (3 left – as of last night). Office hours.
Chapter 5: Java Language Basics Java Statements –Declarations primitive types classes modifiers –Expressions operators –arithmetic, bitwise, comparison,
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Data types Data types Basic types
CISC/CMPE320 - Prof. McLeod
Computing with C# and the .NET Framework
Operators and Expressions
Chapter 15 Pointers, Dynamic Data, and Reference Types
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Basics of ‘C’.
C Operators, Operands, Expressions & Statements
CISC/CMPE320 - Prof. McLeod
2. Second Step for Learning C++ Programming • Data Type • Char • Float
CISC/CMPE320 - Prof. McLeod
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
C Language B. DHIVYA 17PCA140 II MCA.
OPERATORS in C Programming
Presentation transcript:

Intro to C++ for Robotics Made for ALARM

Computer System Hardware – cpu, memory, i/o Software – OS, drivers

Steps in writing software From ideas to stuff a computer will run

C++ - a quick intro Look at some code

IDE (Integrated Development Environment) Editing Saving Compiling Linking Debugging

Breakpoints, watching variables, stepping

C++ - program structure.h - Header files – contain definitions – aka signatures.cc – contain the “code” or how to do what it does

Compiler Directives #include – brings in definition files #define – sets up literal names for unchanging values

C++ Basic Program structure Main Where everything starts, creates and calls other software until task is done. Statement Ends in ; White space does not matter to the compiler, but matter a LOT to the engineer. Compound statements are contained in { } Attributes (aka variables) Methods (aka functions)

C++ Arithmetic Operators Operator NameSyntax UnaryUnary Plus +a+a Addition (Sum) a + b Prefix IncrementIncrement ++a Postfix IncrementIncrement a++ Assignment by Addition a += b Unary Minus (Negation) -a-a Subtraction (Difference) a - b Prefix DecrementDecrement --a Postfix DecrementDecrement a-- Assignment by Subtraction a -= b Multiplication (Product) a * b Assignment by Multiplication a *= b Division (Quotient) a / b Assignment by Division a /= b ModulusModulus (Remainder) a % b Assignment by Modulus a %= b

C++ Comparison Operators Operator NameSyntax Less Than a < b Less Than or Equal To a <= b Greater Than a > b Greater Than or Equal To a >= b Not Equal To a != b Equal To a == b Logical Negation !a!a Logical AND a && b Logical OR a || b Any operation can given a True or False value 0 is False Anything else is True 2 + 3; is True 0 / 1234; is False Comparison operators return an integer (0 for False, 1 for True) int i = !( 2 >= 5);

C++ Bitwise Operators Operator NameSyntax Bitwise Left Shift a << b Assignment by Bitwise Left Shift a <<= b Bitwise Right Shift a >> b Assignment by Bitwise Right Shift a >>= b Bitwise One's Complement ~a~a Bitwise AND a & b Assignment by Bitwise AND a &= b Bitwise OR a | b Assignment by Bitwise OR a |= b Bitwise XOR a ^ b Assignment by Bitwise XOR a ^= b

C++ Other Operators Basic Assignment a = b FunctionFunction, Call a() ArrayArray Subscript a[b]a[b] IndirectionIndirection (Dereference) *a*a Address-ofAddress-of (Reference) &a&a Member by PointerPointer a->b Member a.ba.b Member by Pointer Function Pointer Indirection a->*b Member Function Pointer Indirection a.*b Cast (type) atype Comma a, b Ternary Conditional a ? b : c Scope Resolution a::b Member Function Pointer a::*b Size-of sizeof a sizeof(t ype)t ype Type Identification typeid(a) typeid(t ype)t ype Allocate Storage new typetype Allocate Storage (Array) new type[n]type Deallocate Storage delete a Deallocate Storage (Array) delete[] a

C++ Operators – try out some math Math - addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). Code up this fragment in a new project and find the answers. int n1 = 7; int n2 = 2; int ans1 = 0, ans2 = 0, ans3 = 0, ans4 = 0, ans5 = 0; ans1 = n1 + n2; ans2 = n1 - n2; ans3 = n1 * n2; ans4 = n1 / n2; ans5 = n1 % n2;

C++ Program structure – For loop for (int i = 0; i < MAX_NUMBER_OF_CATS + 1; i++) { listOfCats[i] = NULL; }

C++ Program structure – While loop while (cat_p != NULL) { delete cat_p; cat_p = listOfCats[++postionInList]; }

C++ Program structure – Do loop So what does this code fragment do? char ch = ‘a’; do { cout << ch << ‘ ‘; ch++; } while ( ch <= ‘z’ ); Now see what other characters are out there, change the initial value of ch to 0 (not ‘0’) and the final value to 254 (not ‘254’). (Why did I pick that range?, try going over 254 and see what happens.)

C++ Program structure – If/else if (sensor_inputs.raw_direction_cmd < 123) { if (Front_cal_needed) { pwm06 = CALIBRATE_RIGHT_SPEED; CALIBRATE_FRONT_LED = LED_ON; /* signal calibrating the front */ } else { pwm05 = CALIBRATE_RIGHT_SPEED; CALIBRATE_BACK_LED = LED_ON; /* signal calibrating the back */ } } else if (sensor_inputs.raw_direction_cmd > 133) { if (Front_cal_needed) { pwm06 = CALIBRATE_LEFT_SPEED; CALIBRATE_FRONT_LED = LED_ON; /* signal calibrating the front */ } else { /* assume it is back */ pwm05 = CALIBRATE_LEFT_SPEED; CALIBRATE_BACK_LED = LED_ON; /* signal calibrating the back */ }

C++ Program structure – switch char* Pet::GetGenderAsString() { char* genderString; switch (itsGender) { case Male: genderString = "Male"; break; case Female: genderString = "Female"; break; default: genderString = "???"; break; } // end gender switch return genderString; }

C++ Attributes (aka variables): Definition All the variables that a program is going to use must be declared prior to use associates a type and a name with the variable allows the compiler to decide how much storage space to allocate for storage of the value associated with the identifier and to assign an address

C++ Attributes: Scope Attribute is only defined within the “block” of code that it was declared in. Can re-use names in different blocks (advanced topic – namespaces) Typically attributes are defined To be globally available in an include file used by everyone (see static modifier) As part of a class or structure At the start of a method At the start of a code control block (e.g. for loop) Scope operator is :: Cat::getAge(); Declaration is different than using “new”

C++ Attributes: Pointers/References Every variable has an address. Even without knowing the specific address of a given variable, you can store that address in a pointer Use naming convention foo_p So what does this do? int myAge = 47; int* foo_p = 0; foo_p = &myAge; myAge++; cout << *foo_p; Now try and increment the age using foo_p instead of myAge++.

C++ - Attributes: Built in types int represent negative and positive integer values Typically stored in one word (16 bits), with a range of to char a letter, a digit or a punctuation character Typically stored as a byte in memory (8 bits) with a range of 0 – 255. (Recall the do loop example.) bool hold the values true (1) or false (0) Typically stored as a byte (8 bits) in memory float any real numeric value Memory and accuracy depends on the system

C++ Attributes: Modifiers Const – value can not be changed Static – only one instance created in memory Extern – defined in some other file Long – modifies integer to be two words Unsigned – modifies integer to be only positive Double – modifies float to have twice the range

C++ Attributes: User defined types - typedef You can create types Typedef char* String_p; -- somewhere later --- String_p myName_p = 0;

C++ Attributes: User defined types - Arrays collection of data storage locations, each of which holds the same type of data Keyword are the square brackets, [] int ages[6] int IntegerArray[] = { 10, 20, 30, 40, 50 }; int theArray[5][3] Cat* listOfCats[10] (this is a BAD name – why?) How many fence posts you need for a 10-foot fence if you need one post for every foot?

C++ Attributes: User defined types - Structures structure is just a collection of variables typedef struct { int age; char name[80]; bool cool; } personAttributes;

C++ Attributes: User defined types - Classes class is a special structure - just a collection of variables - PLUS a set of related functions class Cat : public Pet { unsigned int itsAge; unsigned int itsWeight; Meow(); };

C++ Attributes: User defined types – Classes data hiding Private – only this class can get at it Protected – only this class and inheriting classes can get at it Public – everyone has access class Cat : public Pet { Private: unsigned int itsAge; unsigned int itsWeight; Protected: int getWeight(); Public: Meow(); int getAge(); };

C++ Attributes: User defined types – Classes - Inheritance Define classes that derive from one another such that one is a specialization of the other. A cat is a pet is a mammal is a animal The derived class can modify the attributes and methods of the base class The base class can leave things undefined using the virtual key word, meaning that the derived class MUST define it. Now add a fish to the collection of pets