c++ workshop sujana jyothi dept. of computer science

31
C++ Workshop Sujana Jyothi Dept. of Computer Science

Upload: tyrone-mason

Post on 04-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: C++ Workshop Sujana Jyothi Dept. of Computer Science

C++ Workshop

Sujana Jyothi

Dept. of Computer Science

Page 2: C++ Workshop Sujana Jyothi Dept. of Computer Science

Week Outline

• Monday – Introduction to C++ Basics• Tuesday – I/O and Functions• Wednesday – Arrays and Pointers• Thursday – Strings, Structures and

Classes• Friday – File I/O, Exceptional Handling

Page 3: C++ Workshop Sujana Jyothi Dept. of Computer Science

Day Outline

• Lectures : 9:30 – 11:30• Assignment • Lectures : 14:30 – 16:30

Page 4: C++ Workshop Sujana Jyothi Dept. of Computer Science

Introduction to Basic Procedural Programming

• Try to simulate compilation and execution in your head• Try different variations on a theme - simply edit the examples• Easiest way to learn programming is to experiment• The computer is a laboratory• If you don’t know something then write a program to give you

the answer … if you can• Otherwise, try looking in a book (or on the web)• You can always ask someone (doesn’t have to be me)

Page 5: C++ Workshop Sujana Jyothi Dept. of Computer Science

Practical Info

• Work on the C:\ disk can only be saved in the temp folder.

• WARNING!!! If you leave your work in the temp folder and then log out… it will be lost!

• Therefore save all of your work to your personal X:\ disk or on some USB storage

Page 6: C++ Workshop Sujana Jyothi Dept. of Computer Science

C++ Resources

• Textbooks here in the lab.

• Online resources:

1. http://en.wikipedia.org/wiki/C++_standard_library

2. http://www.cplusplus.com/

3. http://www.sgi.com/tech/stl/

Page 7: C++ Workshop Sujana Jyothi Dept. of Computer Science

C++ Intro

• We need to learn the language, start with the syntax!

• Any Java programmers will be fine, C programmers will survive too!

• Lets get started….

Page 8: C++ Workshop Sujana Jyothi Dept. of Computer Science

Workstation

• We will be using Visual Studio 2005

• When you first run Visual Studio, it asks you what kind of work you intend to do with it. Choose the option that says you'll be doing "Visual C++ development," which will configure the application to work best for doing C++ work.

Page 9: C++ Workshop Sujana Jyothi Dept. of Computer Science

First Program// Example 1/* Sujana Jyothi C++ Workshop*/

#include “stdafx.h” #include <iostream>using namespace std;int main(){

cout<<"Hello World"<<endl; }

When namespace is not used: std::cout<<"Hello World"<<std::endl;

Page 10: C++ Workshop Sujana Jyothi Dept. of Computer Science

Command Line Arguments

// Example 2#include "stdafx.h"#include <iostream>

using namespace std;

int main (int argc, char* argv[]){

cout<<"Hello World"<<endl; if (argc == 1)

cout << "No arguments given" << endl; else

for (int i = 1; i < argc; i++)cout << i<< " the argument is "<< argv[i] << "\n";

}

Page 11: C++ Workshop Sujana Jyothi Dept. of Computer Science

execution

Compilation & ExecutionCompile - Go to the Build menu and select Build Solution (or press F7 as a

shortcut). The output of the compilation process should appear in the Output area of the VS window.

Run - You can run it with or without debugging. select ‘Start Debugging’ from the Debug menu or press F5 select ‘Start WO Debugging’ from Debug menu or press ‘ctrl+F5’

Command prompt:

Start -> All Programs -> Microsoft Visual Studio 2005 -> Visual Studio Tools -> Visual Studio 2005 Command Prompt.

At the command prompt, type “notepad filename.cpp” and press Enter.

type cl /EHsc filename.cpp

type filename

Enables c++ Exception handling

Page 12: C++ Workshop Sujana Jyothi Dept. of Computer Science

Example1 Variations – What will happen in each case

/* Example 1A CS613 C++ Code*/

int main ()// does nothing!!{return 0;}

/* Example 1B CS613 C++ Code*/

int main (){}

/* Example 1C CS613 C++ Code*/

main ()// does nothing!!{return 0;}

// Example 1Dint main () {return 7}

// Example 1Eint main (){return 0.7;;}

// Example 1Ffloat main (){return 0.7;}

Page 13: C++ Workshop Sujana Jyothi Dept. of Computer Science

Rules to learn

•variables

•identifiers

•types

•values

•constants

•keywords

Page 14: C++ Workshop Sujana Jyothi Dept. of Computer Science

C++ Data TypesC++ Data Types

Structured

array struct union class

Address

pointer reference

Simple

Integral Floating

char short int long enum

float double long double

Page 15: C++ Workshop Sujana Jyothi Dept. of Computer Science

Typical size of data types

Type Number of Bytes

char 1short (short int) 2int 4unsigned int 4 enum 2long (long int) 4float 4double 8long double 12

Page 16: C++ Workshop Sujana Jyothi Dept. of Computer Science

Finding size of data type - system dependent results

• Use sizeofsizeof to find the size of a type

e.g.std::cout << sizeof(int)

Page 17: C++ Workshop Sujana Jyothi Dept. of Computer Science

Enumerated types: enum

Used to define constant values

enum days{ Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday} yesterday, today;

days tomorrow;

QUESTION: what are the values of Tuesday, today, tomorrow, yesterday?

Default values may be overridden in the enum list.

Page 18: C++ Workshop Sujana Jyothi Dept. of Computer Science

Boolean type

C++ has a built-in logical or Boolean type

#include <iostream>

int main (){bool flag = true;std::cout << flag<< std::endl<<!flag}

This outputs:

1

0

Page 19: C++ Workshop Sujana Jyothi Dept. of Computer Science

Variation - Booleans are really integers?

//

#include <iostream>

int main (){bool flag1 = 100, flag2 = false, flag3;std::cout << flag1<< std::endl<<flag2<<std::endl<<flag3;}

This outputs:

1

0

122

true is any non-zero int

false is zero

true is output as 1false is output as 0

This was not initialised

Page 20: C++ Workshop Sujana Jyothi Dept. of Computer Science

Giving a value to a variable

In your program you can assign (give) a value to the variable by using the assignment operator =

Age_Of_Dog = 12;

or by another standard method, such as

std::cout << “How old is your dog?”;std::cin >> Age_Of_Dog;

Page 21: C++ Workshop Sujana Jyothi Dept. of Computer Science

What is a Named Constant?

• A named constant is a location in memory which we can refer to by an identifier, and in which a data value that cannot be changed is stored.

VALID CONSTANT DECLARATIONS• const char STAR = ‘*’ ;• const float PI = 3.14159 ;• const int MAX_SIZE = 50 ;

Page 22: C++ Workshop Sujana Jyothi Dept. of Computer Science

keywords: words reserved to C++

• bool, true, false,• char, float, int, unsigned, double, long• if, else, for, while, switch, case, break,

• class, public, private, protected, new,

delete, template, this, virtual,

• try, catch, throw.

Note: there are many more … you may see them in the examples that follow

Page 23: C++ Workshop Sujana Jyothi Dept. of Computer Science

Example 6: prefix and postfix

//

#include <iostream>

int main (){int x =3, y=3;std::cout << ++x << std::endl;std::cout << y++ << std::endl;}

Output: 43

++ (prefix) is a unary operator

++ (postfix) is a unary operator

<< is a binary operator

Page 24: C++ Workshop Sujana Jyothi Dept. of Computer Science

Example 7: a C+ ternary operator

// Example 7

#include <iostream>

int main (){int x =3, y=4;std::cout <<"The max. of x and y is: " << (x>y?x:y);}

The max. of x and y is: 4

expression1?expression2:expression3

Output:

Means: if expression1 then expression2 else expression3

Page 25: C++ Workshop Sujana Jyothi Dept. of Computer Science

Program with Three Functions

// #include <iostream>// declares these 2 functionsint Square ( int );int Cube ( int ) ;

int main ( void ){ std::cout << "The square of 27 is "<< Square (27) << std::endl ;// function call

std::cout << "The cube of 27 is "<< Cube (27) << std::endl ;// function call

return 0 ;}int Square ( int n ){return n * n ;}int Cube ( int n ){return n * n * n ;}The square of 27 is 729

The cube of 27 is 19683Output:

Page 26: C++ Workshop Sujana Jyothi Dept. of Computer Science

Precedence of Some C++ Operators

Precedence Operator Description

Higher ( ) Function call

+ Positive

- Negative

* Multiplication

/ Division

% Modulus (remainder)

+ Addition

- Subtraction

Lower = Assignment

NOTE: Write some programs to test your understanding of precedence

Page 27: C++ Workshop Sujana Jyothi Dept. of Computer Science

Type Casting is Explicit Conversion of Type

// Example #include <iostream>

int main ( void ){ std::cout << "int(4.8) ="<<int(4.8)<<std::endl;std::cout << "float(5) ="<<float(5)<<std::endl;std::cout <<”float(2/4)="<<float(2/4)<<std::endl;std::cout<<"float(2)/float(4)

="<<float(2)/float(4)<<std::endl;}

int(4.8) =4float(5) =5float(2/4)=0float(2)/float(4)=0.5

Output:Output of float may look like an int

Page 28: C++ Workshop Sujana Jyothi Dept. of Computer Science

Parts of a Function

Every function has 2 parts

int main (void)

{

return 0;

}

signature

body

Page 29: C++ Workshop Sujana Jyothi Dept. of Computer Science

HEADER FILE FUNCTION EXAMPLE VALUE OF CALL

std::fabs(x) std::fabs(-6.4) 6.4

<math> std::pow(x,y) std::pow(2.0,3.0) 8.0

std::sqrt(x) std::sqrt(100.0) 10.0

<iomanip> std::setprecision(n) std::setprecision(3)

std::log(x) std::log(2.0) 0.693147

std::sqrt(2.0) 1.41421

<stdlib> std::abs(i) std::abs(-6) 6

Page 30: C++ Workshop Sujana Jyothi Dept. of Computer Science

C++ I/O Basics

30

I/O - Input/Output is one of the first aspects of programming that needs to be mastered:

•formatting

•whitespace

•structured inputs - getting data into the correct internal form

However, do not fall into the beginner’s traps:

•brilliant I/O with incorrect functionality

•thinking functionality is wrong because I/O is too complicated to get right

•forgetting that random tests are often better than user dependent I/O

Page 31: C++ Workshop Sujana Jyothi Dept. of Computer Science

Whitespace Characters Include . . .

• blanks

• tabs ‘\t’

• end-of-line (newline) characters ‘\n’

The newline character is created by hitting Enter or Return at the keyboard, or by using the manipulator endl or “\n” in a program.

E.g

std::cout << std::endl;

std::cout << “\n”;