Fundamentals of Programming. Lecture 19 Hamed Rasifard

Similar documents
Lecture 7. Log into Linux New documents posted to course webpage

AN OVERVIEW OF C++ 1

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Short Notes of CS201

Object-Oriented Programming

CS201 - Introduction to Programming Glossary By

Dot and Scope Resolution Operator

Introduction to C++ Systems Programming

CSCE Practice Midterm. Data Types

Implementing an ADT with a Class

Introduction to the C programming language

Introduction to the C programming language

C++ basics Getting started with, and Data Types.

CIS 190: C/C++ Programming. Classes in C++

CSCE 110 PROGRAMMING FUNDAMENTALS

Evolution of Programming Languages

Object Oriented Design

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Cpt S 122 Data Structures. Introduction to C++ Part II

Where do we go from here?

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

CS24 Week 3 Lecture 1

Object Oriented Design

C++ For Science and Engineering Lecture 15

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Data Structures using OOP C++ Lecture 3

SFU CMPT Topic: Classes

Dr. Md. Humayun Kabir CSE Department, BUET

Lecture 18 Tao Wang 1

COMP 2355 Introduction to Systems Programming

Scientific Computing

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

TEMPLATE IN C++ Function Templates

Introduction to Object-Oriented Programming with C++

Objects and streams and files CS427: Elements of Software Engineering

EECS402 Lecture 02. Functions. Function Prototype

Intermediate Programming, Spring 2017*

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

CSE 303: Concepts and Tools for Software Development

September 10,

COMPUTER SCIENCE 2002 (Delhi Board)

Fundamentals of Programming Session 24

BITG 1113: Function (Part 2) LECTURE 5

CMSC 202 Midterm Exam 1 Fall 2015

9. Arrays. Compound Data Types: type name [elements]; int billy [5];

Programmazione. Prof. Marco Bertini

Abstract Data Types. Different Views of Data:

Programming in C/C Lecture 3

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Today USING POINTERS. Functions: parameters and arguments. Todaywewilllookattopicsrelatingtotheuseofpointers

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

CSCE Practice Midterm. Data Types

Fundamentals of Programming Session 23

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Object Oriented Programming COP3330 / CGS5409

Exceptions, Case Study-Exception handling in C++.

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

CSCI 1370 APRIL 26, 2017

Understanding main() function Input/Output Streams

Associate Professor Dr. Raed Ibraheem Hamed

EL2310 Scientific Programming

CS24 Week 4 Lecture 2

Introduction to C++ Introduction to C++ 1

1 Unit 8 'for' Loops

Lab Instructor : Jean Lai

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

CPSC 427: Object-Oriented Programming

Programming in C++: Assignment Week 8

CSC 309/404 Section 901/910 Spring 2017 Midterm Exam Due: May 7 (Sun) 2015, 11:59 pm

Fast Introduction to Object Oriented Programming and C++

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Pointers. Developed By Ms. K.M.Sanghavi

A A B U n i v e r s i t y

COEN244: Class & function templates

CMSC 4023 Chapter 11

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

Computer Science II Lecture 1 Introduction and Background

OBJECT ORIENTED PROGRAMMING USING C++

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

4. C++ functions. 1. Library Function 2. User-defined Function

Module 7 b. -Namespaces -Exceptions handling

Partha Sarathi Mandal

Programming Language. Functions. Eng. Anis Nazer First Semester

Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370

CSI33 Data Structures

Chapter 11. Abstract Data Types and Encapsulation Concepts

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

Chapter 1: Object-Oriented Programming Using C++

Recharge (int, int, int); //constructor declared void disply();

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

From Pseudcode Algorithms directly to C++ programs

Computer Programming

Chapter 10 Introduction to Classes

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

Transcription:

Fundamentals of Programming Lecture 19 Hamed Rasifard 1

Outline C++ Object-Oriented Programming Class 2

C++ C++ began as an expanded version of C. C++ improves on many of C s features and provides object-oriented-programming (OOP) capabilities that increase software productivity, quality and reusability. This chapter dis- cusses many of C++ s enhancements to C. 3

Object-oriented Programming a program can be organized in one of two ways: around its code (what is happening) around its data (who is being affected) Using only structured programming techniques, programs are typically organized around code. Object-oriented programs organized around data, with the key principle being "data controlling access to code." 4

A Sample C++ Program #include <iostream> using namespace std; int main() { int i; cout << "This is output.\n"; // this is a single line comment /* you can still use C style comments */ // input a number using >> cout << "Enter a number: "; cin >> i; // now, output a number using << cout << i << " squared is " << i*i << "\n"; return 0; } 5

C++ Standard Library C++ programs consist of pieces called classes and functions. most C++ programmers take advantage of the rich collections of existing classes and functions in the C++ Standard Library. You should learn how to use the classes and functions in the C++ Standard Library. 6

Header File The C++ Standard Library is divided into many portions, each with its own header file. The header files contain the function prototypes for the related functions that form each portion of the library. A header file instructs the compiler on how to interface with library and userwritten components. 7

Namespaces A namespace is simply a declarative region. The purpose of a namespace is to localize the names of identifiers to avoid name collisions. Elements declared in one namespace are separate from elements declared in another. When you include a new-style header in your program, the contents of that header are contained in the std namespace. 8

C++Classes A class is a user-defined type. A class is similar syntactically to a structure. Classes consist of data members and member functions 9

A class declaration class class-name { private data and functions access-specifier: data and functions access-specifier: data and functions //... access-specifier: data and functions } object-list; 10

Access Control There are three kinds of access specifier to data in classes Private Public Protected Data are private in a class by default 11

A Simple Class #define SIZE 100 // This creates the class stack. class stack { int stck[size]; int tos; public: void init(); void push(int i); int pop(); }; void stack::init() { //Code goes here } void stack::push(int i) { //Code goes here } int stack::pop() { //Code goes here } 12

Scope Resolution Operator In C++, several different classes can use the same function name. The :: is called the scope resolution operator. The compiler knows which function belongs to which class because of the scope resolution 13

Using Classes #include <iostream> using namespace std; #define SIZE 100 // This creates the class stack. class stack {. };. int main() { stack stack1, stack2; // create two stack objects stack1.init(); stack2.init(); stack1.push(1); stack2.push(2); stack1.push(3); stack2.push(4); cout << stack1.pop() << " "; cout << stack1.pop() << " "; cout << stack2.pop() << " "; cout << stack2.pop() << "\n"; return 0; } 14

Constructors The use of functions such as init() to provide initialization for class objects is inelegant and error-prone. A better approach is to allow the programmer to declare a function with the explicit purpose of initializing objects. Because such a function constructs values of a given type, it is called a constructor. A constructor is recognized by having the same name as the class itself. 15

Constructors(Cont.) When a class has a constructor, all objects of that class will be initialized. If the constructor requires arguments, these arguments must be supplied class Date{ int y, m, d; public: //... Date(int, int, int); Date(int, int); Date(int); Date(); Date(const char*); }; // day, month, year // day, month, today s year // day today s month and year // default Date: today // date in string representation 16