1 CS 201 Introduction to c++ (1) Debzani Deb. 2 History of C++ Extension of C (C++ for better C) Early 1980s: Bjarne Stroustrup (Bell Laboratories) Provides.

Slides:



Advertisements
Similar presentations
Object-Oriented PHP (1)
Advertisements

Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Three types of computer languages
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Introduction to Classes and Data Abstraction
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Lecture 9 Concepts of Programming Languages
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
C++ fundamentals.
1 Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
COMPUTER PROGRAMMING. Introduction to C++ History Merges notions from Smalltalk and notions from C The class concept was borrowed from Simular67 Developed.
Introduction to C++ Systems Programming.
COMPUTER SCIENCE I C++ INTRODUCTION
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Learners Support Publications Classes and Objects.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
1 Classes and Data Abstraction Part I Introduction Object-oriented programming (OOP)  Encapsulates data (attributes) and functions (behavior)
1 Classes and Data Abstraction 2 Objectives Understand encapsulation and data hiding Understand data abstraction and Abstract Data Types (ADTs) Create.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Object-Oriented Programming (OOP) and C++
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. Këpuska Summer 2004 from Dr. S. Kozaitis Spring 2003 slides 1 Summary of Chapter 6: Classes.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Chapter 15 - C++ As A "Better C"
Procedural and Object-Oriented Programming
Chapter 16: Classes and Data Abstraction
Introduction to C++ Systems Programming.
Review: Two Programming Paradigms
Introduction to Classes
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
CS212: Object Oriented Analysis and Design
Introduction to Classes
Classes: A Deeper Look Outline
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
Presentation transcript:

1 CS 201 Introduction to c++ (1) Debzani Deb

2 History of C++ Extension of C (C++ for better C) Early 1980s: Bjarne Stroustrup (Bell Laboratories) Provides capabilities for object-oriented programming  Objects – reusable software components : model things in real world  Object-oriented programs : Easy to understand, correct and modify Hybrid language  C-like style  Object-oriented style

3 What is C++? General purpose, biased towards system programming:  A better C  Supports data abstraction  Supports object-oriented programming  Supports generic programming. C++ programs  Built from pieces called classes and functions. C++ standard Library  Rich collection of existing classes and functions.

4 Data abstraction (Encapsulation) Define program entity in terms of related data. Define operations on entity. Separate implementation of program from its data structures (data hiding via object interfaces). Program relies more on abstract properties of classes, than on properties of particular objects.

5 Object oriented … Extends data abstraction. Facilitates code reuse through inheritance. Runtime dynamic binding. C++ is not “truly object-oriented”  Hybrid approach  Not everything is a class.

6 Major Domains of C++ Applications Operating system kernels and components Device drivers Real-time/ deterministic systems Critical systems Parallel computing Numerical calculations 3D games and graphics

7 A better C.. Symbolic constants Inline code submission Default function arguments Function and operator overloading References Namespace management Exception handling Templates

8 C++ Syntax features (1) Symbolic constants const int arraySize = 10; int a [arraySize]; Inline functions: C++ compiler places the function inline rather than generate the code for calling the routine. Eliminates the overhead of function calling in case of small functions. inline double cube (const double s) { return s*s*s; } Default function arguments void foo (int a, int b =0) { …} foo(a); /* called function foo with arguments (a,0) */

9 C++ Syntax features (2) Declaration inside the loop control for( int i = 0; i <max; i++) {… } i is undefined outside the loop. A reference is an alias (an alternative name) for an object. When you initialize a reference with an object, you bind that reference to that object. int num1 = 10; int num2 = 20; int & RefOne = num1; // valid int & RefOne = num2; // error, two definitions of RefOne int & RefTwo; // error, uninitialized reference int & RefTwo = num2; // valid

10 C++ Syntax features (3) Pass-by-reference in C++  Passing the address of an argument in the calling function to a corresponding parameter in the called function. In C, the corresponding parameter in the called function must be declared as a pointer type. In C++, the corresponding parameter can be declared as reference type. void swapnum (int & i, int & j) { int temp = i; i = j; j = temp; } int main(void) { int a = 10; int b = 20; swapnum (a, b); return 0; } void swapnum(int *i, int *j) { int temp = *i; *i = *j; *j = temp; } // C Syntax swapnum(&a, &b); // C Syntax

11 A simple C++ program (1) Input/output  cin: standard input stream, normally keyboard  cout: standard output stream, normally screen  cerr: standard error stream, display error messages Simple C++ program // myFirst.cpp #include int main () { std :: cout << “Welcome to C++! \n”; return 0; } Preprocessor directive to include input/ output stream header file Stream insertion operator Name cout belongs to namespace stdBinary scope resolution operator

12 A simple C++ program (2) // myFirst.cpp #include using namespace std; int main () { cout << “Welcome to C++! \n”; return 0; } Namespace  std :: cout is removed through use of using statements which specifies that we will be using objects (such as cout ) located in a special region/namespace called std.  C++ standard library is defined in the namespace std. So every program generally include using namespace std. Library file names in namespace std do not require a.h extension

13 Namespace (1) Namespaces allow to group entities like classes, objects and functions under a single name. This way the global scope can be divided in "sub- scopes", each one with its own name. The format of namespaces is: namespace identifier { entities }  wh ere identifier is any valid identifier and entities is the set of classes, objects and functions that are included within the namespace. For example: namespace myNamespace { int a, b; }  In order to access these variables from outside the myNamespace namespace we have to use the scope operator ::. For example, myNamespace :: a

14 Namespace (2) The functionality of namespaces is especially useful in the case that there is a possibility that a global object or function uses the same identifier as another one, causing redefinition errors. For example: #include using namespace std; namespace first { int var = 5; } namespace second { double var = ; } int main () { cout << first :: var; cout << second::var; return 0; } In this case, there are two global variables with the same name: var. One is defined within the namespace first and the other one in second. No redefinition errors happen thanks to namespaces.

15 Namespace (3) The keyword using is used to introduce a name from a namespace into the current declarative region. For example: #include using namespace std; namespace first { int x = 5; int y = 10; } namespace second { double x = ; double y = ; } int main () { using first::x; using second::y; cout << x; cout << y; cout << first::y; cout << second::x; return 0; } Notice how in this code, x (without any name qualifier) refers to first::x whereas y refers to second::y, exactly as our using declarations have specified. We still have access to first::y and second::x using their fully qualified names Output

16 Namespace (4) using and using namespace have validity only in the same block in which they are stated or in the entire code if they are used directly in the global scope. For example, if we had the intention to first use the objects of one namespace and then those of another one, we could do something like: #include using namespace std; namespace first { int x = 5; } namespace second { double x = ; } int main () { { using namespace first; cout << x; } { using namespace second; cout << x; } return 0; } Output

17 Structure and their Applicability (1) New data types are built by using elements of other types. struct Time { int hour; int minute; int second; }; Structure member naming  In same structure must have unique name.  Different structure can have members of same name. Structure members

18 Structure and their Applicability (2) Structure member can be pointer to instance of enclosing struct (self-referencial structure) – used for linked lists, queues, stack and trees. Structure member can not be instance of enclosing structure. Few uses: Time timeObject; Time timeArray[10]; Time *timePtr; Time &timeRef = timeObject; Member access operator  Dot (.) for structure/class members  Arrow (->) for pointer to structure/class.`

19 Implementing user defined type Time with a structure C-style structure  No “interface”  If implementation of this data structure changes, all programs using that structure must change accordingly.  Can not print as a whole  Must print/format member by member  Can not compare entirely  Must compare member by member.

20 Implementing Time abstract data type with a class Classes in C ++  Model objects  Attributes (data members)  Behaviors (member functions)  Defied by using keyword class Member access specifiers  Public:  Accessible whenever object of the class is in scope  Private  Accessible only within the member functions of a class  Protected:

21 Implementing Time abstract data type with a class Constructor Function  Special member function  Initializes data members  Same name as the class  Called when object of a class is instantiated  Can have several constructor with different number and type of parameters –Function overloading  N return type

22 Class Time definition class Time { public: Time();// constructor void setTime (int h, int m, int s ); // set hr, min and sec void printUniversal (); // in universal time format void printStandard (); // in standard time format private: int hour; // int minute;// int second; // };

23 Implementing Time ADT with a class Objects of a class  After class definition  Class name can be treated as new type specifier.  Objects, arrays, pointers and references of a specific class can be declared.  Examples: Time sunset; // object of type Time Time arrayOfTimes[ 10 ];// Array of Time objects Time *pointerToTime;// Pointer to a Time object Time &dinnerTime = sunset; // Reference to a Time object

24 Implementing Time ADT with a class Member Functions defined outside class  Binary scope resolution operator (::)  Ties member name to class name  Uniquely identify functions of particular class  Different classes can have members of same name.  Format for defining member functions ReturnType ClassName :: MemberFunctionName (Arguments) { ….} Does not change whether function is declared as public or private. Member Functions defined inside class  Do not need a scope resolution operator or a class name to be specified.

25 // Time Class #include using std:: cout; class Time { // Time ADT definition public: Time();// constructor void setTime (int h, int m, int s ); // set hr, min and sec void printUniversal (); // in universal time format void printStandard (); // in standard time format private: int hour; // int minute;// int second; // };// End of Class Time

26 // Time constructor initializes each data member to zero and ensures that all Time objects start with a consistent state. Time :: Time() { hour = minute = second = 0; } // setTime() sets new Time values using universal method, performs validity checks on the data values and set invalid values to zero. void Time :: setTime (int h, int m, int s) { hour = ( (h >= 0) && (h < 24) ) ? h : 0; minute = ( (m >= 0) && (m < 60) ) ? m : 0; second = ( (s >= 0) && (s < 60) ) ? s : 0; } // end function setTime() ….. // other member function definitions here int main() { Time t; // instantiate object t of class Time …. }

27 Advantages of using Class Simplify programming Interfaces  Hide implementation Software Reuse  Aggregation  Class objects can be members of other classes.  Inheritance  New classes derived from old.

28 Key features of C++ Classes, single/multiple inheritance Streamed I/O Exceptions Templates Namespaces Run-time type information Standard library

29 C++ vs. Java Hybrid Real machine code Minimal standard library Memory management Single/multiple inheritance Easy to access low level system information Everything is a class Byte code Massive APIs Garbage collection Single inheritance Difficult to access system information.

30 Important !!! Take home exercise will be given in the next class. Last class – Quiz 4 All lab related problem should be taken care of by this Friday. After that, nothing can be done.