C++ Basics - C++ Programming - Lecture Slides | COSC 1305, Study notes of Computer Science

Material Type: Notes; Class: C++ Programming; Subject: (Computer Science); University: University of Houston; Term: Spring 2005;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-7l8-1
koofers-user-7l8-1 🇺🇸

7 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
D. Mirkovic, C++ Programming, Spring 2005
Lecture 1: C++ Basics
Dragan Mirkovic
Department of Computer Science
University of Houston
D. Mirkovic, C++ Programming, Spring 2005
Announcements
Today:
C++ Basics
Ch 1. In Irvine.
Introducing C++
Basic terminology
Classes and objects
Standardization of C++
C/C++ differences
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download C++ Basics - C++ Programming - Lecture Slides | COSC 1305 and more Study notes Computer Science in PDF only on Docsity!

D. Mirkovic, C++ Programming, Spring 2005

Lecture 1: C++ Basics

Dragan Mirkovic

Department of Computer Science

University of Houston

Announcements

• Today:

– C++ Basics

– Ch 1. In Irvine.

– Introducing C++

• Basic terminology

  • Classes and objects

• Standardization of C++

• C/C++ differences

D. Mirkovic, C++ Programming, Spring 2005

Introduction

  • C++ is one of the most popular programming languages today
    • Industrial-strength language
    • Incorporates the efficiency of C with OO features
    • Well-suited for development of large complex software systems and

libraries

  • Strongly typed language
    • Compiler performs strict type checking on variables and expressions
  • Object-oriented (OO) approach:
    • Focus on objects that make up an application problem
    • Program structure: relationship between objects
  • Process-oriented approach:
    • Program is organized as a hierarchy of tasks (procedures)
    • Procedural programming languages (C, Fortran, Pascal)

History of C++

  • C was developed at Bell Labs around1969-1973 for implementation of the UNIX operating system, by Dennis Ritchie.
  • 90% of UNIX was then written in C.
  • 1989 - ANSI Standard for C
  • C++ was written by Bjarne Sroustrup at Bell Labs during 1983-
  • C++ is an extension of C. Prior to 1983, Bjarne Stroustrup added features to C and formed what he called "C with Classes".
  • He had combined the use of classes and object-oriented features with the power and efficiency of C. The term C++ was first used in 1983.

D. Mirkovic, C++ Programming, Spring 2005

Inheritance

• Different objects can have some common attributes

and operations

• In some cases it is useful to define a base class

which contains the common characteristics

• Corresponds to “Is a” relationship in Entity-

relationship data model

• Example:

Vehicle

Car Truck Airplane

Base class

Derived classes

Polymorphism

• A single name can denote objects of different types

• The actual type of the object doesn’t have to be

known at compile time

• Most programming languages do not support

polymorphism

• Unions in C allow for a specific form of polymorphism

• In C++ polymorphic objects must be instances of

classes related by inheritance

• Example: calcSpeed() function for

Vehicle–Car–Truck–Airplane family

  • Dynamic binding vs. static binding

D. Mirkovic, C++ Programming, Spring 2005

Structure of a C++ program

• Example:

// my first program in C++

#include <iostream.h>

int main () { cout << "Hello World!"; return 0; }

• Example :

_/ Equivalent program in C /_

#include <stdio.h>

int main () { printf(“Hello, World\n”); return 0; }

•Structure

–Comments (// line comment /* block comment */ )

–Preprocessor directives (#)

–the main function declaration.

–standard output steam cout , equivalent to stdout

Variables, data types, constants.

• Variable = a portion of memory to store a determined

value.

  • Each variable needs an identifier that distinguishes it from the others a = 5; b = 2; a = a + 1; result = a - b;

• Identifier = a sequence of one or more letters, digits

or underline symbols ( _ ).

  • The length of an identifier is not limited
  • Variable identifiers should always begin with a letter or an underline character ( _ )
  • they cannot match any key word

D. Mirkovic, C++ Programming, Spring 2005

Declaration of variables

  • In order to use a variable in C++, we must first declare it
  • The syntax: int a; float mynumber;
  • Initialization of variables:

type identifier =

initial_value ;

Or

type identifier

(initial_value) ;

  • Examples:

int a = 0; int a(0);

#include <iostream.h>

int main ()

// declaring variables:

int a, b;

int result;

// process:

a = 5; b = 2; a = a + 1;

result = a - b;

// print out the result:

cout << result;

// terminate the

program: return 0;

Scope of variables

• In C++ we can declare variables anywhere in the

source code (not in C)

•Global variables •Local variables •External variables

D. Mirkovic, C++ Programming, Spring 2005

Constants

• Literals:

  • Integer, floats, characters and strings
  • Examples:
    • Integer: 75 // decimal 0113 // octal 0x4b // hexadecimal
    • Floating point numbers: 3.14159 // 3. 6.02e23 // 6.02 x 10 23
    • Character and strings: 'p’ "Hello world"
      • Defined constants #define PI 3. #define NEWLINE '\n‘ #define WIDTH 100
      • Declared constants
      • With the const prefix you can

declare constants with a specific

type exactly as you would do

with a variable:

const int width = 100;

const char tab = '\t';

Operators

  • Assignment: =
  • Arithmetic operators: +, -, *, /, %
  • Compound arithmetic operators: +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=
  • Increment/Decrement operators: ++, --
  • Relational operators: ==, !=, >, <, >=, <=
    • According to the ANSI-C++ standard, the result is a bool value

( true or false )

  • Different from C (int)
  • Logic operators: !, &&, ||
  • Conditional operator:?
  • Bitwise Operators: &, |, ^, ~, <<, >>
  • Explicit type casting Operators: (type) or type()

D. Mirkovic, C++ Programming, Spring 2005

Reference Parameters

  • Passing arguments by reference in C++

void swap(int &x, int &y) { int tmp = x; x = y; y = tmp; } int A = 20, B = 10; swap(A, B); cout << A << ',' << B;

  • Passing arguments by reference in C

void swap(int *x, int *y) { int tmp = *x; *x = *y; *y = tmp; } int A = 20, B = 10; swap(&A, &B); printf("%d, %d\n", A, B);

Stream I/O

  • Stream Output
    • The stream output operator (<<) appends an expression to the

output stream

  • Examples: cout << 10; // prints number 10 on screen cout << "Hello"; // prints Hello on screen cout << x; // prints the content of x variable on screen cout << "Hello, I am " << age << " years old.";
  • When writing an expression use () to force evaluation of the

expression before printing

cout << (10 + x);

  • Use a newline character ‘\n’ to force line breaks cout << (10 + x) << ‘\n’;
  • Use endl stream manipulator a newline and flush the output buffer cout << (10 + x) << ‘\n’;

D. Mirkovic, C++ Programming, Spring 2005

Stream I/O

  • Stream Input
    • The stream input operator

(>>) extracts data from an

input stream

  • Examples: int n; cin >> n; //read from input stream
  • By default operator skips

whitespace (tabs, spaces,

new lines)

int a, b; float r; char ch; cin >> a >> b >> r >> ch;

  • cin.get() can be used for character input - Example:

#include <iostream.h>

char name[80], ch = '\0';

int i = 0;

cout << "Enter your name:“

while(1)

cin.get(ch);

if(ch=='\n') break;

name[i++] = ch;

name[i] = '\0';

Summary

• C++ is an industrial-strength language

– Incorporates the efficiency of C with OO features

– Well-suited for development of large complex

software systems and libraries

– Strongly typed language

– Basic terminology:

  • Objects, Classes, Inheritance, Polymorphism

– Differences between C and C++

– Stream I/O