CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution.

Slides:



Advertisements
Similar presentations
File streams Chapter , ,
Advertisements

I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Introduction to C++ Systems Programming.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
DCS 5085 C++ Fundamentals Chapter 4. Overview Basics of a typical C++ Environment C++ Stream Input/Output Classic Stream vs. Standard Stream Iostream.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
C++ Streams Lecture-2.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
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.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
From C to C++. What is C++ C++ is the work of Bjarne Stroustrup of AT&T Bell Labs. C++ is a mostly upward compatible extension of C that provides: A better.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Programming Michael Griffiths Corporate Information and Computing Services The University of Sheffield
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
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
Introduction to C++ Systems Programming.
Programming Fundamentals
Chapter 3 L7.
Output Stream Formatting
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.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Manipulators CSCE 121 J. Michael Moore
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 3: Input/Output
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Variables have attributes
Formatted Input, Output & File Input, Output
Chapter 15 - C++ As A "Better C"
Arrays Arrays A few types Structures of related data items
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

CS116 SENEM KUMOVA METİN

Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution operator Inline functions References Default arguments Function overloading Function templates CS116 SENEM KUMOVA METİN

What is C++? C++ is the work of Bjarne Stroustrup of AT&T Bell Labs. C++ is a mostly upward compatible extension of C that provides: A better C Support for Data Abstraction Support for Object-Oriented Programming C++ supports these paradigms; it does not require their use CS116 SENEM KUMOVA METİN

Some features of C++ C++ is by far the most popular OOPL Information Hiding (public & private) Encapsulation (functions  methods) Abstract Data Types (stacks) Inheritance (code reuse) vehicle  car  truck  bus Polymorphism (having many forms) two_dim  triangle ( area of triangle??)  square ( area of square??) CS116 SENEM KUMOVA METİN

Input and Output Operations (1/6) Input to a program is treated as a stream of consecutive bytes from keyboard, a disk file or some other source.. Output is treated as a stream of consecutive bytes to video display, disk file or some other destination.. cin  reads from standard input(keyboard) cout  writes to standart output (screen) CS116 SENEM KUMOVA METİN

Input and Output Operations (2/6) #include //header file without “.h” suffix using namespace std; main() { cout << “Welcome to C++\n”; cout << “Welcome to C++"<< endl; } CS116 SENEM KUMOVA METİN

Input and Output Operations (3/6) #include //header file without.h suffix using namespace std; main() { cout <<“Welcome”<<“ to C++”<<endl; cout <<“Wel”<<“co”<<“me to “ <<“C++"<< endl; cout <<“Did”<<endl<<“you” <<endl<<“understand?”<<endl; } CS116 SENEM KUMOVA METİN

Input and Output Operations (4/6) #include using namespace std; int main() { int value1, value2 = 0; cout << "enter integers: "; cin >> value1 >> value2; cout <<"\nYou entered "<< value1 << “and” <<value2<< endl; return 0; } CS116 SENEM KUMOVA METİN

// A Simple Example #include using namespace std; int main() { int apples, oranges; // Declare two integer variables apples = 5; oranges = 6; // Set initial values int fruit; // Declare a new variable fruit = apples + oranges; // Get the total fruit cout << endl; // Start output on a new line cout << “WE HAVE " << apples <<“ APPLES” <<endl; cout << “WE HAVE " << oranges << “ ORANGES“<<endl; cout << “WE HAVE " << fruit << “ FRUITS AT ALL…“<<endl; return 0; // Exit the program } CS116 SENEM KUMOVA METİN

// A Simple Example #include using namespace std; int main() { int apples, oranges; // Declare two integer variables int fruit; //...then another one cin>>apples;// Get initial value from keyboard cin>>oranges; // Get initial value from keyboard fruit = apples + oranges; // Get the total fruit cout << endl; // Start output on a new line cout << “WE HAVE " << apples <<“ APPLES” <<endl; cout << “WE HAVE " << oranges << “ ORANGES“<<endl; cout << “WE HAVE " << fruit << “ FRUITS AT ALL…“<<endl; return 0; // Exit the program } CS116 SENEM KUMOVA METİN

Manipulators (1/4) Input and the output can be formatted by manipulators Manipulators permanently change the state of the stream to which is applied (except setw) For example placing “hex” in the output stream causes all subsequent output of ints, shorts, longs to be written in hexadecimal format int a=19; cout<< a; // 19 cout<<hex<<a; // 13 Manipulators without arguments require iostream file, manipulators with arguments require iomanip header file CS116 SENEM KUMOVA METİN

Manipulators (2/4) endl  write a newline dec  input or output in decimal oct  input or output in octal hex  input or output in hexadecimal fixed  use fixed notation for floating point numbers: d.ddd scientific  use scientific notation for floating point numbers left  left justify right  right justify setfill(c)  make c to fill character setprecision(n)  set floating point precision to n setw(n)  set field width to n CS116 SENEM KUMOVA METİN Needs iomanip header file

Manipulators (3/4) #include using namespace std; main() { int i=19; cout<< "i= "<< i <<" (decimal)\n"; cout<< "i= "<< oct <<i <<" (octal)\n"; cout<< "i= "<< hex<< i <<" (hexadecimal)\n"; cout<< "i= "<< i <<" (? ? ?)\n"; cout<< "i= "<<dec<< i <<" (decimal)\n"; } CS116 SENEM KUMOVA METİN

Manipulators (4/4) #include using namespace std; int main() { int num1 = 12, num2 = 345, num3 = 6789; cout << endl; //Start on a new line cout << setw(6) << num1 << endl<< setw(6) << num2; cout << endl << setw(6) <<num3; //Output three values cout << endl; //Start on a new line return 0; //Exit program } CS116 SENEM KUMOVA METİN

C++ Header Files … (Chapter 15 p ) CS116 SENEM KUMOVA METİN

Namespaces & Scope Resolution Operator #include using namespace std; main() { cout << “Welcome to C++"<< endl; } #include main() { std::cout << “Welcome to C++"<<std:: endl; } CS116 SENEM KUMOVA METİN C++ provides namespaces to prevent name conflicts… Namespace std includes standard C++ definitions like cout, cin, endl

Namespaces & Scope Resolution Operator CS116 SENEM KUMOVA METİN namespace xxx { int flag; int s;} namespace yyy { int flag; int s;} main() { xxx::flag=3; // :: is the scope resolution operator yyy::flag=-1; } // with a using declaration namespace xxx { int flag; int s;} namespace yyy { int flag; int s;} using xxx::flag; main() { flag=3; // xxx::flag=3; xxx:: s=0; yyy::flag=-1; yyy:: s=1; } //using namespace  declaration covers entire namespace namespace xxx { int flag; int s;} namespace yyy { int flag; int s;} using namespace xxx; main() { flag=3; s=0; // xxx::s=0; yyy::flag=-1; yyy:: s=1; cout<<flag; cout<< yyy::flag; }

Namespaces & Scope Resolution Operator // Declaring a namespace #include namespace myStuff { int value = 0; } int main() { std::cout << "enter an integer: "; std::cin >> myStuff::value; std::cout << "\nYou entered " << myStuff::value << std::endl; return 0; } CS116 SENEM KUMOVA METİN

Namespaces & Scope Resolution Operator // Declaring a namespace #include namespace myStuff { int value = 0; } using namespace std; int main() { cout << "enter an integer: "; cin >> myStuff::value; cout << "\nYou entered " << myStuff::value << endl; return 0; } CS116 SENEM KUMOVA METİN

Inline function Placing the qualifier inline before function’s return type “advises” the compiler to generate a copy of the function’s in place to avoid a function call CS116 SENEM KUMOVA METİN

Inline function CS116 SENEM KUMOVA METİN #include using namespace std; inline double cube(double side) {return side*side*side; } main() {double sideValue; cout << “enter the side lenght of the cube”<<endl; cin>>sideValue; cout<< “Volume of the cube is ”<< cube(sideValue)<<endl; }

References There are two ways to call a function Call by value (send the value of the variable) Call by reference (send the adress/reference of the variable) Call by reference in C  pointers Call by reference in C++  pointers  references int x  a variable int & y  a reference to a variable CS116 SENEM KUMOVA METİN

23 References Reference is signaled by & and provides an alternative name for storage int x; int & ref =x; x=3; ref=3; x … ref 3

References CS116 SENEM KUMOVA METİN #include using namespace std; main() { int x=2; int &y=x; // You have to initialize the references cout<<x<<“ ” <<y<<endl; x++; cout<<x<<“ ” <<y<<endl; y++; cout<<x<<“ ” <<y<<endl;

References CS116 SENEM KUMOVA METİN #include using namespace std; int squareByValue (int); void squareByReference(int &); main() {int x=2, y=4; cout<<“before function call ”<<x<<endl; cout<<squareByValue(x)<<endl; cout<<“after function call ”<<x<<endl; cout<<“before function call ”<<y<<endl; squareByReference(y); cout<<“after function call ”<<y<<endl;} int squareByValue (int n) { n=n*n; return n;} void squareByReference(int & n) {n=n*n; }

CS116 SENEM KUMOVA METİN26 References : SWAP example #include using namespace std; void swap (int &, int &); void main() {int i=7 ; int j=-3; swap (i,j); cout <<i<<endl; cout <<j<<endl;} void swap (int & a, int & b) {int t= a; a=b; b=t;}

CS116 SENEM KUMOVA METİN27 Default Arguments C++ allows to specify default values for function parameters…. void f (int v, float s=3.14, char t=‘\n’, string msg =“Error!!”); Valid invocations: f(12, 3.5, ‘\t’, “OK”); f(12, 3.5, ‘\t’); f(12, 3.5); f(12);

Default Arguments CS116 SENEM KUMOVA METİN #include using namespace std; int boxVolume (int lenght=1, int height=1, int width=1); main() { cout <<“Default volume”<< boxVolume()<<endl; cout<<“lenght=10,height=width=1”<<boxVolume(10)<<endl; cout <<“lenght=10,height=20,width=1”<<boxVolume(10,20)<<endl; cout <<“lenght=10,height=20,width=30”<<boxVolume(10,20,30)<<endl; } int boxVolume (int lenght, int height, int width) {return lenght*height*width;}

CS116 SENEM KUMOVA METİN29 Function Overloading #include using namespace std; // C++ checks the number and type ofparameters to decide which overloaded function to use int square( int a); double square (double a); int main() { int x=8; double y=8; cout<<square(x); cout<<square (y); return 0;} int square(int a ) { return a*a; } double square(double a) { return a*a; }

CS116 SENEM KUMOVA METİN30 Function Overloading #include using namespace std; void print( int a); void print (double a); int main() { int x=8; double y=8; print(x); print(y); return 0;} void print (int a ) { cout <<a <<endl; } void print (double a) { cout <<showpoint<< a <<endl; }

Function templates template T GetMax (T a, T b) {if(a>b) return a; else return b; } CS116 SENEM KUMOVA METİN char GetMax (char a, char b) { if(a>b) return a; else return b; } float GetMax (float a, float b) { if(a>b) return a; else return b; } int GetMax (int a, int b) { if(a>b) return a; else return b; }

Function Template : Example 1 #include using namespace std; template T GetMax (T a, T b) { return (a>b?a:b); } void main () { int i=5, j=6, k; float l=10.0, m=5.3, n; k=GetMax(i,j); n=GetMax(l,m); cout << k << endl; cout << n << endl; } CS116 SENEM KUMOVA METİN

Function Template : Example 2 void printArray( const int *, int ); void printArray( const double *, int ); void printArray( const char *, int ); CS116 SENEM KUMOVA METİN template void printArray( const T *, int );

Function Template : Example 2 #include using namespace std; template void printArray( const T *array, int count ) {for ( int i = 0; i < count; i++ ) cout << array[ i ] << " "; cout << endl;} // end function template printArray void main() {int a[ 5 ] = { 1, 2, 3, 4, 5 }; char b[ 4] = “C++"; cout << "Array a contains:" << endl; printArray( a, 5 ); cout << "Array b contains:" << endl; printArray( b, 4 );} CS116 SENEM KUMOVA METİN