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

Slides:



Advertisements
Similar presentations
C++ Introduction.
Advertisements

Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers.
C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
CPSC 441 TUTORIAL – JANUARY 16, 2012 TA: MARYAM ELAHI INTRODUCTION TO C.
Introduction to C++ Programming. Brief Facts About C++ Evolved from C Designed and implemented by Bjarne Stroustrup at the Bell Labs in the early 1980s.
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
C/c++ 4 Yeting Ge.
16/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Functions A function consists of: Name Name Arguments (also called parameters) Arguments.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Lecture 9 Concepts of Programming Languages
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Abstract Data Types and Encapsulation Concepts
CSCI 130 Chapter 2. Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition Comments.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
chap13 Chapter 13 Programming in the Large.
Introduction of C++ language. C++ Predecessors Early high level languages or programming languages were written to address a particular kind of computing.
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
1 CSC241: Object Oriented Programming Lecture No 06.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Object-Oriented Programming Using C++ CLASS 1. 2 Review of Syllabus Catalog Description –An introduction to object oriented programming techniques using.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
DOCUMENTATION SECTION GLOBAL DECLARATION SECTION
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Dr. Mark L. HornickCS-1030 Dr. Mark Hornick 1 C++ Global functions Declarations & Definitions Preprocessor Directives.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
TK1924 Program Design & Problem Solving Session 2011/2012
Tools of the Trade
APPENDIX a WRITING SUBROUTINES IN C
CS SUMMER LECTURE 1 by İlker Korkmaz.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Introduction to Classes and Objects
Namespaces How Shall I Name Thee?.
A programming language
C Programming Language
What Is? function predefined, programmer-defined
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
COP 3330 Object-oriented Programming in C++
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
SPL – PS1 Introduction to C++.
Object Oriented Programming
Lecture 9 Concepts of Programming Languages
Introduction to Classes and Objects
Presentation transcript:

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

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

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 Introduction B language B language B expanded to C language B expanded to C language The C++ programming language The C++ programming language

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 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 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 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 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 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 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 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 Data Types (contd.) booltrue/false state booltrue/false state enumvalues given to symbols/meaningful words enumvalues given to symbols/meaningful words

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 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 Specifiers (contd.) Illegal specifier combinations are: Illegal specifier combinations are: long float long float short float short float short double short double

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