1 / 24

Programming for Beginners

Programming for Beginners. Lecture 13: An Introduction to C++. Martin Nelson Elizabeth FitzGerald. Session 13 – aims and objectives. So far we’ve only talked about Java, but there are many other languages available... Today, we will take a quick look at one alternative: C++.

Download Presentation

Programming for Beginners

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. Programming for Beginners Lecture 13: An Introduction to C++ Martin Nelson Elizabeth FitzGerald

  2. Session 13 – aims and objectives • So far we’ve only talked about Java, but there are many other languages available... • Today, we will take a quick look at one alternative: C++. • We will see that C++ has many similarities to Java. • There are also many key differences.

  3. Languages Available • So far, we’ve only talked about Java, but there are many other languages available... • Language choice depends on the task in hand! Object-oriented • COBOL (latest version) • C++ • C# • Java • Delphi • Visual Basic • Perl Procedural • Fortran • COBOL (old versions) • Pascal • C • Perl

  4. Languages Available • Fortran (acronym for formula translator) is one of the oldest high-level languages. It is still commonly used for mathematical/scientific computation. • COBOL (Common business-oriented language) is another old high-level language. Some argue that it is now very out-dated. Was used for business applications running on large computers. Required very wordy coding! • Pascal – often used for teaching, but not much else. Very inflexible. • Perl (Practical Extraction & Report Language) is a programming language especially designed for processing text. Good for quick program development, task automation and some web applications.

  5. C • Procedural language • Originally designed as a systems programming language – e.g. Unix. • Can be used for a variety of applications, from business programs to engineering. • Requires less memory than other languages. • Lower-level, a lot closer to assembly language than other high-level languages • this can make it unsuitable for some applications – especially those requiring rapid development.

  6. C++ • C, but incremented once! • Includes object-orientation, but can still use procedural syntax too. • Popular for writing programs for graphical applications, such as Windows or Mac applications • Good general language to learn, especially if you want a job as a programmer!

  7. C# • Hybrid of C and C++, written by Microsoft to compete with Java. • Object-oriented programming language used with XML-based web services on the .NET platform. • We’ll talk about XML next time... • The .NET framework allows interaction between codes written in different languages. • Designed for improving productivity in the development of web applications. • Many similarities to Java!

  8. Differences between C and C++ • C++ is object-oriented; C is not • C++ standardizes how objects are created and managed; C is a lot more variable in terms of style and design. • C++ applications are slower at runtime and slower to compile than C programs. • C++ programs tend to be larger than C. • C interacts with memory much more than C++ (pointers).

  9. Compiling C++ Code on Granby • Various C++ compilers are available. We will use g++ • C++ files are usually given the extension .cpp • To compile the file test.cpp: g++ test.cpp • By default, the compiler will create an executable called a.out. To run it, type ./a.out • Or, we can give the output a specific name (test): g++ -o test test.cpp ./test

  10. Compiling C++ Code on Granby • Various C++ compilers are available. We will use g++ • C++ files are usually given the extension .cpp • To compile the file test.cpp: g++ test.cpp • By default, the compiler will create an executable called a.out. To run it, type ./a.out • Or, we can give the output a specific name (test): g++ -o test test.cpp ./test Input file name Output file name

  11. Hello World • Typical beginner’s program • Used as first exposure to a new programming language or environment • Can be written in loads of different languages: http://www2.latech.edu/~acm/HelloWorld.html(lists 204 examples of different Hello World programs!)

  12. Hello World in Java class Hello { public static void main (String[] args); { System.out.print("Hello World"); } }

  13. Hello World in C #include <stdio.h> main() { printf ("Hello World!\n"); } • Just as in Java, every C program has a method called main, which is run when the program starts. • The printf method is used to write some text on the screen. Header file providing the printf method

  14. Hello World in C++ #include <iostream> using namespace std; int main() { cout << “Hello World!\n"; return 0; } • In C++, cout represents the standard output stream (the screen. • The operator << can be read as “send to”. Provides cout Don’t worry about this yet! In C++, main must return an int.

  15. User Input in C++ • Just as cout is the standard output stream (the screen), we have cin to represent the standard input (the keyboard). • The operator >> means “read from”.

  16. User Input in C++ Provides cout & cin #include <iostream> #include <string> using namespace std; int main() { string name; cout <<“Tell me your name!” << endl; cin >> name; cout << “Hello “ << name << endl; return 0; } Provides the string class Declare a string Newline

  17. Flow Control in C++ • For loop: for( int i=1; i<100; i++) { cout << i << endl; } • While loop: int i=100; while(i>0) { cout << i << endl; i--; }

  18. Flow Control in C++ • If statement: int score; cout << “Enter your exam mark” << endl; cin >> score; if(score<40) { cout << “You failed!” << endl; } else { cout << “You passed!” << endl; }

  19. Arrays in C++ double nums[3]; nums[0] = 1; nums[1] = 12; nums[2] = 428; double total = nums[0]+nums[1]+nums[2]; cout << “Total is “ << total << endl; • Beware: C++ will let you refer to nums[3] in the above, but that data will rarely make sense and is likely to make your code malfunction. • Can also have arrays of arrays! e.g. nums[4][5] Declare an array Assign values

  20. Methods in C++ • Easiest way (for now) is to define methods above main. • Can also define methods below main, or in separate files, but this is harder. We’ll ignore these options for now. • Just like Java, methods have a return type and (optionally) a list of arguments.

  21. Methods in C++ #include <iostream> using namespace std; // Method to add two numbers together double add(double a, double b) { return a+b; } int main() { cout << add(1,3) << endl; return 0; }

  22. Want to know more about C++? if(answer==no) { Stop listening for a minute! } else { Sign up for the forthcoming C++ course! } • C++ Programming Ten Sessions: 12th April – 17th May 2011 Tutor: Dr. Robert Oates (rxo@cs.nott.ac.uk) More info: www.nottingham.ac.uk/csc

  23. Exercises Choice of exercises for today: • Either have a go at some new exercises, which introduce some familiar concepts in C++. • Or, continue with your Java Address Book.

  24. Coming up in Session 14 • An introduction to scripting languages • HTML • CSS • XML/XSLT • php • javascript

More Related