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));