EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.

Slides:



Advertisements
Similar presentations
Problem Solving & Program Design in C
Advertisements

Addition 1’s to 20.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #8 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Strings string.h library. String Library Functions Dr. Sadık EşmelioğluCENG 1142 NameDescription strlen return the length of string not counting \0 strcopy.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 10.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
 2000 Deitel & Associates, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
February 14, 2005 Characters, Strings and the String Class.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
1 Lecture 12 Pointers and Strings Section 5.4, ,
1 Chapter 5 - Pointers and Strings Outline 5.1Introduction 5.2Pointer Variable Declarations and Initialization 5.3Pointer Operators 5.4Calling Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 namespaces Program has identifiers in different scopes –Sometimes scopes overlap, lead to problems Namespace.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
Characters and Strings
1 Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character Handling Library 8.4String Conversion.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
Fundamentals of Characters and Strings
Chapter 8 - Characters and Strings
Pointers and Pointer-Based Strings
Remark: Data Type of Array Name
Pointers and Pointer-Based Strings
String in C++.
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 20 Strings
Lecture 19: Working with strings
Lecture 2 Arrays & Pointers September 7, 2004
Characters and Strings Functions
C Characters and Strings
Presentation transcript:

EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST

Fundamentals of Characters and Strings  Character constant –Integer value represented as character in single quotes – 'z' is integer value of z 122 in ASCII  String –Series of characters treated as single unit –Can include letters, digits, special characters +, -, *... –String literal (string constants) Enclosed in double quotes, for example: "I like C++" –Array of characters, ends with null character '\0' –String is constant pointer Pointer to string’s first character –Like arrays

Fundamentals of Characters and Strings  String assignment –Character array char color[] = "blue"; –Creates 5 element char array color last element is '\0' –Variable of type char * char *colorPtr = "blue"; –Creates pointer colorPtr to letter b in string “blue” “blue” somewhere in memory –Alternative for character array char color[] = { ‘b’, ‘l’, ‘u’, ‘e’, ‘\0’ };

Fundamentals of Characters and Strings  Reading strings –Assign input to character array word[ 20 ] cin >> word Reads characters until whitespace or EOF String could exceed array size cin >> setw( 20 ) >> word; Reads 19 characters (space reserved for '\0' )

Fundamentals of Characters and Strings  cin.getline –Read line of text – cin.getline( array, size, delimiter ); –Copies input into specified array until either One less than size is reached delimiter character is input –Example char sentence[ 80 ]; cin.getline( sentence, 80, '\n' );

String Manipulation Functions of the String-handling Library  String handling library provides functions to –Manipulate string data –Compare strings –Search strings for characters and other strings –Tokenize strings (separate strings into logical pieces)

String Manipulation Functions of the String-handling Library char *strcpy( char *s1, const char *s2 ); Copies the string s2 into the character array s1. The value of s1 is returned. char *strncpy( char *s1, const char *s2, size_t n ); Copies at most n characters of the string s2 into the character array s1. The value of s1 is returned. char *strcat( char *s1, const char *s2 ); Appends the string s2 to the string s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned. char *strncat( char *s1, const char *s2, size_t n ); Appends at most n characters of string s2 to string s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned. int strcmp( const char *s1, const char *s2 ); Compares the string s1 with the string s2. The function returns a value of zero, less than zero or greater than zero if s1 is equal to, less than or greater than s2, respectively.

String Manipulation Functions of the String-handling Library  Copying strings – char *strcpy( char *s1, const char *s2 ) Copies second argument into first argument –First argument must be large enough to store string and terminating null character – char *strncpy( char *s1, const char *s2, size_t n ) Specifies number of characters to be copied from string into array Does not necessarily copy terminating null character

1 2 // Using strcpy and strncpy. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include // prototypes for strcpy and strncpy 9 10 int main() 11 { 12 char x[] = "Happy Birthday to You"; 13 char y[ 25 ]; 14 char z[ 15 ]; strcpy( y, x ); // copy contents of x into y cout << "The string in array x is: " << x 19 << "\nThe string in array y is: " << y << '\n'; // copy first 14 characters of x into z 22 strncpy( z, x, 14 ); // does not copy null character 23 z[ 14 ] = '\0'; // append '\0' to z's contents cout << "The string in array z is: " << z << endl;

26 27 return 0; // indicates successful termination } // end main The string in array x is: Happy Birthday to You The string in array y is: Happy Birthday to You The string in array z is: Happy Birthday

Object Oriented Programming (OOP)  What is a Class –Class is a keyword in C++ language –A class is a user defined data type.  What are Objects –The instances (variables) of the class are called objects.

Advantages of using Classes –Simplify programming –Interfaces Hide implementation –Software reuse Composition (aggregation) –Class objects included as members of other classes Inheritance –New classes derived from old

Classes  Structure of a class class name_of_class { // definition of a class };  Why we need classes ? –Idea is that we first group the data then we manipulate the data as a group not separately.

Classes  We want to write a class called Calendar, so class Calendar { int day; int month; int year; };  Now we create an object of this class main () { Calendar MyCalendar; MyCalendar.day; MyCalendar.month; MyCalendar.year; }

Data Encapsulation  By using an object of the class Calendar can we set the value of day, month or year ? –Answer is, NO  MyCalendar.month = 10; // error  Here compiler does not know what month is ? Compiler cannot see month, i.e. default visibility of data members in a class is Private.

Data Encapsulation  Real world example would be a watch. We can see the pins but cant touch them, for that we have been given an interface a button… but internal structure of a watch is hidden.  Note: Default visibility of all data and functions inside a class is private. –private is a keyword –opposite is public

Private / Public class Calendar { Private: // private data and functions Public: // public data and functions visible from outside };  Normally in a class we keep the data private but the functions public.

Example class Calendar { Private: int day, month, year; Public: SetMonth(); // visible functions Print(); }; main() { Calendar MyCalendar; MyCalendar.SetMonth(); // public interface MyCalendar.print(); }

Inheritance  Inheritance –Software reusability –Create new class from existing class Absorb existing class’s data and behaviors Enhance with new capabilities –Derived class inherits from base class Derived class –More specialized group of objects –Behaviors inherited from base class Can customize –Additional behaviors

Inheritance  Class hierarchy –Direct base class Inherited explicitly (one level up hierarchy) –Indirect base class Inherited two or more levels up hierarchy –Single inheritance Inherits from one base class –Multiple inheritance Inherits from multiple base classes –Base classes possibly unrelated

Base Classes and Derived Classes  Base classes and derived classes –Base class typically represents larger set of objects than derived classes Example: –Base class: Vehicle Cars, trucks, boats, bicycles, … –Derived class: Car Smaller, more-specific subset of vehicles

Base Classes and Derived Classes  Inheritance examples

Base Classes and Derived Classes  Inheritance hierarchy –Inheritance relationships: tree-like hierarchy structure –Each class becomes Base class –Supply data/behaviors to other classes OR Derived class –Inherit data/behaviors from other classes

Single inheritance CommunityMember EmployeeStudent AdministratorTeacher AdministratorTeacher StaffFaculty Alumnus Single inheritance Multiple inheritance Inheritance hierarchy for university CommunityMember s.