c overview
Download
Skip this Video
Download Presentation
C++ overview

Loading in 2 Seconds...

play fullscreen
1 / 23

C overview - PowerPoint PPT Presentation


  • 186 Views
  • Uploaded on

C++ overview. CS113. Computer. I/O. CPU. RAM. Memory (HD). From you to computer. a.cpp : … x=5; …. a.obj. Linker. C++ pre-proc + compiler. a.exe. C++ program. What you write. The rest is commentary. Comments Line comment: // … “ Block ” (multi-line) comment: /* … */

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'C overview' - warren


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
computer
Computer

I/O

CPU

RAM

Memory

(HD)

Gene Itkis CS112

from you to computer
From you to computer

a.cpp:

x=5;

a.obj

Linker

C++ pre-proc

+ compiler

a.exe

Gene Itkis CS112

c program

C++ program

What you write

the rest is commentary
The rest is commentary
  • Comments
    • Line comment: //…
    • “Block” (multi-line) comment: /*…*/

//this is a comment to the end of the line

str = “this line is not a comment”;

/*and this too will be ignored

by the the computer*/

printf (“%s”, str);

Gene Itkis CS112

wrappers
Wrappers
  • Headers

#include <iostream>

    • Inserts the text from file iostream.h into the program
    • Header files can be your own “common blocks” or system dependent (come with the system

Gene Itkis CS112

headers
Headers

header.h:

int x;

char c;

a.cpp:

//main file

int x;

char c;

x = 5;

c = ‘a’;

a.cpp:

//main file

#include <header>

x = 5;

c = ‘a’;

Gene Itkis CS112

the main thing
The main thing

int main()

{ …

… // the program

}

Gene Itkis CS112

finally objects
Finally, Objects
  • Constants
    • 1, 27, -97
    • 3.14, 6.02e23
    • ‘a’, ‘1’
    • true, false
    • “hello”, “123)(*^@#, - string”

Gene Itkis CS112

creating objects
Creating Objects
  • Integers
    • int x, y = -5;
    • long i, j; // usually same as int
    • short k; // -215 to 215–1
  • Reals
    • float p=3.14;
    • double zp=3.1415926535897932384;
    • double avogadro=6.02e23; // = 6.02*1023

Gene Itkis CS112

more types of objects
More types of objects
  • Alphanumericals
    • char c=‘a’;
    • string s=“this is a @#& string”, ps;
  • Boolean
    • bool there_is_God = true, agnostic;

In the above examples, note

    • multiple variables in one declaration
    • initialization

Gene Itkis CS112

object names
Object names
  • NO keywords (reserved words)
    • Names of types
    • Other C++ constructs
  • Variable names
    • Just about any alphanumeric string: x1_Z
      • Except keywords, some special chars, etc.
    • Use descriptive names

Gene Itkis CS112

existing objects operators
Existing objects: Operators
  • Assignment
    • x=x+5; // general: object = expr;
  • Arithmetic operators
    • The usual: -, -, +, *, /
      • x-z ; x = -5
    • Less usual: % //remainder
      • 14/3 // quotient = 4
      • 14%3 // remainder = 14 mod 3 = 2

Gene Itkis CS112

who s first
Who’s first
  • Precedence (highest first)
    • -(negation)
    • * / %
    • + –

Gene Itkis CS112

shorthand
Shorthand
  • x++ // x = x+1
  • x += y; // x = x+y
  • x *= y; // x = x*y
  • x /= --y; // x = x/(y-1) // and y = y-1
  • Do NOT over(ab)use shorthand
    • a source of bugs and confusion
    • last 2 examples above are bad style

Gene Itkis CS112

conversions
Conversions
  • Implicit conversion – to more complex:
    • int + long // long + long
    • int + double // double + double
    • BUT 14/4 –> 3 !!!
  • Explicit conversion
    • i = int(3.14); // i=3
    • i= static_cast<int>(3.14) // i=3, more C++

Gene Itkis CS112

conversions1
Conversions
  • More explicit conversions
    • int i=14, j=4, r;double x;r=i/j; // r=3x=double(i)/j; // x=3.5

Gene Itkis CS112

char integrity
Char integrity
  • Character object represented by integer

char c=‘a’;c++; // c=‘b’c -= 32; // c=‘B’int i= c-16; // i=2c=‘5’;int j=c-’0’; // j=5string s, h=“hello”, b=“ “,w=“world”;s=h+b+w; //s=“hello world”

Gene Itkis CS112

being logical but bool ish
Being logical but boolish
  • Operators
    • and (&&), or (||), not (!)
  • Binary arithmetic
    • Bit-wise
      • And: &
      • Or: |
      • Not: ~
      • XOR: ^
      • Shift: << , >>
    • We’ll see examples of these later

Gene Itkis CS112

bool s bin s
Bools & Bins
  • Examples
    • bool learn=true, miss_test, fail;int mask=0xF0F0, donts, bits=0x1234;miss_test = !learn;fail = !learn || miss_test;mask = ~mask;donts = bits & (~bits);bits = bits & mask;
    • Result: fail=false; mask=0x0F0F; donts=0x0; bits=0x0204

Gene Itkis CS112

when to store
When to store
  • string s = “now”; // compile time init
  • s = “now”;// run time assignment
  • Watch out for using un-initialized objects!

Gene Itkis CS112

re cap objects
Re-cap Objects
  • “Pre-existing” (atomic):
    • Constants
    • Types
    • Operators
  • Yours
    • Variables
  • Can now write very simple programs
    • Calculators, converters, etc

Gene Itkis CS112

where to now
Where to now?
  • More objects
    • New kinds – classes
    • New “operators”– functions
  • Flow control
    • Conditionals
      • if/else
    • Iteration
      • while, do … while, for
      • Iteration with functions: recursion

Gene Itkis CS112

ad