COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Similar documents
COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Ramana Isukapalli W3101: Programming Languages C++

W3110: Programming Languages C++ Ramana Isukapalli

Abstract classes const member functions const arguments to a function Function overloading and overriding

Passing arguments to functions by. const member functions const arguments to a function. Function overloading and overriding Templates


Function Overloading and Overriding this keyword static members inline functions Passing arguments by values and reference

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Object Oriented Programming. Solved MCQs - Part 2

CS 162, Lecture 25: Exam II Review. 30 May 2018

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Fast Introduction to Object Oriented Programming and C++

Object Oriented Pragramming (22316)

Higher Secondary Second Year COMPUTER SCIENCE Model question Paper - 3

Pointers, Dynamic Data, and Reference Types

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

CS349/SE382 A1 C Programming Tutorial

EL2310 Scientific Programming

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

EL2310 Scientific Programming

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CSE 333 Midterm Exam 7/27/15 Sample Solution

CSE 333 Midterm Exam 2/14/14

W3101: Programming Languages C++ Ramana Isukapalli

END TERM EXAMINATION

Intermediate Programming, Spring 2017*

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

ESC101N: Fundamentals of Computing End-sem st semester

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

CS201 Some Important Definitions

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

CSC231 C Tutorial Fall 2018 Introduction to C

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

G Programming Languages - Fall 2012

Kurt Schmidt. October 30, 2018

This exam is to be taken by yourself with closed books, closed notes, no calculators.

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

ONE DIMENSIONAL ARRAYS

Advanced Systems Programming

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V.

ARRAYS(II Unit Part II)

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING REWRAP TEST I CS6301 PROGRAMMING DATA STRUCTURES II

CS201- Introduction to Programming Current Quizzes

Programming, numerics and optimization

C programming basics T3-1 -

CS 61c: Great Ideas in Computer Architecture

G52CPP C++ Programming Lecture 13

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

Babaria Institute of Technology Computer Science and Engineering Department Practical List of Object Oriented Programming with C

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

CS61C : Machine Structures

Programming in C++: Assignment Week 1

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

AN OVERVIEW OF C++ 1

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

CPSC 427: Object-Oriented Programming

C++ : Object Oriented Features. What makes C++ Object Oriented

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

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Object Oriented Design

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Introduction to Programming Using Java (98-388)

C Programming Review CSC 4320/6320

CSE 5A Final Fall 2006

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

PRINCIPLES OF OPERATING SYSTEMS

COMSW Introduction to Computer Programming in C

CSE 303: Concepts and Tools for Software Development

CS61C : Machine Structures

Character Strings. String-copy Example

Data Representation and Storage. Some definitions (in C)

Computers Programming Course 10. Iulian Năstac

CS 222: Pointers and Manual Memory Management

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

Computers Programming Course 11. Iulian Năstac

Introduction to C/C++ Lecture 5 - String & its Manipulation

C++_ MARKS 40 MIN

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

Introduction to C++ Systems Programming

C++ (Non for C Programmer) (BT307) 40 Hours

CS3157: Advanced Programming. Outline

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

How about them A s!! Go Oaktown!! CS61C - Machine Structures. Lecture 4 C Structures Memory Management Dan Garcia.

HOLIDAYS HOMEWORK CLASS : XII. Subject : Computer Science

CS24 Week 3 Lecture 1

Transcription:

COMS W3101 Programming Language: C++ (Fall 2015) ramana@cs.columbia.edu

Lecture-2 Overview of C continued C character arrays Functions Structures Pointers C++ string class C++ Design, difference with C Concepts of Object oriented Programming Concept of class and Object Constructor and destructor Data and Member functions Data encapsulation public, private and protected members 2

C character arrays

C uses character arrays for strings C uses character arrays for strings. C O L U M B I A Useful string functions strlen find the length of a string. strcmp compares two strings Returns 0 if they match. strstr check if a string is sub-string of another string. strcat concatenate two strings. Many others 4 CS3101: Programming Languages: C++

C++ string class

C++ strings C uses char arrays to represent strings char arrays are messy Need to predefine the size of array Size can t be increased easily for longer strings. Copying strings need to use strcpy. C++ strings don t have these issues. E.g. string str1 = abc ; string str2 = str1; string str3 = str1 + pqr ; Much more convenient than C character arrays 6 CS3101: Programming Languages: C++

C functions A group of statements To perform a task Possibly return a value Syntax <return_type> fn_name (arguments) // function body } 7

C functions example Example int square (int x) /* fn to compute square*/ return (x * x); } void main() int i = 5; int i_sq = square (5); cout << square of 5: << i_sq << endl; } /* Starting point of ANY C program*/ 8

C structs C struct used to contain > 1 basic data types Can contain other structs E.g., typedef struct int a, b, c; float x,y,z; } mystruct; mystruct m; m.a = 1; 9

C pointers A pointer points to a memory location. E.g., int x; /* x is an integer */ int *y; /* y points to an integer */ x = 5; *y = 6; /* NOT y = 6! */ Pointers can point to any data type; int *x; short *y; char *str; double *z; void *p, etc. 10

C pointers, contd. Why do we need pointers? Mainly to manage the memory, as opposed to the compiler managing memory. User needs to assign and delete memory. Allocate memory using malloc. Delete memory using free. Examples int *x = (int *) malloc (sizeof (int)); *x = 3; free (x); 11

C functions A group of statements To perform a task Possibly return a value Syntax <return_type> fn_name (arguments) // function body } 12

C functions example Example int square (int x) /* fn to compute square*/ return (x * x); } void main() int i = 5; int i_sq = square (5); printf ( Square of 5 is: %d\n, i_sq); } /* Starting point of ANY C program*/ 13

C structs C struct used to contain > 1 basic data types Can contain other structs E.g., typedef struct int a, b, c; float x,y,z; } mystruct; mystruct m; m.a = 1; 14

C pointers A pointer points to a memory location. E.g., int x; /* x is an integer */ int *y; /* y points to an integer */ x = 5; *y = 6; /* NOT y = 6! */ Pointers can point to any data type; int *x; short *y; char *str; double *z; void *p, etc. 15

C pointers, contd. Why do we need pointers? Mainly to manage the memory, as opposed to the compiler managing memory. User needs to assign and delete memory. Allocate memory using malloc. Delete memory using free. Examples int *x = (int *) malloc (sizeof (int)); *x = 3; free (x); 16

C++

C++ Philosophically different from C High level features of C++ Uses concepts of object oriented programming (OOP) Everything that works in C works in C++ C syntax, operators, structures, control statements, etc. work in C++ Reverse is NOT true Object Oriented Programming Concept of class/object, methods, inheritance, encapsulation, abstraction, polymorphism Key concepts in this Separation of data and methods 18

Class methods Syntax: <ret_type> class::functionname(args) // code } Method code can be present in class definition Outside the class definition In a separate file Example void account::withdrawmoney (int amount) // code 19 }

A simple account example class account private: int user_ssn; // data int accountnumber; // data public: void withdrawmoney (int amount); // method void depositmoney (int amount); // method: void computeinterest( ); // method }; account x; // x is an object of class account 20 W3101: Programming Languages C++

Constructor and Destructor

Constructor and destructor contd. Constructor o A function with the same name as the class o Called when an object is created o A class can have more than one constructor Destructor o Called when an object is cleaned up (goes out of scope) o One class can have only one destructor Examples account x; // constructor code is called account *y = new account; // constructor code is called delete (y); // destructor code is called 22

Constructor and destructor Constructor code account::account( ) user_ssn = -1; accountnumber = -1; } account::account( ) : user_ssn (-1), accountnumber(-1) } account::account (int ssn, int acctnum) user_ssn = ssn; accountnumber = acctnum; } Destructor code account::~account( ) // Any memory/resource cleanup, etc. } 23

Initializing member values class Account private: int balance; public: Account ( ) : balance (0) } Account (int amount) : balance (amount) } }; class checkingaccount : public Account checkingaccount (int amount) : Account (amount) } } 24