Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming in C++

Similar presentations


Presentation on theme: "Object-Oriented Programming in C++"— Presentation transcript:

1 Object-Oriented Programming in C++
Lecture 1 Introduction

2 Introduction Staff Information Module Blackboard site Fred Pratt K319
Cathy French K233 Module Blackboard site Module Information Descriptor Timetable - 9 lectures + 9 practicals, plus revision/test preparation in Week 4 Assessment – portfolio test Resources – Visual Studio Books Study Space – lecture slides and practical exercises

3 Assumptions you have studied programming before you are new to C++
Java, C, algorithms, object-orientation you are new to C++ have you studied UML diagrams?

4 Why learn C++? one of the most widely used programming languages
C++ applications are everywhere embedded systems, operating systems, games, real-time systems, servers, applications closely maps to hardware instructions strongly typed, supports OO programming but unlike C# and Java, not every variable and function needs to belong to a class flexible, efficient, stable related to other commonly-used languages C, C#, Java

5 Computer Science jobs http://www. itjobswatch. co
For the 6 months to 29 June 2011, IT jobs within the UK citing Computer Science also mentioned the following programming languages in order of popularity. The figures indicate the number of jobs and their proportion against the total number of IT job ads sampled that cited Computer Science. (36.01 %) Java (29.35 %) C# (27.76 %) SQL (24.63 %) C++ (20.54 %)JavaScript (12.82 %) C (9.340 %) PHP (8.264 %) Python (6.452 %) Perl (4.149 %) Ruby (4.030 %) VB.NET (3.675 %) T-SQL (2.786 %) VB (2.337 %) Shell Script (2.168 %) Objective-C

6 Early programming languages
Simula Lisp Algol60 Algol68 Fortran Pascal BCPL Classic C COBOL PL\1 Red==major commercial use Yellow==will produce important “offspring” 6 Stroustrup/Programming

7 Modern programming languages
Lisp Python Smalltalk PHP Fortran77 Java95 Java04 Eiffel Simula67 C89 C++ C++98 C++0x Ada Ada98 C# Object Pascal Pascal Javascript COBOL04 COBOL89 Visual Basic PERL Stroustrup/Programming 7

8 Hello World #include <string> #include <iostream>
using namespace std; int main() { string name; cout << "What is your name? "; cin >> name; cout << "Hello, " << name << "!" << endl; return 0; } How does this compare to Java? C? C#?

9 Comparison to Java main method syntax #include directive
no parameter returns an int 0 for success – can omit syntax brackets, semicolons, variables #include directive copy the contents of the named file here using statement use a C++ namespace without qualification similar to importing a Java package

10 string part of the standard library namespace string is a class
like String in Java unlike C-style strings null-terminated character array has useful methods s.size() s.length() s.insert(pos, x) s.append(pos, x) s.erase(pos) pos = s.find(x)

11 C++ primitive types Type Name Bytes Other Names Range of Values int 4
signed –2,147,483,648 to 2,147,483,647 bool 1 none false or true char –128 to 127 by default signed char –128 to 127 unsigned char 0 to 255 short 2 short int, signed short int –32,768 to 32,767 long long int, signed long int long long 8 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 float 3.4E +/- 38 (7 digits) double 1.7E +/- 308 (15 digits) wchar_t __wchar_t 0 to 65,535 size is implementation-dependant table is from MSDN and refers to Microsoft Visual C++

12 Input and output defined in iostream library
cin is the standard input stream from keyboard cout is the standard output stream to console window the streams contain a sequence of characters cin >> name; puts the characters in the input stream into the variable name cout << name; puts the characters in the variable name into the output stream

13 << and >> operators
<< sends bytes to an output stream object insertion operator works for all standard C++ data types can concatenate output cout << "Hello, " << name << "!" << endl; endl replaces the C endline character '\n' >> reads bytes from the input stream up to a whitespace character use istream::getLine() function to read multiple words getline(cin, name);

14 C++ operators similar to Java and C need to know precedence
the same operator can have a different meaning depending on the operand type << operator overloading when we define C++ classes we can define our own operator overload be sensible!

15 C++ constructs sequence, selection, iteration constructs are the same as in Java, C and C# unlike C, can declare variables anywhere within a block doesn't need to be at the beginning C++ for loop for (int i=0; i < 5; i++) { cout << "Hello, " << "!" << endl; }

16 Example – read-ahead while loop
int sum=0; int x; cin >> x; while (x!=-9999) { sum=sum+x; } cout << "The sum is " << sum << endl;

17 multiway if- example int x = 8; if ( x <= 5)
cout << "The number is small: "; else if (x <= 10) cout << "The number is medium: "; else cout << "The number is big: "; cout << x << endl The number is medium: 8

18 C++ bool be careful with boolean operations int x = 5;
C++ has a bool data type but integers and booleans are interchangeable (like C) 0 is false, non-zero is true false is 0, true is 1 == comparison operator = assignment operator int x = 5; if (x) true (x is non-zero) if (x==4) false (x is 5) if (x=4) true (x is now set to 4, which is non-zero)

19 Summary Today we have introduced the C++ language and compared it to other languages same data types and operators but their size can vary between C++ implementations program structure is similar input and output uses iostream sequence, selection, iteration constructs are the same as in Java, C and C# Practical exercises: getting started with Visual Studio and C++ a few simple C++ programs

20 Further reading “C++: A beginners guide” by Herbert Schildt
string library fundamental types C++ operators


Download ppt "Object-Oriented Programming in C++"

Similar presentations


Ads by Google