Object Oriented Programming in C++ Dr. Hammadi Nait-Charif Media School Bournemouth University

Slides:



Advertisements
Similar presentations
C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Advertisements

Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Guide To UNIX Using Linux Third Edition
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
C++ Streams Lecture-2.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Fundamental Programming: Fundamental Programming Introduction to C++
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term
Looping and Counting Lecture 3 Hartmut Kaiser
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
C++ Streams Lecture-2. C++ Streams Stream  A transfer of information in the form of a sequence of bytes I/O Operations:  Input stream: A stream that.
Introduction to Programming
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
 Simple UNIX commands  I/O techniques in C++  Solutions to Lab#0 problems  Solutions to Lab#1 problems 1.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Lecture 3: Getting Started & Input / Output (I/O)
Chapter 15 - C++ As A "Better C"
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ (Extensions to C)
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
A bit of C programming Lecture 3 Uli Raich.
Introduction to C++ Systems Programming.
Session 1 - Introduction
Command Line Arguments
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Chapter 2 part #3 C++ Input / Output
Chapter 14 - Advanced C Topics
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
C++ Programming Lecture 3 C++ Basics – Part I
Chapter 15 - C++ As A "Better C"
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
Chapter 2 part #3 C++ Input / Output
C++ Programming Basics
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 1 c++ structure C++ Input / Output
SPL – PS1 Introduction to C++.
Presentation transcript:

Object Oriented Programming in C++ Dr. Hammadi Nait-Charif Media School Bournemouth University

Overview Prerequisite: Programming CP2 Text:  Object Oriented Programming with C++, D. Parsons  An Introduction to Object Oriented Programming, Timothy A. Budd  Thinking C++: Credits: 20 Grading: unfortunately only an Exam

Course Organization Study hours 180h Contact  Lectures: Two Hours Weekly  Supervised Labs: One Hour weekly Linkage: Major Project

Learning Outcomes An understanding of concepts & Techniques of Object Oriented Programming The ability to apply OOP through the use of C++

C++ C++ is an extension of C C++ was developed in early 1980s C++ is an Object Oriented programming language  Programs are viewed as collection of Objects  Objects have attributes and actions

C++ C++ uses the same syntax as C, and adds extra elements to the core C language All of the C programming elements are still applicable However we need to think in a different way to design programs in C++

Namespaces I Almost all compilers allow the use of the traditional header files ANSI standard has completely redesigned these libraries using templates features All functions and variables are declared under the namespace std the new names for these “headers” are basically the same for C++, but without the “.h”

Namespaces II The standard specified new names for the header files, basically the same as C, but without the “.h” for example iostream.h becomes iostream #include P1 int main(void)‏ { std::cout << “Hello World in ANSI C++” << endl; return 1 ; }

Namespaces III In the previous example we used the std:: scope operator to load associate cout operator using keyword introduces a name from a namespace into the current declarative region ==> no need for scope resolution operator #include using namespace std; int main(void)‏ { cout << “Hello World in ANSI C++” << endl; return· ; }

namespace IV #include P2 using namespace std; namespace vertex1 { double x = 8.95; double y = 11.21;} namespace vertex2 { double x = 54.84; double y = 17.56;} int main () { using namespace vertex1; cout << x << endl; cout << y << endl; cout << vertex2::y << endl; cout << vertex2::x << endl; return 0; } Output

Basic Input/Output standard output stream object cout uses the operator (<<) to redirect text output to the screen Handling the standard input is done by applying the operator of extraction (>>) on the cin stream. #include P3 int main(void)‏ { int age; std::cout << “Age ?”<< endl;//writes to standard output std::cin >> age ; //waits for an input from cin & store //it in age return 0 ; }

The String Data Type Good news: C++ has a string data type  It is very powerful and has many functions built into it We include header without the.h: #include In C++ program we declare variables when we need them not at the top of the function (although we should declare them at the top if possible)‏

More on strings #include P4 #include using namespace std; int main()‏ { cout << “Please enter your name “ << endl; string Name, Age, NameAge; cin >> Name; cout << “Please enter your age “ << endl; cin >> Age; string NameAge = Name + “ “ + Age; cout << “Length of the string is “ << NameAge.length()<< endl; string Test = NameAge; if(NameAge == Test) cout << “the strings are the same“ << endl; else cout << “the strings are different” << endl; Test += “ another bit “; cout << Test << endl; if(NameAge == Test) cout << “the strings are the same “ << endl; else cout << “ the strings are different”; return 1; }

the program's output Please enter your name Hammadi Please enter your age 42 Length of the string is 11 the strings are the same Hammadi 40 NCCA the strings are different

Reading a Whole Line #include P9 #include using namespace std; int main()‏ { cout << "Please enter a line:\n"; string s; getline(cin,s); cout << "You entered " << s << '\n'; }

Bool C++ gives us a boolean data type which can be set to either true or false int main() P5 { bool A=true; bool B=false; cout << “A= “ << A <<“ & B = “ << endl; if(A==B) cout << “A == B” << endl; else cout << “A != B” << endl; A = B; if(A==B) cout << “A == B” << endl; else cout << “A != B” << endl; return 1; } A= 1 & B = 0 A != B A == B The Output is

Local Variable Scope It is possible to declare variables whenever we want For example it is common to declare a variable within the loop int main()‏ { int total=0; for(int i=0; i<10; i++)‏ { total += a; cout << “ “ << i ; } cout<<endl << “total = “<< total << endl; } total = 45 Programs Output

Dynamic Memory Allocation int main() P6 { int *myArray; int arraysize; cout << “How big do you want your array << endl; cin >> arraysize; // now allocate space for the array myArray = new int[arraysize]; for(int i=0; i<arraysize; i++) { cout << “Enter myArray[“<< i <<”] =”; cin >> myArray[i]; } for(int i=0; i<arraysize; i++) cout << “myArray[“<< i << “] =” << myArray[i]<<endl; // finally we delete the array delete [] myArray; return 1; }

Dynamic Memory Allocation II How big do you want your array 5 Enter myArray[0] = 54 Enter myArray[1] = 7 Enter myArray[2] = 96 Enter myArray[3] = 41 Enter myArray[4] =

Command Line Argument the main() function is the entry point in any C++ program and may be declared in a number of ways: int main(void)‏ int main(int argc, char *argv[]) the first returns an integer when the program exits The 2 nd has two parameters: int argc and char *argv[]. These are the only parameters that main is allowed

Command Line Argument int argc is the argument count: the number of arguments passed at the command line char *argv[] is a pointer to a list of command line arguments which may be accessed as of they were a series of things.

A Simple Example Program #include P7 using namespace std; int main(int argc, char *argv[])‏ { cout << “Arguments count=”<< argc <<endl; for(int i=0;i<argc; i++)‏ cout << “argument-”<<i<<” = “<<argv[i]<<endl; return 1; } argexample -c -l -m -t Arguments count = 5 argument-0 = argexample argument-1 = -c argument-2 = -l argument-3 = -m argument-4 = -t The Output

A Simple Example Program The first argument argv[0] prints as argexample this is because the whole of the command line typed is passed into the program when the program is executed the command line is placed into argv array element Finally the size of the argv array is placed into argc

Parsing Command line arguments The following program parses the command line to find one of three flags. These are -mode1, - mode2 and -help. The mode flags both accept further parameters but help does not Finally an incorrect parameter causes the program to exit

More on strings int main(int argc, char *argv[])‏ { int argcount = 1; //*argv[1] is the first parameter int mode; while(argcount<argc)‏ { string Argument(argv[argcount]); if(Argument == “-mode1”) { argcount++; cout << “mode 1 parameter = “<< argv[argcount++] << endl; mode = 1; } else if(Argument == “-mode2”)‏ { argcount++; cout << “mode 2 parameter = “<< argv[argcount++] << endl; mode = 2; } else if (Argument == “-help”)‏ { argcount++; cout << “Help mode “ << endl; } else { cout << “unkown argument” << endl; exit(1); } cout << “End of the Program in mode”<<mode << endl; return 1; }

Using getopt As you can see we have to write a bit of parsing code each time we add new options which is a pain However, the standard unix C library contains a function to help us to deal with command line arguments: An element of argv that starts with ‘-’ (and is not exactly "-" or "--") is an option element. man 3 getopt to find more on linux

A simple getopt program #include // for printf P10 #include // for various unix defines #include //for getopt #include // for strcpy #include //for bool and true false // define the command line argument parameters:indicates 2nd arg #define ARGUMENTS "vdh" int main(int argc, char *argv[])‏ { // define some inital global variables for the program bool Verbose = false; bool Debug = false; bool Help = false; // the character returned from the getopt function char c; // now loop and parse the command line options while( (c=getopt(argc,argv,ARGUMENTS)) !=EOF)‏ {

switch(c) // which option has been chosen { case 'v' : // -v printf("Setting Verbose Mode\n"); Verbose = true; break; case 'd' : // -d printf("Setting Debug Mode\n"); Debug = true; break; case 'h' : //-h printf("Help Mode\n"); Help=true; break; case '?' : // unknown option report this and exit printf("Unknown argument %c\n",optopt); printf("Valid arguments are -v -d -h \n"); printf("Will now exit\n"); exit(EXIT_FAILURE); break; }

printf("Argument parsed current modes are as follows\n"); printf("Debug = %d\n",Debug); printf("Verbose = %d\n",Verbose); printf("Help = %d\n",Help); return EXIT_SUCCESS; }

Exercise (Lab session -1)‏ The program P10 is written in C Using this as a basis convert it to use C++ input and output

Compiling a C++ Program To compile with the gnu C++ compiler use shell and issue the following commands g++ -Wall -g Hello.cpp -o Hello What does that say? g++ is the name of the compiler -Wall Print all Warnings -g Add debug information (so we can use the integrated debuggers)‏ -o Output to executable named [in this case Hello] If this option is not included the default a.out is used

Other g++ options There are many other options to g++ most of the ones we will use are to do with either paths or optimisations Path Options are as follows -I (capital i) specifies a path to search for an Include file e.g. -I /home/GraphicsLib/include -L specifies a path to search for Libraries. e.g. -L/usr/local/lib To include other libraries we use the following option - lGraphicsLib -lglut To include a library it must be in a location the compiler can find specified by the -L option

g++ Compiler Optimization There are a number of optimizations which make your code faster, the main one are as follows -funroll-loops unrolls loops and makes code execution faster (but program size bigger)‏ -O3 There are a number of different optimisation level O3 being the highest -ffast-math This version removes error checking from the maths lib (no reporting of errors)‏ Use the g++ man pages to find more info on these options.