Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prof. Bhushan Trivedi Director GLS Institute of Computer Technology

Similar presentations


Presentation on theme: "Prof. Bhushan Trivedi Director GLS Institute of Computer Technology"— Presentation transcript:

1 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

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

3 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

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

5 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++.

6 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

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

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

9 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

10 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

11 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.

12 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++.

13 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.

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

15 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

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

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

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

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

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

21 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.

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

23 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

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

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

26 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

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

28 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.

29 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

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

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


Download ppt "Prof. Bhushan Trivedi Director GLS Institute of Computer Technology"

Similar presentations


Ads by Google