1 / 20

Object-Oriented Programming in C++

Object-Oriented Programming in C++. Lecture 1 Introduction. Introduction. Staff Information 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

miles
Télécharger la présentation

Object-Oriented Programming in C++

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Introduction • Staff Information • 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 • 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. 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. 1 4253 (36.01 %) Java 2 3466 (29.35 %) C# 3 3278 (27.76 %) SQL 4 2909 (24.63 %) C++ 5 2426 (20.54 %)JavaScript 6 1514 (12.82 %) C 7 1103 (9.340 %) PHP 8 976 (8.264 %) Python 9 762 (6.452 %) Perl 10 490 (4.149 %) Ruby 11 476 (4.030 %) VB.NET 12 434 (3.675 %) T-SQL 13 329 (2.786 %) VB 14 276 (2.337 %) Shell Script 15 256 (2.168 %) Objective-C Computer Science jobshttp://www.itjobswatch.co.uk/jobs/uk/computer%20science.do

  6. Early programming languages 1950s: 1960s: 1970s: Simula Lisp Algol60 Algol68 Fortran Pascal BCPL Classic C COBOL PL\1 Red==major commercial useYellow==will produce important “offspring” 6 Stroustrup/Programming http://www.stroustrup.com/Programming/lecture-slides.html

  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 http://www.stroustrup.com/Programming/lecture-slides.html 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 • no parameter • returns an int • 0 for success – can omit • syntax • brackets, semicolons, variables • #include directive • copy the contents of the named file here • usingstatement • 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 • size is implementation-dependant • table is from MSDN and refers to Microsoft Visual C++

  12. Input and output • defined in iostream library • cinis the standard input stream • from keyboard • coutis 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 http://msdn.microsoft.com/en-us/library/126fe14k.aspx • 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; cin >> 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 • 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 http://go.microsoft.com/?linkid=8311584 • string library http://msdn.microsoft.com/en-us/library/xabz5s9c.aspx • fundamental types http://msdn.microsoft.com/en-us/library/cc953fe1.aspx • C++ operators http://msdn.microsoft.com/en-us/library/x04xhy0h.aspx

More Related