Prof. Bhushan Trivedi Director GLS Institute of Computer Technology

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Advertisements

C++ Basics Variables, Identifiers, Assignments, Input/Output.
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. The Fundamentals of C++ Basic programming elements and concepts.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Data Types.
KEAN UNIVERSITY Visual C++ Dr. K. Shahrabi. Developer studio Is a self-contain environment for creating, compiling, linking and testing windows program.
CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
 Programming Languages  First Generation Languages (Machine Language)  We Actually have to do a few things. First we have to find the operating code,
1 More C++ Basics Chapter 3 Lecture CSIS 10A. 2 Agenda Review  C++ Standard Numeric Types Arithmetic–way more than you want! Character (char) data type.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
C++ Programming, Namiq Sultan1 Chapter 2 Introduction to C++ Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Basic Types, Variables, Literals, Constants. What is in a Word? A byte is the basic addressable unit of memory in RAM Typically it is 8 bits (octet)
Programming with ANSI C ++
C++ Lesson 1.
Asst.Prof.Dr. Tayfun ÖZGÜR
Variables, Identifiers, Assignments, Input/Output
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Programming with ANSI C ++
Programming with ANSI C ++
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
Chapter 1.2 Introduction to C++ Programming
Data types Data types Basic types
LESSON 3 IO, Variables and Operators
University of Central Florida COP 3330 Object Oriented Programming
Compiler Construction
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Short Overview Lembit Jürimägi.
Computing with C# and the .NET Framework
CMSC 104, Section 4 Richard Chang
بسم الله الرحمن الرحيم.
Reserved Words.
Programming with ANSI C ++
Basics (Variables, Assignments, I/O)
null, true, and false are also reserved.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
C++ Basics.
פרטים נוספים בסילבוס של הקורס
درس برنامه‌سازي کامپيوتر
An overview of Java, Data types and variables
Chapter 1: Computer Systems
Keywords.
Govt. Polytechnic,Dhangar
Variables, Identifiers, Assignments, Input/Output
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
Chap 2. Identifiers, Keywords, and Types
C Programming - Lecture 5
Building Blocks of C Programming Language
C Language B. DHIVYA 17PCA140 II MCA.
Presentation transcript:

Prof. Bhushan Trivedi Director GLS Institute of Computer Technology Programming with ANSI C ++ A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology

Overview of the C++ Language Chapter 2 Overview of the C++ Language

The Agenda Identifiers and Constants (Literals) Keywords Data Types Data Types Borrowed from C Data Types Borrowed from C with Modifications Newly Added Data Types

The Agenda Pointers Void Pointer Access Modifiers const Volatile Storage Class Specifies Initialization Normal Initialization Variable Initialization

Identifiers and Constants (Literals) The same set of rules for defining an identifier as C has C++ identifier has no limit on significant length of the identifier (32 in C) C++ supports all the literals, though there are some additions in ANSI C++.

Wide characters L‘A’ as a wide character constant This constant is wide, i.e., of 16-bit storage (so L precedes ‘A’) Possible to accommodate characters in the Unicode format

Keywords asm else new this auto enum operator throw bool explicit private true break export protected try case extern public typedef

Keywords catch false register typeid char float reinterpret_cast typename class for return union const friend short unsigned const_cast goto signed using

Keywords continue if sizeof virtual default inline static void delete int static_cast volatile do long struct wchar_t double mutable switch while dynamic_cast namespace template

Data Types Data Types Borrowed from C The data types void, int, char, float, and double operate in the same way as they do in C The modifiers to all these types, i.e., signed, unsigned, long, and short work the same

Data Types Pointers also work the same, except for void pointers Arrays, unless containing data of newly added data types, also operate in the same manner as they do in C.

Data Types Borrowed from C with Modifications Some data types of C have been adapted to be compatible with C++. These data types are structure, union, enumeration, and pointers Newly Added Data Types class is the central construct in ANSI C++.

Data types: Strings Strings in C++ are much simpler to use than in C. In C string is treated as character arrays, but ANSI C++ has a special string class for defining strings. We need to include <string> to provide operations on strings.

Data types: Strings There are few other classes such as queue, vector, etc. which are part of the Standard Template Library.

Data Types: bool bool (Boolean) is a new data type added in ANSI C++. It can have only two values, true or false. Both of these are keywords

The Bool Example void main() { bool flag; flag = true; int test; while(flag){ cin >> test; if (!test) flag = false; } }

Void Pointer int* FirstPointer; void* SecondPointer SecondPointer = FirstPointer; is valid in both C and C++, but the reverse, FirstPointer = SecondPointer; is not!

Void Pointer Explicit casting is the solution FirstPointer = (int*) SecondPointer;

The Const Pointer It cannot point to anything other than what it is pointing to at the time of its definition int * const SecondPointer = &Content1; //following is allowed *SecondPointer = AnotherContent;

These operations are not allowed! The Const Pointer SecondPointer++; SecondPointer = &AnotherContent; Changing the pointer value itself is not allowed These operations are not allowed!

A Pointer to Const A pointer to constant is a pointer variable that can point to any memory location, but the content of the memory location to which it points cannot be modified.

A Pointer to Const int const* SecondPointer = &Content; const int* SecondPointer = &Content; *SecondPointer = 100; // Not allowed SecondPointer = &AnotherContent;//allowed

Need of Pointer to const void DangerousDisplay (Employee* TempEmployee) cout << TempEmployee->OtherDetails; //following is a dangerous statement, should not be allowed Cin >> TempEmployee->OtherDetails; void SafeDisplay(Employee const* TempEmployee) is a correct way to write

Incorrect way of coding int DangerousStringLength(char* String) { int length; (*String) = 'a'; // dangerous! for (length=0; (*String);length++, String++); return length; }

Correct way using const pointer int ConstStringLength(char *const String) { int length; char* Count = String; for (length=0; (*Count);length++, Count++); return length; }

Reference Variables Is a reference to an already defined variable. a kind of a link or alias to the original variable similar to a named pointer, with two differences pointer can be null but reference should always refer to a valid variable Reference is always const

Using Reference Variables as Dummy Parameters void SwapInt(int & First, int & Second) { int temp;   temp = First; First = Second; Second = temp; } Following is the call! SwapInt(FirstVariable, SecondVariable);

Reference as a return type int& RetRefTest(int, int); ThirdInt = RetRefTest(FirstInt, SecondInt); RetRefTest(FirstInt, SecondInt) = FourthInt; int & RetRefTest(int i, int j) { return (i>j?i:j);} The function call is on the right hand side.

Access Modifier : -const #define SIZE 10 void main(){ const int size = 10; char name1[size]; // only C++ accepts this char name2[SIZE]; // C and C++ both accept this } //Volatile is also available here

Normal Initialization void main() { int i; i = 50; //< some other code related to i> int j; // this will not work in C! j = 20; }

Variable Initialization at run time int FirstVariable; FirstVariable = 50; //< some other code related to FirstVariable> int SecondVariable; cin >> SecondVariable; double ThirdVariable = sqrt(double(SecondVariable));