Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C++ An object-oriented language Unit - 01.

Similar presentations


Presentation on theme: "Introduction to C++ An object-oriented language Unit - 01."— Presentation transcript:

1 Introduction to C++ An object-oriented language Unit - 01

2 2 Unit Introduction This unit covers the basics of C++ language This unit covers the basics of C++ language

3 3 Unit Objectives After covering this unit you will understand… After covering this unit you will understand… Introduction to C++ Introduction to C++ Program structure in C++ Program structure in C++ Data types Data types Specifiers and modifiers Specifiers and modifiers Scope and storage allocation Scope and storage allocation Operators Operators Functions Functions

4 4 Introduction B language B language B expanded to C language B expanded to C language The C++ programming language The C++ programming language

5 5 Header Files (.h) An interface of an implementation to the client An interface of an implementation to the client Main contents of a header file Main contents of a header file Declarations of classes and structures Declarations of classes and structures Other header files or libraries to be included Other header files or libraries to be included Global variables Global variables

6 6 Source Files (.cpp) In general, implementation of the interface part (declared in.h file) In general, implementation of the interface part (declared in.h file).cpp file can also contain header file contents.cpp file can also contain header file contents Main contents of a source file Main contents of a source file Implementation of class (and/or structure) declared in header file Implementation of class (and/or structure) declared in header file Can also have a main(), an entry point to the executable program Can also have a main(), an entry point to the executable program

7 7 Executing C++ Program Search the included libraries Search the included libraries Compile the related code into.obj form Compile the related code into.obj form Link the complied code to an executable code Link the complied code to an executable code Execute the code Execute the code

8 8 Hello World Program // hello.cpp #include #include #include myfile.h// user defined header file int gVariable = 100;// global variables void main () { cout << Hello World!;// printing Hello World on cout << Hello World!;// printing Hello World on // console }

9 9 NameSpaces Solve naming problems in large applications Solve naming problems in large applications using and namespace keywords are used to declare and define a name space e.g. using and namespace keywords are used to declare and define a name space e.g. using namespace std;

10 10 Example: NameSpaces // create namespace 'NSfromAccount' namespace NSfromAccount { int accountNumber = 2000; int accountNumber = 2000;}; // create namespace 'NStoAccount' namespace NStoAccount { int accountNumber = 3000; int accountNumber = 3000;}; void main() { // local variable // local variable int accountNumber = 100; int accountNumber = 100; // using accountNumber of namespace 'NSfromAccount' // using accountNumber of namespace 'NSfromAccount' cout << NSfromAccount::accountNumber << endl; cout << NSfromAccount::accountNumber << endl;

11 11 Example: NameSpaces (contd.) // using accountNumber of namespace 'NStoAccount' cout << NStoAccount::accountNumber << endl; cout << NStoAccount::accountNumber << endl; // using local variable accountNumber cout << accountNumber << endl; cout << accountNumber << endl;}

12 12 Data Types Data Types provide a way you use a memory Data Types provide a way you use a memory Data Type have a storage size associated with it Data Type have a storage size associated with it C/C++ has the following data types: C/C++ has the following data types: char 8 bits char 8 bits int 16 bit int 16 bit float single-precision float single-precision double double-precision double double-precision

13 13 Data Types (contd.) booltrue/false state booltrue/false state enumvalues given to symbols/meaningful words enumvalues given to symbols/meaningful words

14 14 Example: Data Types #include #include void main() { char characterVal = a; char characterVal = a; int integerVal = 1; int integerVal = 1; float floatVal = 2.3f; float floatVal = 2.3f; double doubleVal = ; double doubleVal = ; bool booleanVal = 1; // true state bool booleanVal = 1; // true state enum ENumeratedVal = { ZERO, ONE, TWO }; enum ENumeratedVal = { ZERO, ONE, TWO };}

15 15 Specifiers Specifiers modify the meaning of the basic data types Specifiers modify the meaning of the basic data types Four types of specifiers Four types of specifiers long long short short signed signed unsigned unsigned

16 16 Specifiers (contd.) Illegal specifier combinations are: Illegal specifier combinations are: long float long float short float short float short double short double

17 17 Example: Specifiers #include #include void main() { int intVar = 10; // standard integer format int intVar = 10; // standard integer format short int shortVar = 10; // takes less space short int shortVar = 10; // takes less space short shortVar2 = 10; // you may omit the int short shortVar2 = 10; // you may omit the int long int longVar = 10; // takes more space than int long int longVar = 10; // takes more space than int long longVar2 = 10; // you may omit the int long longVar2 = 10; // you may omit the int unsigned int uIntVar = 10; // no sign bit reserved unsigned int uIntVar = 10; // no sign bit reserved signed int intVar2 = -10; // same as int (sign bit // reserved by default) signed int intVar2 = -10; // same as int (sign bit // reserved by default)}

18 18 Modifiers Modifiers change the meaning, type, scope or accessibility of the variable, function, structure or class Modifiers change the meaning, type, scope or accessibility of the variable, function, structure or class Modifiers supported by C/C++ are: Modifiers supported by C/C++ are: const const static static register register volatile volatile friend friend

19 19 Example: Modifiers #include #include void main() { const int CONST_VAR = 10; const int CONST_VAR = 10; // this variables value cannot be changed and is final // this variables value cannot be changed and is final static char* s_StaticString = Hello; static char* s_StaticString = Hello; /* this variable is available throughout the program /* this variable is available throughout the program length and is equivalent to a global variable */ length and is equivalent to a global variable */ volatile float volatileVar = 10.3; volatile float volatileVar = 10.3; /* no compiler optimisation is done on the variable, as /* no compiler optimisation is done on the variable, as volatile, its value is supposed to change frequently volatile, its value is supposed to change frequently friend requires class declaration, and will friend requires class declaration, and will be discussed later in the training program */ be discussed later in the training program */}

20 20 Scope and Storage Allocation Scope means where a variable can be accessible Scope means where a variable can be accessible Storage allocation means the lifetime of a variable and how the storage is allocated by the compiler Storage allocation means the lifetime of a variable and how the storage is allocated by the compiler Global variables Global variables Local variables Local variables Register variables Register variables Static variables Static variables External variables External variables

21 21 Example: Variables with different scope and lifetime #include #include int gVar1;// define global variable extern int geVar1;// declare an extern variable static int sCount = 100;// static global variable void f() { int x;// local variable declaration x = 100;// local variable definition static int y = 200;// local static variable cout << y++ << endl << x++ << gVar1++ << endl << geVar1++ << endl << sCount++ << endl; } int geVar1;// define an external variable void main() { gVar1 = 300; f();f();}

22 22 Operators Arithmetic Operators (+, -, *, /, =) Arithmetic Operators (+, -, *, /, =) Relational Operators (, =, ==, !=) Relational Operators (, =, ==, !=) Logical Operators (&&, ||) Logical Operators (&&, ||) Bitwise Operators (&, |, ^, ~) Bitwise Operators (&, |, ^, ~) Shift Operators ( >) Shift Operators ( >) Unary Operators (-, ~) Unary Operators (-, ~) Ternary Operator (? :) Ternary Operator (? :)

23 23 Operators (contd.) a = a < b ? b : a; // this is equivalent to max operator Comma Operator Comma Operator a = (b++, c++, d++); sizeof Operator sizeof Operator cout << sizeof (long); Casting Casting long longVal = (long) intVal; Operator Precedence Operator Precedence

24 24 Example: Operators #include #include void main() { int a, b, c; // declaration of variables int a, b, c; // declaration of variables b = 2;// value assignment b = 2;// value assignment c = 3; c = 3; a = (((b+c)*b)-c) / b;// add, multiply, subtract a = (((b+c)*b)-c) / b;// add, multiply, subtract // etc. if(((a = c)) && (a > c)) // compare if(((a = c)) && (a > c)) // compare { a = ~(b & ~c); a = ~(b & ~c); } else else { a >> b; // a is right shifted b times a >> b; // a is right shifted b times cout << a; cout << a; }

25 25 Example: Operators (contd.) d = a; d = a; a = a < b ? b : a; // a is assigned the greater value // between a & b a = a < b ? b : a; // a is assigned the greater value // between a & b a = (b++, c++, d++); // comma and increment operations a = (b++, c++, d++); // comma and increment operations long longVal = (long) a; // casting from int to long long longVal = (long) a; // casting from int to long cout << sizeof(long); // displaying the size of long cout << sizeof(long); // displaying the size of long } // end main

26 26 Type Definition typedef suggests type definition typedef suggests type definition name aliasing is a more accurate description name aliasing is a more accurate description typedef int* IntPtrType; IntPtrType px, py; // p represents pointer type Here int* is aliased to IntPtrType Here int* is aliased to IntPtrType

27 27 Functions Functions have Functions have parameters parameters processing processing return values return values Functions perform operations on the parameters passed to it and returns a value e.g. Functions perform operations on the parameters passed to it and returns a value e.g. returnVal FunctionName (functionParameters) { // perform some operations on parameters passed // perform some operations on parameters passed return returnVal; return returnVal;}

28 28 Parameter Passing Parameters can be passed to a function in following ways Parameters can be passed to a function in following ways By Reference By Reference By Value By Value

29 29 Example: Function #include #include void ByValue(int intVar) { intVar = 9; // passed as 5, and changes to 9 locally intVar = 9; // passed as 5, and changes to 9 locally} void ByReference(int& intVar) { intVar = 5; intVar = 5;} void main() { int j = 2; int j = 2; ByReference(j); // j changes to 5 ByReference(j); // j changes to 5 ByValue(j); // j remains at 5 ByValue(j); // j remains at 5 cout << j; // displays 5 cout << j; // displays 5}

30 30 Unit Summary In this unit you have covered … Introduction to C++ Introduction to C++ Program structure in C++ Program structure in C++ Data types Data types Specifiers and modifiers Specifiers and modifiers Scope and storage allocation Scope and storage allocation Operators Operators Functions Functions


Download ppt "Introduction to C++ An object-oriented language Unit - 01."

Similar presentations


Ads by Google